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

#########################################################################
#									#
# Author: Copyright (C) 2013-2017, 2019, 2021, 2023, 2024  Mark Grant	#
# Author: Copyright (C) 2026  Mark Grant				#
#									#
# Released under the GPLv3 only.					#
# SPDX-License-Identifier: GPL-3.0-only					#
#									#
# Purpose:								#
# Retrieves one or more files whose URL's are specified in a text file.	#
#                                         	                     	#
# 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 #
##################
outputprefix="$(basename "$0"):"
readonly outputprefix
readonly version=1.2.7			# set version variable
readonly packageversion=1.4.1	# Version of the complete package

readonly conffile="$HOME/.mget"
owner=false
ownergroup=false
persist=false
script_exit_code=0
sourcefile=""
targetdir=""
unix=false
verbosity="-nv"		# wget defaults to verbose so make non-verbose default
windows=false


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

# -h --help output.
# No parameters
# No return value
usage()
{
cat << EOF
${0##*/} [-g GROUP] [-o OWNER] [-p] [{-q|-s|-v}] [-S SOURCEFILE] [-T TARGETDIR]
		[{-u|-w}]
${0##*/} {-h|-V}

Usage is:-
${0##*/} [OPTIONS]
	-g or --group newgroup Save target file with 'newgroup' owner.
	-h or --help Displays usage information.
	-o or --owner newowner Save target file with 'newowner' owner.
	-p or --persist Saves command line options for future use.
	-q or --quiet Quiet mode, limited output.
	-s or --silent Silent mode, no output.
	-S or --sourcefile sourcefile Name of file containing source URL's.
		Can be persisted with -p.
	-T or --targetdir targetdir Name of directory for storing retrieved
		files. Can be persisted with -p.
	-u or --unix Use Unix LF line endings instead of Windows CR/LF line
		endings.
	-v or --verbose Verbose mode.
	-V or --version Displays version information.
	-w or --windows Use Windows CR/LF line endings instead of Unix LF
		endings.
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.
# 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

# Function to write config file.
# No parameters.
# No return value.
write_file()
{
	printf "%s\n" "sourcefile=$sourcefile" >> "$conffile"
	printf "%s\n" "targetdir=$targetdir" >> "$conffile"
}

# Read or create config file.
# No parameters.
# No return value.
proc_config_file()
{
	local old_IFS

	if [[ -f $conffile ]]; then
		old_IFS=$IFS
		IFS="="
		exec 3<"$conffile"
		while read -u3 -ra input; do
			case ${input[0]} in
			sourcefile)
				sourcefile=${input[1]}
				;;
			targetdir)
				targetdir=${input[1]}
				;;
			esac
		done
		exec 3<&-
		IFS=$old_IFS
	fi
}

# 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 g:ho:pqsS:T:uvVw "
	tmpGETOPTTEMP+="--long group:,help,owner:,persist,quiet,silent,"
	tmpGETOPTTEMP+="sourcefile:,targetdir:,unix,verbose,version,windows"
	GETOPTTEMP=$($tmpGETOPTTEMP -n "$0" -- "$@")
	std_cmd_err_handler $?

	eval set -- "$GETOPTTEMP"
	std_cmd_err_handler $?

	while true; do
		case "$1" in
		-g|--group)
			ownergroup=true
			newownergroup=$2
			shift 2
			;;
		-h|--help)
			usage
			shift
			script_exit 0
			;;
		-o|--owner)
			owner=true
			newowner=$2
			shift 2
			;;
		-p|--persist)
			persist=true
			shift
			;;
		-q|--quiet)
			verbosity="-q"
			shift
			;;
		-s|--silent)
			verbosity=""
			exec 1>/dev/null 2>/dev/null
			shift
			;;
		-S|--sourcefile)
			sourcefile=$2
			shift 2
			;;
		-T|--targetdir)
			targetdir=$2
			shift 2
			;;
		-u|--unix)
			unix=true
			shift
			;;
		-v|--verbose)
			verbosity="-v"
			shift
			;;
		-V|--version)
			printf "%s Script version %s\n" "$0" $version
			printf "%s Package version %s\n" "$0" $packageversion
			shift
			script_exit 0
			;;
		-w|--windows)
			windows=true
			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

	# u and w flag cannot both be set.
	if $windows && $unix; then
		output "-u and -w cannot both be set." 1
		script_exit 64
	fi
}

# Check source file exists and is readable and writeable.
# No parameters.
# No return value.
validate_source_file()
{
	if [[ ! -f "$sourcefile" ]]; then
		output "Source file not valid." 1
		script_exit 66
	fi

	if [[ ! -r "$sourcefile" ]]; then
		output "Source file not readable." 1
		script_exit 77
	fi

	if [[ ! -w "$sourcefile" ]]; then
		output "Source file not writeable." 1
		script_exit 77
	fi
}

# Check target directory exists and is writable.
# No parameters.
# No return value.
validate_target_dir()
{
	if [[ ! $targetdir ]]; then
		targetdir=$PWD
	fi

	if [[ ! -d "$targetdir" ]]; then
		output "Target directory not valid." 1
		script_exit 66
	fi

	if [[ ! -w "$targetdir" ]]; then
		output "Target directory not writeable." 1
		script_exit 77
	fi
}


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

proc_config_file

proc_CL "$@"

validate_source_file

# Now ensure source file is in Unix text format.
wutconv --unix "$sourcefile"
std_cmd_err_handler $?

validate_target_dir

# Now get the files
touch "$sourcefile".new

old_IFS=$IFS
IFS="/"
exec 3<"$sourcefile"
while read -u3 -ra input; do
	targetdir="${targetdir%/}"
	outputfile=${input[${#input[*]}-1]}
	output "Attempting to get file - ""${input[*]}" 0
	output "Attempting to save file to - ""$targetdir/$outputfile" 0

	wget $verbosity --no-check-certificate -P "$targetdir" "${input[*]}"
	status=$?

	output "File get completed with status "$status $status
	((script_exit_code +=  status))

	# If this wget errored then skip to the next.
	if (( status )); then
		rm -f "$targetdir/$outputfile"
		printf "%s\n" "${input[*]}" >> "$sourcefile".new
		continue
	fi

	# If Windows CR/LF line ending required, convert.
	if $windows; then
		wutconv --windows --verbose "$targetdir/$outputfile"
		status=$?
		((script_exit_code += status))
	fi
	# If Unix LF line ending required, convert.
	if $unix; then
		wutconv --unix --verbose "$targetdir/$outputfile"
		status=$?
		((script_exit_code += status))
	fi
	# Change owner?
	if $owner; then
		chown "$newowner" "$targetdir/$outputfile"
		status=$?
		output "Owner change complete with status "$status $status
		((script_exit_code += status))
	fi
	#Change group?
	if $ownergroup; then
		chown :"$newownergroup" "$targetdir/$outputfile"
		status=$?
		msg="Owner group change complete with status "$status
		output "$msg" $status
		((script_exit_code += status))
	fi
done
exec 3<&-
IFS=$old_IFS

rm -f "$sourcefile"
mv -f "$sourcefile".new "$sourcefile"

# If persist is TRUE, save parameters.
if $persist; then
	rm -f "$conffile"
	write_file
fi

# And exit
output "Script complete with exit code: $script_exit_code" "$script_exit_code"
script_exit "$script_exit_code"

