#!/bin/sh -e

##########################################################################
#   Script description:
#       Common admin tasks
#       
#   History:
#   Date        Name        Modification
#   2016-01-01  J Bacon     Begin
##########################################################################

usage()
{
    printf "Usage: $0\n"
    exit 1
}


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

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


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

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

if ! auto-root-check $0; then
    # 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
    printf "Root "
    exec su -m root -c "$absolute $@"
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.. List available auto-admin scripts
Q.. Quit

EOM

    read -p 'Selection? ' resp
    case 0$resp in
    01)
	printf '\nThe following users are currently logged in:\n\n'
	w
	printf '\nProceed with system updates? y/[n] '
	read 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
	    read -p 'Reboot? [y]/n ' reboot
	    if [ 0$reboot != 0n ]; then
		read -p "Minutes before reboot? [2] " 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)
	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
