#! /bin/bash
#########################################################################
#									#
#	alarmclock is automatically generated,				#
#		please do not modify!					#
#									#
#########################################################################

#########################################################################
#									#
# Script ID: alarmclock							#
# Author: Copyright (C) 2012-2019, 2021, 2022  Mark Grant		#
#									#
# Released under the GPLv3 only.					#
# SPDX-License-Identifier: GPL-3.0-only					#
#									#
# Purpose:								#
# To invoke the AlarmClock jar package.					#
#									#
# Syntax:	alarmclock [ -h || --help || -V || --version ]		#
#									#
# Exit codes used:-							#
# Bash standard Exit Codes:	0 - success				#
#				1 - general failure			#
# User-defined exit code range is 64 - 113				#
#	C/C++ Semi-standard exit codes from sysexits.h range is 64 - 78	#
#		EX_USAGE	64	command line usage error	#
#		EX_DATAERR	65	data format error		#
#		EX_NOINPUT	66	cannot open input		#
#		EX_NOUSER	67	addressee unknown		#
#		EX_NOHOST	68	host name unknown		#
#		EX_UNAVAILABLE	69	service unavailable		#
#		EX_SOFTWARE	70	internal software error		#
#		EX_OSERR	71	system error (e.g., can't fork)	#
#		EX_OSFILE	72	critical OS file missing	#
#		EX_CANTCREAT	73	can't create (user) output file	#
#		EX_IOERR	74	input/output error		#
#		EX_TEMPFAIL	75	temp failure; user is invited	#
#					to retry			#
#		EX_PROTOCOL	76	remote error in protocol	#
#		EX_NOPERM	77	permission denied		#
#		EX_CONFIG	78	configuration error		#
#	User-defined (here) exit codes range 79 - 113:			#
#		None							#
#									#
# Further Info:								#
#									#
#########################################################################

#########################################################################
#									#
# Changelog								#
#									#
# Date		Author	Version	Description				#
#									#
# 07/11/2012	MG	1.0.1	Created.				#
# 15/06/2013	MG	1.0.2	Changed command line processing to	#
#				use getopts.				#
# 20/08/2013	MG	1.0.3	Debian best practice documentation	#
#				requires that programs placed in the	#
#				PATH should not have extensions as the	#
#				programs may be rewritten in a		#
#				different language. This would then	#
#				require a name change, or, if left	#
#				as-is would cause confusion. So name	#
#				changed from AlarmClock.sh to		#
#				AlarmClock.				#
# 13/01/2014	MG	1.0.4	Changed stdout & stderr message output	#
#				to use a function directing to one or	#
#				other based on a status. Changed	#
#				version option to -V. Added usage of	#
#				standard error routine.			#
# 13/11/2014	MG	1.0.5	Switched from getopts to GNU getopt to	#
#				allow long options.			#
# 19/11/2014	MG	1.0.6	Change FreeBSD specifics to *BSD and	#
#				change Linux to be the default.		#
# 25/11/2014	MG	1.0.7	Add overall package version to -V.	#
# 08/12/2014	MG	1.0.8	Add jar version information to -V.	#
# 10/12/2014	MG	1.0.9	Change exit code from 1 to 64 for	#
#				invalid argument.			#
# 27/06/2015	MG	1.0.10	Remove BSD support.			#
# 07/10/2015	MG	1.0.11	Standardise on jarlocation substitution	#
#				variable.				#
# 23/10/2015	MG	1.0.12	Conform to use AutoTools General	#
#				Template v1.0.6 which uses pkgdatadir	#
#				as installation location of jar files.	#
# 25/06/2017	MG	1.0.13	Enforce 80 column rule.			#
# 04/12/2017	MG	1.0.14	Add SPDX license tags to source files.	#
# 08/12/2017	MG	1.0.15	Adopt standard script exit codes; 0 on	#
#				success, 1 on failure.			#
# 18/07/2018	MG	1.0.16	Manual merge with the Java project.	#
# 08/03/2019	MG	1.1.1	Do not hard code outputprefix contents.	#
#				Make loops and ifs more C-like.		#
#				Refactor into functions.		#
#				Use new extjarloc configure variable	#
#				for external standard jar location for	#
#				use in the ClassPath.			#
#				Use new configure variables projjarloc	#
#				and projjarname to enable testing in	#
#				local build directory.			#
#				Use (( ... )) where appropriate.	#
#				Use [[ ... ]] where appropriate.	#
#				Generally use printf rather than echo.	#
#				Remove script_exit_code.		#
#				Add missing error handling after getopt	#
#				Address local, readonly and global	#
#				variables.				#
#				Replace #! env bash with absolute bash	#
#				path via configure.			#
# 22/06/2019	MG	1.1.2	Modernise getopt processing.		#
#				Setup trap handling ASAP.		#
# 14/10/2019	MG	1.1.3	Move script_exit() before it is used.	#
# 29/11/2021	MG	1.1.4	Tighten SPDX tag.			#
# 04/03/2022	MG	1.1.5	Add processing of config file to get	#
#				new HiDPI scaling factor.		#
#									#
#########################################################################


##################
# Init variables #
##################

readonly outputprefix="$(basename $0):"
readonly version=1.1.5				# Script version
readonly packageversion=1.3.7	# Package version

readonly projjarloc=/usr/share/java
readonly projjarname=alarmclock-java.jar
readonly extjarloc=/usr/share/java

readonly etclocation=/etc		# Path to config directory


#############
# Functions #
#############

# -h --help output.
# No parameters
# No return value
usage ()
{
cat << EOF
Usage is $0 [options]
	-h or --help displays usage information
	OR
	-V or --version displays version information
EOF
}

# Standard function to emit messages depending on various parameters.
# Parameters -	$1 What:-	The message to emit.
#		$2 Where:-	stdout == 0
#				stderr != 0
# No return value.
output()
{
	if (( !$2 )); then
		printf "%s %s\n" $outputprefix "$1"
	else
		printf "%s %s\n" $outputprefix "$1" 1>&2
	fi
}

# Standard function to tidy up and return exit code.
# Parameters - 	$1 is the exit code.
# No return value.
script_exit()
{
	exit $1
}

# Standard function to test command error and exit if non-zero.
# Parameters - $1 is $?
# No return value.
std_cmd_err_handler()
{
	if (( $1 )); then
		script_exit $1
	fi
}

# Standard trap exit function.
# No parameters.
# No return value.
trap_exit()
{
	local -i exit_code=$?
	local msg

	msg="Script terminating with exit code $exit_code due to trap received."
	output "$msg" 1 0
	script_exit $exit_code
}

# Setup trap.
trap trap_exit SIGHUP SIGINT SIGQUIT SIGTERM

## Process the config file.
# Parameters - None
# No return value.
proc_config_file()
{
	local input=()
	local oldIFS=$IFS
	local scaling_factor

	IFS="="

	exec 3<"$etclocation/alarmclock-java.conf"
	while read -u3 -ra input; do
		case ${input[0]} in
		scaling_factor)
			scaling_factor=${input[1]}
			cmdline="java -cp "$extjarloc/*
			cmdline+=":$projjarloc/$projjarname"
			cmdline+=" -Dsun.java2d.uiScale="$scaling_factor
			cmdline+=" alarmclock.AlarmClock"
			;;
		esac
	done
	exec 3<&-

	IFS=$oldIFS
}

 Process command line arguments with GNU getopt.
# Parameters -	$1 is the command line.
# No return value.
proc_CL()
{
	local GETOPTTEMP
	local tmp

	tmp="getopt -o hV --long help,version"
	GETOPTTEMP=$($tmp -n "$0" -- "$@")
	std_cmd_err_handler $?

	eval set -- "$GETOPTTEMP"
	std_cmd_err_handler $?

	while true; do
		case "$1" in
		-h|--help)
			usage
			shift
			script_exit 0
			;;
		-V|--version)
			printf "%s Script version %s\n" $0 $version
			printf "%s Package version %s\n" $0 $packageversion
			printf "Java command line -\n"
			printf "%s\n" "$cmdline"
			eval "$cmdline -V"
			shift
			script_exit 0
			;;
		--)	shift
			break
			;;
		*)	output "Internal error." 1
			script_exit 64
			;;
		esac
	done

	# Script does not accept other arguments.
	if (( $# > 0 )); then
		output "Invalid argument." 64
		script_exit 64
	fi
}


########
# Main #
########

proc_config_file

proc_CL "$@"

eval "$cmdline"
std_cmd_err_handler $?

script_exit 0

