#!/bin/bash
# Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: To reset the home or auto-login account for all DRBL clients, this command is run in the server. It is different from autologin-home-reset, which is run in client and only reset its own autologin account.

# Load DRBL setting and functions
if [ ! -f "/opt/drbl/sbin/drbl-conf-functions" ]; then
  echo "Unable to find /opt/drbl/sbin/drbl-conf-functions! Program terminated!" 
  exit 1
fi
. /opt/drbl/sbin/drbl-conf-functions

#
usage() {
  echo "Delete the auto login accounts in DRBL."
  echo "Usage: $0 [OPTION]"
  echo "Options:"
  language_help_prompt_by_idx_no
  echo "-v, --verbose:  verbose mode."
  echo "-h, --host IP_ADDRESS:  set only for the host with IP_ADDRESS instead of all DRBL clients"
}

#
check_if_root

#
     
# main
while [ $# -gt 0 ]; do
  case "$1" in
    -l|--language)
		shift; specified_lang="$1"
		shift;;
    -h|--host)
		shift; specified_host="$1"
		shift;;
    -v|--verbose)
		shift; verbose="on"
                ;;
    -*)		echo "${0}: ${1}: invalid option" >&2
		usage >& 2
		exit 2 ;;
    *)		break ;;
  esac
done

ask_and_load_lang_set $specified_lang
#
if [ -n "$specified_host" ]; then
 [ ! -d "$drblroot/$specified_host" ] && echo "Can NOT find DRBL client $specified_host (i.e. no $drblroot/$specified_host)! Program terminated!" && exit 1
 [ -n "$verbose" ] && echo "specified_host: $specified_host"
fi

# set the host to be processed
# host_list is the IP address of client, like 192.168.1.1...
host_list=""
if [ -n "$specified_host" ]; then
   # set the host path
   host_list=$drblroot/$specified_host
else
   # withoud specified_host, it must be all clients, append each one to $host_list
   for ihost in $drblroot/*; do
     host_list="$host_list $ihost"
   done
fi

account_list=
# get the autologin account
for ihost in $host_list; do
  iaccount="`get_autologin_account $ihost`"
  account_list="$account_list $iaccount"
done

# remove the leading space
account_list=`echo $account_list | sed -e "s/^ //g"`

[ -z "$account_list" ] && echo "$msg_no_autologin_account! $msg_program_stop" && exit 1

#
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_are_you_sure_reset_autologin_home_dir ?"
echo "$msg_warning_home_dir_will_be_deleted!!!"
echo "$msg_those_accounts_are:"
[ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
echo "$account_list"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[y/N] "
read reset_account_confirm
case "$reset_account_confirm" in
	Y|y|[yY][eE][sS])
	   echo "$msg_ok_let_do_it!"
	   ;;
	*)
	   echo "$msg_do_not_reset_autologin_home_dir! $msg_program_stop!"
	   exit 0
esac

for id in $account_list; do
  USER_HOME_DIR=`grep "^$id:" /etc/passwd | awk -F':' '{print $6}'`
  if [ -n "$USER_HOME_DIR" -a -d "$USER_HOME_DIR" ]; then
    echo -n "Reseting account $id's home directory $USER_HOME_DIR..."
    [ -n "$verbose" ] && rsync_opt_verbose="--progress"
    rsync -a --delete $rsync_opt_verbose /etc/skel/ $USER_HOME_DIR/
    auto_login_id="$(id -n -u $id)"
    auto_login_group="$(id -n -g $id)"
    chown -R $auto_login_id.$auto_login_group $USER_HOME_DIR/
  fi
  echo "done!"
done
