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

#!/usr/bin/env bash
shopt -s -o nounset  # Don't allow the use of variables that haven't been declared
shopt -s -o pipefail # This option sets the exit code of a pipeline to that of the rightmost
					 # command to exit with a non-zero status, or zero if all commands of the
					 # pipeline exit successfully.
shopt -s -o errexit  # Exit when a command fails
shopt -s extglob  # Allow the use of the extra pattern

# Program Variables
#___________________________________________________________________________________________________
declare -r scriptName=${0##*/}
declare -r path=$(dirname $0)
declare -r options="a:hv:"
declare optionResult=""
declare -r projectName="duxreinummariae"
declare version=""
declare -r rpmBuildDirectoryPath="$HOME/rpmbuild"
declare -r rpmSpecDirectoryPath="${rpmBuildDirectoryPath}/SPECS"
declare tarballFileName=""
declare -r specFileName="${projectName}.spec"
declare apiKey=""

# Program Argument Validation
#___________________________________________________________________________________________________
while getopts "${options}" optionResult
do
	case ${optionResult} in
	a)  # Set API key
		apiKey="$OPTARG"
		;;
	h)  # Show help
		printf "usage: ${scriptName} [options]\n"
		printf "Options:\n"
		printf "  -a <key>        Sets the API key for currency updates.\n"
		printf "  -v <version>    Sets the version to package.\n"
		exit 0
		;;
	v)  # Set Package Version
		version="$OPTARG"
		;;
	\?)
		exit 2
		;;
	*)
		exit 2
		;;
	esac
done

if [[ -z "${version}" ]]
then
	echo "The version of ${projectName} must be specified."
	exit 1
fi

# Program Execution
#___________________________________________________________________________________________________
tarballFileName="${projectName}-${version}.tar.gz"
pushd "$path/.."

echo "Generating project tarball..."
hg archive "${tarballFileName}" -r "${version}"
mv "${tarballFileName}" "${rpmBuildDirectoryPath}/SOURCES"

echo "Copying SPEC file..."
cp "pkg/${specFileName}" "${rpmSpecDirectoryPath}"

echo "Building RPM package..."
export CURRENCY_API_KEY="${apiKey}" &&  \
	export BINARY_VERSION="${version}" && \
	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  \
	|| exit $?

echo "Successfully created RPM package."
popd
