#!/bin/sh
# Steven Shiau <steven@nchc.org.tw>
# License: GPL
# Description: To set the client to be graphic mode and autologin now

# 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:"
  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
    -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

# check if specified host exists
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


# set the host to be autologin, make sure it is, then run init 3 and init 5.
for ihost in $host_list; do
  host_ip=$(basename $ihost)
  $DRBL_SCRIPT_PATH/sbin/drbl-login-switch --auto --host $host_ip
  $DRBL_SCRIPT_PATH/bin/drbl-doit --no-ping --host $host_ip "init 3; sleep 3; init 5"
done
