#*******************************************************************************
#**  This file is part of Pecunia.                                           ***
#**                                                                          ***
#**  Copyright (C) 2017, 2018, 2019, 2020, 2021, 2023                        ***
#**  John Schneiderman <Licencing _AT_ Schneiderman _DOT_ me>                ***
#**                                                                          ***
#**  This program is free software: you can redistribute it and/or modify it ***
#**  under the terms of the GNU Lesser 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 Lesser General Public License for more details.             ***
#**                                                                          ***
#**  You should have received a copy of the GNU Lesser General Public License***
#**  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
#*******************************************************************************

#!/usr/bin/env bash
shopt -s -o nounset
shopt -s -o pipefail
shopt -s -o errexit
shopt -s extglob

# Program Variables
#___________________________________________________________________________________________________
declare -r scriptName=${0##*/}
declare -r path=$(dirname $0)
declare -r options="b:hr:"
declare optionResult=""
declare -r projectName="pecunia"
declare version=""
declare -r rpmBuildDirectoryPath="$HOME/rpmbuild"
declare -r rpmSpecDirectoryPath="${rpmBuildDirectoryPath}/SPECS"
declare tarballFileName=""
declare -r specFileName="${projectName}.spec"
declare branch=""
declare release=0

# Program Argument Validation
#___________________________________________________________________________________________________
while getopts "${options}" optionResult
do
	case ${optionResult} in
	b)  # Set Branch Name
		branch="$OPTARG"
		;;
	h)  # Show help
		printf "usage: ${scriptName} [options]\n"
		printf "Options:\n"
		printf "  -b <name>       Sets the name of the branch to package.\n"
		printf "  -r <release>    Sets the package release number.\n"
		exit 0
		;;
	r)  # Set Package Release Number
		release="$OPTARG"
		;;
	\?)
		exit 2
		;;
	*)
		printf "Unknown option: ${optionResult}\n" 1>&2
		exit 2
		;;
	esac
done

if [[ -z "${branch}" ]]
then
	echo "The branch name of ${projectName} must be specified." 1>&2
	exit 1
fi

if (( ${release} <= 0 ))
then
	echo "The package release number is missing or not a number." 1>&2
	exit 1
fi

# Program Execution
#___________________________________________________________________________________________________
pushd "$path/.."
version=$(grep VERSION CMakeLists.txt | tail -1 | awk '{ print $2 }')

if [[ -z "${version}" ]]
then
	echo "Failed to determine the version of ${projectName}."
	exit 1
else
	echo "Determine the version of ${projectName} to be ${version}."
	tarballFileName="${projectName}-${version}.tar.gz"
fi
echo "Generating project tarball..."
hg pull
hg update --clean --rev "${branch}"
hg archive "${tarballFileName}" --rev "${branch}"
mv "${tarballFileName}" "${rpmBuildDirectoryPath}/SOURCES"
cp --verbose "pkg/${specFileName}" "${rpmSpecDirectoryPath}"
echo "Building RPM package..."
export BINARY_VERSION="${version}" &&  \
	export PACKAGE_RELEASE="${release}" &&  \
	export BUILD_TIME="$(export LC_ALL=C ; date '+%a %b %d %Y')" &&  \
	rpmbuild -ba --clean --rmsource --rmspec "${rpmSpecDirectoryPath}/${specFileName}"
echo "Signing RPM package..."
rpm --addsign  \
	"${rpmBuildDirectoryPath}/RPMS/noarch/"${projectName}*-${version}-*.rpm  \
	"${rpmBuildDirectoryPath}/RPMS/x86_64/"${projectName}*-${version}-*.rpm  \
	"${rpmBuildDirectoryPath}/SRPMS/"${projectName}*-${version}-*.rpm
echo "Successfully created RPM package."
popd
