#! /bin/bash
#########################################################################
#									#
# Script ID: txt2manwrap						#
# Author: Copyright (C) 2015-2019, 2021  Mark Grant			#
#									#
# Released under the GPLv3 only.					#
# SPDX-License-Identifier: GPL-3.0-only					#
#									#
# Purpose:								#
# Wrapper for txt2man to perform some string manipulation not possible	#
# in Makefile.am on parameters to be passed to txt2man.			#
#									#
# For example, ignoring subsequent parameters, a Makefile might invoke	#
# txt2manwrap with the makefile target which would become		#
# txt2manwrap -t mget.1 which txt2manwrap would convert to		#
# txt2man -t mget stripping the final '.*'.				#
#									#
# Syntax:	txt2manwrap MakefileTargetFileName \			#
#				ManualSectionNumber \			#
#				ManualVolumeName \			#
#				MakefileSourceTextFileFullName \	#
#				MakefileTargetManualFullFileName	#
#		OR							#
#		txt2manwrap	[ -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							#
#									#
#########################################################################

#########################################################################
#									#
# Changelog								#
#									#
# Date		Author	Version	Description				#
#									#
# 19/09/2015	MG	1.0.1	Created.				#
# 02/12/2017	MG	1.0.2	Add SPDX license tags to source files.	#
#				Adopt normal exit code policy; 0 on	#
#				success, 1 on failure.			#
# 13/04/2018	MG	1.0.3	Improve comments explaining process.	#
# 04/01/2019	MG	1.0.4	Use absolute bash path in shebang	#
#				instead of env. Using env is OK for a 	#
#				non-AutoTools script.			#
# 06/02/2019	MG	1.1.1	Do not hard code outputprefix contents.	#
#				Remove script_exit_code variable 	#
#				propogating the exit code as a function	#
#				argument instead.			#
#				Improve trap function.			#
#				Implement local / global variables.	#
#				Add help and version CLAs.		#
#				Improve exit status.			#
#				Ensure numerics use (( )) AOT [[ ]].	#
# 16/03/2019	MG	1.1.2	Add missing error check after getopt.	#
# 28/04/2019	MG	1.1.3	Use $( .. ) AOT backticks.		#
# 28/04/2019	MG	1.1.4	Correct regression in 1.1.3 by fixing	#
#				getopt command line.			#
# 30/04/2019	MG	1.1.5	Keep getopt option with relevant option	#
#				argument on the CL build. Improves	#
#				readability.				#
# 27/11/2021	MG	1.1.6	Tighten SPDX tag.			#
#									#
#########################################################################

##################
# Init variables #
##################
readonly outputprefix="$(basename $0):"
readonly version=1.1.6				# set version variable
readonly packageversion=1.2.1		# package version


#############
# 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 == 1
# 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 the exit code, (normally $? from the preceeding command).
# 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 exit_code=$?
	local msg

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

# Setup trap.
trap trap_exit SIGHUP SIGINT SIGQUIT SIGTERM

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

	tmpGETOPTTEMP="getopt -o hV --long help,version"
	GETOPTTEMP=$($tmpGETOPTTEMP -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
			shift
			script_exit 0
			;;
		--)	shift
			break
			;;
		*)	output "Internal error." 1
			script_exit 64
			;;
		esac
	done

	# Script does not accept other arguments.
	if (( $# != 5 )); then
		output "Takes 5 arguments." 1
		script_exit 64
	fi
}


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

proc_CL "$@"

# ${var%Pattern} Remove from $var the shortest part of $Pattern that matches the
# back end of $var.
# So ignoring subsequent parameters, a Makefile might invoke txt2manwrap with
# the makefile target which would become txt2manwrap -t mget.1 which txt2manwrap
# would convert to txt2man -t mget stripping the final '.*'.
txt2man -t ${1%.*} -s $2 -v "$3" $4 > $5
txt2man_exit_code=$?

if (( txt2man_exit_code )); then
	output "txt2man failed with exit code: "$txt2man_exit_code 1
	script_exit $txt2man_exit_code
fi

output "Script completed successfully." 0

script_exit 0

