#!/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

#
accept_one_nic="no"

#
# 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 "-e, --accept-one-nic    Accept to run DRBL service in only one network card. ///NOTE/// This might mess up your network environment especially if there is an exi sting DHCP service in your network environment."
  echo "-p, --port_client_no    The client no. in each NIC port."
  echo "-k, --client_archi [0/1/2]: Set the client's CPU arch, 0 for i386, 1 for i586, 2 for same level with DRBL server (for x86_64, use 2)."
  echo "-t, --testing [y/n]:    Use packages in testing branch or not (Only works for RH-like distribution)."
  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)"
  language_help_prompt_by_idx_no
}

#
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" ;;
   -e|--accept-one-nic)
        shift; accept_one_nic="yes" ;;
   -k|--client_archi)
        shift
        if [ -z "$(echo $1 |grep ^-.)" ]; then
          # skip the -xx option, in case 
          client_archi_ans="$1"
          shift
        fi
        ;;
   -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 [ -n "$client_archi_ans" ]; then
  client_archi_ans_opt="-k $client_archi_ans"
fi

if [ -n "$accept_one_nic" ]; then
  accept_one_nic_opt="--accept-one-nic"
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
     [ -n "$verbose" ] && echo "Running drblsrv -i $client_archi_ans_opt $testing_opt $unstable_opt"
     yes "" | drblsrv -i $client_archi_ans_opt $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
     [ -n "$verbose" ] && echo "Running drblpush -i $accept_one_nic_opt $port_client_no_opt $clonezilla_mode_opt"
     yes "" | drblpush -i $accept_one_nic_opt $port_client_no_opt $clonezilla_mode_opt
     ;;
esac
