#!/bin/sh -e

##########################################################################
#   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),
#       auto-software-manager(1), auto-network-manager(1),
#       auto-power-manager(1), auto-filesys-manager(1),
#       auto-security-manager(1), auto-sys-manager(1),
#       auto-services-manager(1)
#       
#   History:
#   Date        Name        Modification
#   2016-01-01  J Bacon     Begin
##########################################################################

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

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


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

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


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


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

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

timeout=60   # Exit from su if idle time too long

# 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 system
2.. User management
3.. Software management
4.. Network management
5.. Power management
6.. File system actions and settings
7.. Security settings
8.. System settings
9.. Services manager
10.. List available auto-admin scripts
Q.. Quit

EOM

    read -t $timeout -p 'Selection? ' resp
    case 0$resp in
    01)
	printf '\nThe following users are currently logged in:\n\n'
	finger
	printf '\nProceed with system updates? y/[n] '
	read -t $timeout proceed
	if [ 0$proceed = 0y ]; then
	    cat << EOM | wall

This server may need to be rebooted shortly to complete the installation of
important security updates.  An additional notice will follow if a reboot
is necessary.

EOM
	    auto-update-system --defaults
	    printf "Reboot? [y]/n "
	    read reboot
	    if [ 0$reboot != 0n ]; then
		printf "Minutes before reboot? [2] "
		read minutes
		if [ 0$minutes = 0 ]; then
		    minutes=2
		fi
		cat << EOM | wall

This server will be rebooted shortly to complete the installation of
important security updates.  It should be available again after about
$((minutes + 5)) minutes.

EOM
		shutdown -r +$minutes
	    fi
	fi
	;;
    
    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
