#! /bin/bash
#########################################################################
#									#
#	apt-key-scripts is automatically generated,			#
#		please do not modify!					#
#									#
#########################################################################

#########################################################################
#									#
# Script ID: apt-key-scripts						#
# Author: Copyright (C) 2020, 2021, 2023, 2025, 2026  Mark Grant	#
#									#
# Released under the GPLv3 only.					#
# SPDX-License-Identifier: GPL-3.0-only					#
#									#
# Purpose:								#
# To manage and execute scripts for maintaining apt trusted keys and	#
# keyrings.								#
#									#
# Syntax:	See usage().						#
#									#
# 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:								#
#									#
#########################################################################


##################
# Init variables #
##################
readonly version=1.1.2				# Script version
readonly packageversion=1.4.1		# Package version

readonly scriptloc=/etc/agmaint/trusted.sh.d	# Live scripts
readonly exampleloc=/usr/share/agmaint/trusted.sh.d		# Example scripts

process=false
verbose=()


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

# -h --help output.
# No parameters
# No return value
usage()
{
cat << EOF
Usage is:-
$(basename "$0") [-a] [-c FILENAME] [-e FILENAME] [-h] [-i FILENAME] \\
		[-l] [-r FILENAME] [-v] [-V] [-x]
Usage is:-
$(basename "$0") [options]
	-a or --exec-all Execute all the available scripts
	-c or --cat 'filename' Cat the specified script
	-e or --exec 'filename' Execute the specified script
	-h or --help Displays usage information
	-i or --install 'filename' Install the specified script
	-l or --ls List the available scripts
	-r or --rm 'filename' Remove the specified script
	-v or --verbose Make output verbose
	-V or --version Displays version information
	-x or --install-examples Install the example scripts
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\n" "$1"
	else
		printf "%s\n" "$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.
# shellcheck disable=SC2317  # Do not warn about unreachable commands in trap
# functions, they are legitimate.
trap_exit()
{
	local -i 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

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

	tmp="getopt -o a:c:e:hi:l:r:vVx "
	tmp+="--long exec-all:,cat:,exec:,help,install:,ls:,rm:,verbose,version,"
	tmp+="install-examples"
	GETOPTTEMP=$($tmp -n "$0" -- "$@")
	std_cmd_err_handler $?

	eval set -- "$GETOPTTEMP"
	std_cmd_err_handler $?

	while true; do
		case "$1" in
		-a|--exec-all)
			if $process ; then
				exec_all_scripts "$2"
			fi
			shift 2
			;;
		-c|--cat)
			if $process ; then
				cat_script "$2"
			fi
			shift 2
			;;
		-e|--exec)
			if $process ; then
				exec_script "$2"
			fi
			shift 2
			;;
		-h|--help)
			if $process ; then
				usage
			fi
			shift
			;;
		-i|--install)
			if $process ; then
				install_script "$2"
			fi
			shift 2
			;;
		-l|--ls)
			if $process ; then
				ls_scripts "$2"
			fi
			shift 2
			;;
		-r|--rm)
			if $process ; then
				rm_script "$2"
			fi
			shift 2
			;;
		-v|--verbose)
			verbose=(-v)
			shift
			;;
		-V|--version)
			if $process ; then
				printf "Script version %s\n" $version
				printf "Package version %s\n" $packageversion
			fi
			shift
			;;
		-x|--install-examples)
			if $process ; then
				install_examples
			fi
			shift
			;;
		--)	shift
			break
			;;
		*)	output "Internal error." 1
			script_exit 64
			;;
		esac
	done

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

# Execute the specified script.
# Parameters	- $1 is the script.
# No return value.
exec_script()
{
	sudo "$1" "${verbose[@]}"
	std_cmd_err_handler $?
}

# Execute all available scripts.
# Parameters	- $1 is the script directory.
# No return value.
exec_all_scripts()
{
	local IFS=$'\n'
	local script

	for script in "$1"*; do
		exec_script "$script" "${verbose[@]}"
	done
}

# Cat the specified script.
# Parameters	- $1 is the script.
# No return value.
cat_script()
{
	cat "$1"
	std_cmd_err_handler $?
}

# Install the specified script.
# Parameters	- $1 is the script.
# No return value.
install_script()
{
	sudo cp "${verbose[@]}" "$1" "$scriptloc"
	std_cmd_err_handler $?
	sudo chmod "${verbose[@]}" u+x "$scriptloc/$(basename "$1")"
	std_cmd_err_handler $?
}

# List the script location directory.
# Parameters	- $1 is the script directory.
# No parameters.
# No return value.
ls_scripts()
{
	ls -l "$1"
	std_cmd_err_handler $?
}

# Remove the specified script.
# Parameters	- $1 is the script.
# No return value.
rm_script()
{
	sudo rm "${verbose[@]}" "$1"
	std_cmd_err_handler $?
}

# Install the example scripts provided with the package.
# No parameters
# No return value.
install_examples()
{
	local ex_script_list
	local script

	ex_script_list=$(ls $exampleloc)
	for script in $ex_script_list; do
		install_script "$exampleloc/$script" "${verbose[@]}"
	done
}


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

proc_CL "$@"

process=true
proc_CL "$@"

script_exit 0

