#!/bin/bash
# Steven Shiau <steven@nchc.org.tw>
# License: GPL
# Script to setup DRBL server for the impatient.

# Source function library.
[ -e /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions

# Source DRBL setting
. /opt/drbl/conf/drbl.conf

# Source the drbl related functions
. $DRBL_SCRIPT_PATH/sbin/drbl-functions

# usage
Usage() {
  echo "Usage:"
  echo "$0 [Option]"
  echo "Option:"
  echo "-a, --unstable [y/n]:   use packages in unstable branch or not (Only works for RH-like distribution)."
  echo "-b, --batch:            run $0 in batch mode (non-attended mode)."
  echo "-p, --port_client_no The client no. in each NIC port."
  echo "-t, --testing [y/n]:    use packages in testing branch or not (Only works for RH-like distribution)."
  echo "-l, --language N  Set the language to be shown, where N:"
  echo "                  0: English,"
  echo "                  1: Traditional Chinese (Big5) - Taiwan,"
  echo "                  2: Traditional Chinese (UTF-8, Unicode) - Taiwan."
  echo "-z, --clonezilla_mode [0|1|2]: Assign Clonezilla mode (0: Full DRBL mode, 1: Clonezilla box mode, 2: Do NOT provide clonezilla service to clients)"
}

#
check_if_root

# main
while [ $# -gt 0 ]; do
  case "$1" in
   -l|--language)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         specified_lang="$1"
         shift
       fi
       ;;
   -b|--batch)
        shift; run_4impatient="yes"
        ;;
   -t|--testing)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         drbl_test_answer="$1"
         shift
       fi
       ;;
   -a|--unstable)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         drbl_unstable_answer="$1"
         shift
       fi
       ;;
   -p|--port_client_no)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         assign_port_client_no="$1"
         shift
       fi
       ;;
   -z|--clonezilla_mode)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         clonezilla_mode="$1"
         shift
       fi
       ;;
   -v|--verbose)
       shift; verbose="on"
       ;;
   *)
       Usage
       exit 1
  esac
done

#
ask_and_load_lang_set $specified_lang

if [ -n "$assign_port_client_no" ]; then
  port_client_no_opt="-p $assign_port_client_no"
fi

if [ -n "$drbl_unstable_answer" ]; then
  unstable_opt="-a $drbl_unstable_answer"
fi

if [ -n "$drbl_test_answer" ]; then
  testing_opt="-t $drbl_test_answer"
fi

if [ -n "$clonezilla_mode" ]; then
  clonezilla_mode_opt="-z $clonezilla_mode"
fi

#
if [ -z "$run_4impatient" ]; then
  echo "$msg_delimiter_star_line"
  echo "$msg_are_you_sure_run_impatient"
  echo -n "[Y/n] "
  read run_4impatient
fi

case "$run_4impatient" in
	n|N|[nN][oO])
           echo "$msg_delimiter_star_line"
	   echo "$msg_run_step_by_step: "
           echo "(a):\"$DRBL_SCRIPT_PATH/sbin/drblsrv -i\" "
           echo "(b):\"$DRBL_SCRIPT_PATH/sbin/drblpush -i\" "
	   ;;
	*)
           if [ -z "$run_4impatient" ]; then
             echo "$msg_delimiter_star_line"
	     echo "$msg_run_drbl4imp"
             echo -n "$msg_press_enter_to_continue"
             read 
           fi
	   if [ -x "$drbl_setup_path/drblsrv" ]; then
             [ -n "$verbose" ] && echo "Running $drbl_setup_path/drblsrv -i -l $lang_answer $testing_opt $unstable_opt"
	     yes "" | $drbl_setup_path/drblsrv -i -l $lang_answer $testing_opt $unstable_opt
	     RC=$?
	     if [ $RC -gt 0 ]; then
                [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
		echo "Someting went wrong with drblsrv! Program terminted!!!"
                [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
		exit 1
	     fi
           fi
	   if [ -x "$drbl_setup_path/drblpush" ]; then
	     [ -n "$verbose" ] && echo "Running $drbl_setup_path/drblpush -i -l $lang_answer $port_client_no_opt $clonezilla_mode_opt"
	     yes "" | $drbl_setup_path/drblpush -i -l $lang_answer $port_client_no_opt $clonezilla_mode_opt
           fi
	   ;;
esac
