#!/bin/sh -e

##########################################################################
#   Title:
#       Optional, defaults to the name of the script sans extention
#
#   Name:
#       script - short scription
#
#   Section:
#       8
#
#   Synopsis:
#       auto-admin
#       
#   Description:
#       Main menu for common auto-admin tasks.
#       
#   Arguments:
#       None
#       
#   Returns:
#       0 upon successful exit, non-zero if certain commands failed
#
#   See also:
#       auto-update-system(1), auto-user-admin(1),
#       
#   FIXME:  Write a script to auto-generate this man page with a SEE ALSO
#           for all existing man pages.
#
#   History:
#   Date        Name        Modification
#   2016-01-01  J Bacon     Begin
#   2024-11-12  Jason Bacon Add man page template and usage
##########################################################################

##########################################################################
#   Function description:
#       Pause until user presses return
##########################################################################

pause()
{
    local junk
    
    printf "Press return to continue..."
    read $read_flags junk
}


su_exit()
{
    printf "Exiting root process due to excessive idle time.\n"
    exit 0
}


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

if [ $# != 0 ]; then
    usage
fi

# FIXME: Add other platforms that support read -t timeout
if [ $(auto-ostype) = FreeBSD ]; then
    # Exit from su if idle time too long
    read_flags='-t 60'
fi

# FIXME: Figure out how to trap read -t timeout.  This doesn't work.
# trap su_exit ALRM
# trap -l

# Prevent user from running a Trojan as root in the case their account
# was compromised
absolute="$(which $0)"
# Don't count on -e being set at this point
if ! auto-file-secure "$absolute"; then
    exit 1
fi

if ! auto-root-check $0; then
    printf "Root "
    # exec quotes '$absolute --flag', causing usage error
    # Assigning to cmd works around the problem
    cmd="$absolute $@"
    exec su -m root -c "$cmd"
fi

while true
do
    clear
    auto-admin-banner
    cat << EOM

This menu system encompasses only a small fraction of the total auto-admin
functionality.  To see what else is available via the command-line, choose
"List available auto-admin scripts" below.

Full documentation is in the works and will be included in a future release.

1.. Update manager
2.. User manager
3.. Software manager
4.. Network manager
5.. Power manager
6.. File system actions and settings
7.. Security settings
8.. System settings
9.. Services manager
10.. List available auto-admin scripts
Q.. Quit

EOM

    printf 'Selection? '
    read $read_flags resp
    case 0$resp in
    01)
	auto-update-manager
	;;
    
    02)
	auto-user-admin
	;;

    03)
	auto-software-manager
	;;

    04)
	auto-network-manager
	;;
	
    05)
	auto-power-manager
	;;
    
    06)
	auto-filesys-manager
	;;
    
    07)
	auto-security-manager
	;;
    
    08)
	auto-sys-manager
	;;
    
    09)
	auto-services-manager
	;;
    
    010)
	bin=$(dirname `which auto-admin`)
	ls $bin/auto-* | more
	printf "$(ls $bin/auto-* | wc -l) scripts in total.\n"
	pause
	;;

    0Q|0q)
	exit 0
	;;

    *)
	printf "Invalid option: $resp\n"
    esac
done
