#!/usr/bin/env bash
#
# FreeBSD Packaging Test Script
# Copyright (C) 2010-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

# shellcheck disable=SC2002
PACKAGE="$(cat Makefile | grep "^PORTNAME=" | sed -e "s/^PORTNAME=//g" | tr -d " \t")"
# shellcheck disable=SC2002
UPSTREAM_VERSION="$(cat Makefile | grep "^DISTVERSION=" | sed -e "s/^DISTVERSION=//g" | tr -d " \t")"
# shellcheck disable=SC2002
PORTREVISION="$(cat Makefile | grep "^PORTREVISION=" | sed -e "s/^PORTREVISION=//g" | tr -d " \t")"
# shellcheck disable=SC2002
CATEGORY="$(cat Makefile | grep "^CATEGORIES=" | sed -e "s/^CATEGORIES=//g" | tr -d " \t")"

PACKAGE_VERSION="${UPSTREAM_VERSION}"
if [ "${PORTREVISION}" != "" ] ; then
   PACKAGE_VERSION="${UPSTREAM_VERSION}_${PORTREVISION}"
fi


echo "######################################################################"
echo "PACKAGE:          ${PACKAGE}"
echo "UPSTREAM_VERSION: ${UPSTREAM_VERSION}"
echo "PACKAGE_VERSION:  ${PACKAGE_VERSION}"
echo "CATEGORY:         ${CATEGORY}"
echo "######################################################################"


if [ -e work ] ; then
   rm -rf work
fi
rm -f "${PACKAGE}-${PACKAGE_VERSION}.txz"
if [ -d work/pkg ] ; then
   find work/pkg -name "${PACKAGE}*.txz" -type f -exec rm -f {} +
fi


echo "1. ###### make deinstall ##############################################" && \
make deinstall && \
echo "2. ###### make install ################################################" && \
make install && \
echo "3. ###### make package ################################################" && \
make package && \
echo "4. ###### make deinstall ##############################################" && \
make deinstall && \
echo "5. ###### pkg add #####################################################" && \
if [ ! -e "work/pkg/${PACKAGE}-${PACKAGE_VERSION}.pkg" ] ; then
   echo >&2 "ERROR: Package work/pkg/${PACKAGE}-${PACKAGE_VERSION}.pkg not found!"
   exit 1
fi && \
pkg add work/pkg/"${PACKAGE}"-"${PACKAGE_VERSION}".pkg && \
echo "6. ###### make deinstall ##############################################" && \
make deinstall && \
echo "7. ###### make reinstall ##############################################" && \
make reinstall && \
echo "8. ###### make package ################################################" && \
make package && \
echo "9. ###### tar tzvf *.pkg ##############################################" && \
tar tzvf "work/pkg/${PACKAGE}-${PACKAGE_VERSION}.pkg" && \
echo "Running portlint ..." && \
portlint && \
echo "====== Successfully completed! ======"
