#!/bin/sh

##########################################################################
#   Script description:
#       Manage user accounts
#       
#   History:
#   Date        Name        Modification
#   2014-01-08  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

case $(auto-ostype) in
FreeBSD)
    pw_file=master.passwd
    ;;

RHEL)
    pw_file=shadow
    ;;

*)
    printf "$0: Not supported on $(auto-ostype).\n"
    pause
    exit 1
    ;;

esac

LOCALBASE=$(cluster-localbase)

while true; do

    clear
    spcm-banner
    cat << EOM
    
1.. Add a new user
2.. Change a user password
3.. Synchronize a user password to all nodes
4.. Change a user password expiration date (to days after today)
5.. Change password expiration date for all users (to days after today)
6.. Add a user to a supplemental group
7.. Remove a user from a supplemental group
8.. Restore / transfer a user account (using same uid/gid)
9.. Recreate a user account (possibly new uid/gid)
Q.. Quit / Return to main menu

EOM

    printf "Selection? "
    read selection
    clear
    case $selection in
    1)
	spcm-banner
	cluster-adduser
	;;
    2)
	spcm-banner
	printf "\nUsername? "
	read user_name
	if [ 0$user_name != 0 ] && id $user_name > /dev/null; then
	    cluster-passwd $user_name
	fi
	;;
    3)
	spcm-banner
	printf "\nUsername? "
	read user_name
	cluster-sync-pw $user_name
	;;
    4)
	spcm-banner
	printf "\nUsername? "
	read user_name
	printf "Expire passwords in how many days? "
	read days
	cluster-passwd-user-expiration $user_name $days
	;;
    5)
	spcm-banner
	printf "\nThis will reset password expiration date for ALL users.\n"
	printf "Some users may have special expiration periods, which will be overwritten.\n"
	printf "\nAre you sure you want to proceed? y/[n] "
	read proceed
	if [ 0$proceed = 0y ]; then
	    printf "Expire password in how many days? "
	    read days
	    cluster-passwd-expiration $days
	fi
	;;
    
    6)
	spcm-banner
	printf "\nUsername? "
	read user_name
	if [ 0$user_name != 0 ] && id $user_name; then
	    printf "Add to which group? "
	    read group_name
	    if [ 0$group_name != 0 ]; then
		cluster-add-to-group $user_name $group_name
	    fi
	fi
	;;
    
    7)
	spcm-banner
	printf "\nUsername? "
	read user_name
	if [ 0$user_name != 0 ] && id $user_name; then
	    printf "Remove from which group? "
	    read group_name
	    if [ 0$group_name != 0 ]; then
		cluster-remove-from-group $user_name $group_name
	    fi
	fi
	;;
    
    8)
	spcm-banner
	printf "\n"
	read -p "Username? " user_name
	read -p "Directory with passwd, $pw_file, group, and pw-age? [$LOCALBASE/etc/spcm/Old] " \
	    source_dir
	: ${source_dir:=$LOCALBASE/etc/spcm/Old}
	cluster-restore-user $user_name $source_dir/passwd $source_dir/$pw_file \
	    $source_dir/group $source_dir/pw-age
	;;
    
    9)
	spcm-banner
	printf "\n"
	read -p "Username? " user_name
	read -p "Directory with passwd, $pw_file, group, and pw-age? [$LOCALBASE/etc/spcm/Old] " \
	    source_dir
	: ${source_dir:=$LOCALBASE/etc/spcm/Old}
	read -p "Old home directory? [/home/Old/$user_name] " old_home
	: ${old_home:=/home/Old/$user_name}
	cluster-recreate-user \
	    $user_name $source_dir/passwd $source_dir/$pw_file \
	    $source_dir/group $source_dir/pw-age $old_home
	;;
    
    Q|q)
	exit 0
	;;
    esac
    pause
done
