#!/bin/sh -e

##########################################################################
#   Title:
#       Optional, defaults to the name of the script sans extention
#
#   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 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

    printf 'Selection? '
    read $read_flags 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 $read_flags 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
