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

#########################################################################
#									#
# Script ID: agmaint							#
# Author: Copyright (C) 2014-2017, 2019-2021, 2023  Mark Grant		#
#									#
# Released under the GPLv3 only.					#
# SPDX-License-Identifier: GPL-3.0-only					#
#									#
# Purpose:								#
# To simplify the running of daily apt-get maintenance commands.	#
#									#
# Syntax:	agmaint [ -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				#
#									#
# 27/10/2014	MG	1.0.1	Created.				#
# 13/11/2014	MG	1.0.2	Switched from getopts to GNU getopt to	#
#				allow long options.			#
# 16/11/2014	MG	1.0.3	Modify getopt processing to allow for	#
#				FreeBSD's quirk of 2 different getopt	#
#				programs. See comments at the start of	#
#				"Main"					#
# 18/11/2014	MG	1.0.4	Change FreeBSD specifics to *BSD and	#
#				change Linux to be the default.		#
# 25/11/2014	MG	1.0.5	Add overall package version to -V.	#
# 29/03/2015	MG	1.0.6	Remove BSD support.			#
# 01/12/2017	MG	1.0.7	Add SPDX license tags to source files.	#
# 05/12/2017	MG	1.0.8	Adopt standard script exit values; 0 on	#
#				success, 1 on failure.			#
# 22/09/2019	MG	1.1.0	Do not hardcode outputprefix.		#
#				Tidy variable scopes.			#
#				Adopt C shape for if, for, while etc.	#
#				Refactor into functions.		#
#				Standardise common functions.		#
#				Use [[ ... ]] where appropriate.	#
#				Use (( ... )) where appropriate.	#
#				Use more meaninful exit codes.		#
#				Replace env hashpling with absolute	#
#				bash path from configure.		#
# 21/05/2020	MG	1.1.1	Change script to .sh extension.		#
# 03/06/2020	MG	1.1.2	Pass CLA's after a -- straight to the	#
#				apt-get commands.			#
# 22/11/2021	MG	1.1.3	Tighten SPDX tag.			#
# 26/06/2023	MG	1.1.4	Fix shellcheck warnings.		#
# 10/07/2023	MG	1.1.5	Treat cli_extra_args as an array.	#
# 09/10/2023	MG	1.1.6	Drop .sh extension from scripts in	#
#				$PATH.					#
#									#
#########################################################################


##################
# Init variables #
##################
readonly version=1.1.6				# Script version
readonly packageversion=1.2.3		# Package version

cli_extra_args=()


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

# -h --help output.
# No parameters
# No return value
usage ()
{
cat << EOF
Usage is $0 [OPTIONS]
	[OPTIONS] are:-
	-h or --help Displays usage information
	-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\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.
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

# 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 "Script version %s\n" $version
			printf "Package version %s\n" $packageversion
			shift
			script_exit 0
			;;
		--)	shift
			break
			;;
		*)	output "Internal error." 1
			script_exit 64
			;;
		esac
	done

	# Pass anything after the -- straight to the commands.
	if (( $# )); then
		# bash concatenates with a space, fine for later word splitting
		# on command line.
		# shellcheck disable=SC2124
		cli_extra_args=( "$@" )
	fi
}


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

proc_CL "$@"

apt-get "${cli_extra_args[@]}" upgrade
status=$?
output "apt-get upgrade completed with exit code: "$status $status
std_cmd_err_handler $status

apt-get "${cli_extra_args[@]}" autoclean
status=$?
output "apt-get autoclean completed with exit code: "$status $status
std_cmd_err_handler $status

apt-get "${cli_extra_args[@]}" check
status=$?
output "apt-get check completed with exit code: "$status $status
std_cmd_err_handler $status

apt-get "${cli_extra_args[@]}" autoremove
status=$?
output "apt-get autoremove completed with exit code: "$status $status
std_cmd_err_handler $status

script_exit 0

