#!/usr/bin/env bash
# ==========================================================================
#              ____        _ _     _     _____           _
#             | __ ) _   _(_) | __| |   |_   _|__   ___ | |___
#             |  _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __|
#             | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \
#             |____/ \__,_|_|_|\__,_|     |_|\___/ \___/|_|___/
#
#                           --- Build-Tools ---
#                https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Packaging Scripts
# Copyright (C) 2025-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


POUDRIERE_PORTS="/usr/local/poudriere/ports/default/"

# ====== Get port default ===================================================
PORTNAME=$(grep "^PORTNAME=" freebsd/*/Makefile | awk '{ print $2 }')
CATEGORY=$(grep "^CATEGORIES=" freebsd/*/Makefile | awk '{ print $2 }')
echo "Processing ${CATEGORY}/${PORTNAME} ..."

# ====== Update =============================================================
cd freebsd/*
git pull
sudo make makesum

# ====== Update Poudriere ports =============================================
DISTVERSION=$(grep "^DISTVERSION=" Makefile | awk '{ print $2 }')
PORTREVISION=$(grep "^PORTREVISION=" Makefile | awk '{ print "-" $2 }' || echo "")
MASTER_SITES=$(grep "^MASTER_SITES=" Makefile | sed -e "s/MASTER_SITES=//g" | tr -d "\t")

sudo git clean -dfx
sudo rsync -a --info=progress2 --delete ./ "/usr/local/poudriere/ports/default/${CATEGORY}/${PORTNAME}/"
cd "${POUDRIERE_PORTS}/${CATEGORY}/${PORTNAME}/"

DIFF_FILE="${PORTNAME}-${DISTVERSION}${PORTREVISION}.diff"
git diff . | tee ~/diff/"${DIFF_FILE}"

# ====== Test the port by using Poudriere ===================================
sudo poudriere testport -j 143amd64-release -p default -b latest "${CATEGORY}/${PORTNAME}"

# ====== Show FreeBSD Update Bug template ===================================
# shellcheck disable=SC2001
URL=$(echo "${MASTER_SITES}" | sed -e 's#download[/]*$##g')
SUMMARY="${CATEGORY}/${PORTNAME}: Update to new release ${DISTVERSION}"
COMMENT="The latest upstream version of ${PORTNAME} is ${DISTVERSION}.\nTherefore, the port ${CATEGORY}/${PORTNAME} needs an update.\n\nFix: The updated diff file ${DIFF_FILE} is attached.\n"

print-utf8 -s "\e[34m====== FreeBSD Update Bug template " "=" "=\e[0m"
echo ""
echo -e "Summary: \e[34m${SUMMARY}\e[0m"
echo ""
echo -e "Product:      \e[33mPorts & Packages\e[0m"
echo -e "Component:    \e[33mIndividual Ports\e[0m"
echo -e "Bug Severity: \e[33mAffects Some People\e[0m"
echo -e "URL:          \e[34m${URL}\e[0m"
echo ""
echo "Description:"
echo -e "---\e[34m"
echo "The latest upstream version of ${PORTNAME} is ${DISTVERSION}."
echo "Therefore, the port ${CATEGORY}/${PORTNAME} needs an update."
echo ""
echo "Fix: The updated diff file ${DIFF_FILE} is attached."
echo -e "\e[0m---"
echo ""
print-utf8 -s "\e[34m=" "=" "=\e[0m"
echo ""
echo "Submit here:"
echo ""
echo -e "\e[33mhttps://bugs.freebsd.org/bugzilla/enter_bug.cgi?product=Ports%20%26%20Packages&component=Individual%20Port%28s%29&short_desc=$(echo "${SUMMARY}" | sed -e 's/ /%20/g')&comment=$(echo "${COMMENT}" | sed -e 's/\\n/%0A/g' -e 's/ /%20/g')&bug_severity=Affects%20Some%20People&bug_file_loc=${URL}\e[0m"
echo ""
