# Author: Theodore Zacharia
# V0.1: 30/07/2021 - Initial Release
#
# This script provides a template to start all other scripts with
# supports arguments and options being passed
# use -l myoption1: for options with value
# Use shell add on getopt, you may need to install this
#

# *** Globals
TRACE=0
MYARG=""

# *** Functions
_usage()
{
	echo "usage: $0 [-h|--help] [-t] [-a argvalue|--aarg argvalue] positional_params"
	echo "where"
	cat <<- _EOF_
	  -h, --help    Displays this help message
	  -t            Set trace on
_EOF_
}

# *** Mainline

# process input parameters
OPTS=$(getopt -o hta: -l help,aarg: -- "$@")

eval set -- "$OPTS"

while true ; do
case "$1" in
	-h|--help) _usage ; shift ; exit 1 ;;
	-t) TRACE=1 ; shift ;; # set TRACE mode
	-a|--aarg) MYARG=$2 ; shift 2;;
	--) shift ; break ;;	# end of options
	*) echo "$1 is an invalid option" >&2
	   exit 2 ;;
esac
done

shift $((OPTIND-1))

if [ $TRACE -gt 0 ]
then
	echo "Starting at $(date)"
	echo "TRACE=$TRACE" >&2
	echo "MYARG=$MYARG" >&2
	echo "positional_args=$@" >&2
fi