#!/usr/bin/env bash
#
# FreeBSD Ports Packaging Scripts
# Copyright (C) 2017-2026 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com

# Bash options:
set -eu


# ====== Check for location of distinfo file ================================
DISTINFO=`find ./ -maxdepth 1 -name "distinfo" ; if [ -d freebsd/ ] ; then find freebsd/ -mindepth 2 -maxdepth 2 -name "distinfo" ; fi`
if [ "${DISTINFO}" == "" ] ; then
   echo >&2 "ERROR: distinfo not found in ./ or freebsd/*/ directory!"
   exit 1
fi
DISTINFO_DIRECTORY=`dirname "${DISTINFO}" | xargs realpath`
cd "${DISTINFO_DIRECTORY}"

# ====== Check for location of package base directory =======================
cd ../..
if [ ! -e "packaging.conf" ] ; then
   echo >&2 "ERROR: packaging.conf not found in `pwd`!"
   exit 1
fi


# ====== Extract package information ========================================
PORTNAME=`grep "^PORTNAME=" ${DISTINFO_DIRECTORY}/Makefile | awk '{ print $2 }'`
DISTVERSION=`grep "^DISTVERSION=" ${DISTINFO_DIRECTORY}/Makefile | awk '{ print $2 }'`
LOCATION=`grep "^MASTER_SITES=" ${DISTINFO_DIRECTORY}/Makefile | awk '{ print $2 }'`
EXTRACT_SUFX=`grep "^EXTRACT_SUFX=" ${DISTINFO_DIRECTORY}/Makefile | awk '{ print $2 }'`
if [ "${EXTRACT_SUFX}" == "" ] ; then
   if grep "^USES=" ${DISTINFO_DIRECTORY}/Makefile | grep -q "tar:xz" ; then
      EXTRACT_SUFX=".tar.xz"
   elif grep "^USES=" ${DISTINFO_DIRECTORY}/Makefile | grep -q "tar:bz2" ; then
      EXTRACT_SUFX=".tar.bz2"
   else
      EXTRACT_SUFX=".tar.gz"
   fi
fi

echo "PORTNAME:     ${PORTNAME}"
echo "DISTVERSION:  ${DISTVERSION}"
echo "LOCATION:     ${LOCATION}"
echo "EXTRACT_SUFX: ${EXTRACT_SUFX}"


# ====== Check for sources tarball ==========================================
if [ ! -e "${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}" ] ; then
   echo -en "\x1b[34m${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX} does not exist -> Try to download it from ${LOCATION}?  [yes/no]?\x1b[0m "
   read -er applyChanges
   if [ "$applyChanges" == "yes" -o "$applyChanges" == "y" ] ; then
      echo "Downloading ${LOCATION}/${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}"
      wget -4 "${LOCATION}/${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}"
   else
      exit 1
   fi
fi


# ====== Copy sources tarball to /usr/ports/distinfo (FreeBSD) ==============
if [ -e "/usr/ports/distfiles/" ] ; then
   if [ ! -e "/usr/ports/distfiles/${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}" ] ; then
      echo "Copying ${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX} to /usr/ports/distfiles/ ..."
      sudo cp "${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}" /usr/ports/distfiles/
   elif [ -e "${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}" ] ; then
      if ! diff "${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}" "/usr/ports/distfiles/${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}" ; then
         echo -en "\x1b[34m${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX} differs from /usr/ports/distfiles/${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}! Replace version in /usr/ports/distfiles?  [yes/no]?\x1b[0m "
         read -er applyChanges
         if [ "$applyChanges" == "yes" -o "$applyChanges" == "y" ] ; then
            echo "Copying ${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX} to /usr/ports/distfiles/ ..."
            sudo cp "${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}" /usr/ports/distfiles/
         fi
      fi
   fi
fi


# ====== Update distinfo ====================================================
echo "Updating distinfo ..."
(
   echo "TIMESTAMP = `date +%s`"
   if [ -x "$(command -v sha256sum)" ] ; then
      sha256sum --tag ${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}
   else
      sha256 ${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}
   fi
   echo "SIZE (${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX}) = `wc -c ${PORTNAME}-${DISTVERSION}${EXTRACT_SUFX} | awk '{ print $1 }'`"
) >${DISTINFO_DIRECTORY}/distinfo.new

echo "Update:"
if ! diff --color=always -I '^TIMESTAMP.*$' ${DISTINFO_DIRECTORY}/distinfo ${DISTINFO_DIRECTORY}/distinfo.new ; then
   echo -en "\x1b[34mRun: Apply update? [yes/no]?\x1b[0m "
   read -er applyChanges
   if [ "$applyChanges" == "yes" -o "$applyChanges" == "y" ] ; then
      mv ${DISTINFO_DIRECTORY}/distinfo.new ${DISTINFO_DIRECTORY}/distinfo
   fi
   echo "Done!"
else
   echo "No change -> exiting."
fi
