#!/bin/bash
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: reset use's home directory as initial status.
#
# Modified by Steven Shiau <steven@nchc.org.tw> to be used in DRBL for RedHat

# 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

#
check_if_root

#
run_cmd="`basename $0`"

#
usage() {
  echo "set the files in user's home directory."
  echo "Usage: $run_cmd [-g|--group] group [-r|--reset] [-v|--verbose]"
  echo "-g, --group GROUP:  set the GROUP to be processed." 
  echo "-a, --all:  process all user except root." 
  echo "-r, --reset:     reset the file in user's home to initial condition."
  echo "-v, --verbose    prints out verbose information"
  echo "If -g or -a is not specified, only auto-login account will be reset"
}

#
while [ $# -gt 0 ]; do
  case "$1" in
    -g|--group)
		shift; chosen_group="$1"
		shift;;
    -a|--all)
		shift; chosen_group="all"
                ;;
    -v|--verbose)
		shift; VERBOSE="on"
                ;;
    -r|--reset)
		shift; mode="reset"
                ;;
    -*)		echo "${0}: ${1}: invalid option" >&2
		usage >& 2
		exit 2 ;;
    *)		break ;;
  esac
done

# check parameter
if [ -z "$mode" ]; then
  usage
  exit 1
fi

# check if group exists
if [ -n "$chosen_group" -a "$chosen_group" != "all" -a  -z "`grep \"^$chosen_group:\" /etc/group 2>/dev/null`" ]; then
    echo "group $chosen_group does NOT exist!!!"
    exit 1
fi

# the default one for chosen_group is auto_login
[ -z "$chosen_group" ] && chosen_group="auto_login"

#
if [ -n "$VERBOSE" ]; then
   echo "chosen_group=$chosen_group"
   echo "mode=$mode"
fi

# main
grp_member=
case "$chosen_group" in
  all)
    echo "Process all users..."
    #
    IFS_org=$IFS
    IFS=':'
    while read uname x uid gid x x x; do
        # nfs user create a user called "nfsnobody", skip it.
        if [ "$uid" -ge "$UID_BEGIN_DEFAULT" -a "$uname" != "nfsnobody" ]; then
           grp_member="$grp_member $uname"
        fi
    done </etc/passwd
    IFS=$IFS_org
    ;;
  
  auto_login)
    echo "You did NOT specify group or all users, so process auto-login users only..."
    # get the autologin account
    for ihost in $drblroot/*; do
      iaccount="`get_autologin_account $ihost`"
      grp_member="$grp_member $iaccount"
    done
    ;;

  *)
    echo "Process the users in the chosen group..."
    # process the chosen group
    grp_member=$(grep "^$chosen_group:" /etc/group | awk -F':' '{print $4}' | sed -e "s/,/ /g")
    ;;
esac

# remove the leading space if it exists
grp_member=`echo $grp_member | sed -e "s/^ //g"`

[ -n "$VERBOSE" ] && echo "grp_member=$grp_member"

if [ "$mode" = "reset" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Warning! This will delete all files in user's home directory, then reset it as initial condition, these users are:"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$grp_member" 
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Are you sure ?"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read rm_confirm
fi

# store the LC_ALL
LC_ALL_org=$LC_ALL
export LC_ALL=C

# main
for imember in $grp_member; do
  USER_HOME_DIR=`grep "^$imember:" /etc/passwd | awk -F':' '{print $6}'`
  uid=`grep "^$imember:" /etc/passwd | awk -F':' '{print $3}'`
  gid=`grep "^$imember:" /etc/passwd | awk -F':' '{print $4}'`
  group=`grep "^.*:.*:$gid:" /etc/group | awk -F':' '{print $1}'`
  case "$mode" in
    "reset")
      case "$rm_confirm" in
        y|[yY][eE][sS])
          echo -n "Resetting the home directory of $imember..."
          rsync -a --delete --progress /etc/skel/ $USER_HOME_DIR/
          chown -R $imember.$group /etc/skel/ $USER_HOME_DIR/
          echo "done!"
          ;;
        *)
          echo "abort!"
          exit
      esac
      ;;
    *)
      usage
      ;;
  esac
done

# restore the LC_ALL
export LC_ALL=$LC_ALL_org
