#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: To select the DRBL clients and show the results (IP or MAC)
# 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

MAX_DIALOG_HEIGHT="10"

#
ask_by_MAC_or_IP() {
  # Note!!! We must use common tmp file (ANS_TMP) to pass the result, 
  # we can NOT
  # just echo result in the end of this function. Otherwise in newer
  # dialog (FC3), the script will wait for the end of function, then it 
  # shows the result. 
  # There is nothing in the screen when function is called if we just 
  # use echo the result to stdout.
  ANS_TMP=$1
  local TMP=`mktemp /tmp/drbl-tmp.XXXXXX`
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
  # if hardware is set in dhcpd.conf, then we can choose hosts via MAC
  # example:  "hardware ethernet  00:02:e3:53:34:6e;
  if grep -q -i "^[[:space:]]*hardware ethernet .*;" $DHCPDCONF_DIR/dhcpd.conf; then
     # we can find the MAC address...
     choose_by_MAC="available"
     $DIA --backtitle "$msg_nchc_free_software_labs" --title "$msg_drbl_title" \
     --radiolist "$msg_choose_the_method:" 12 80 2 \
     "by_MAC_address" "$msg_by_MAC_address" on \
     "by_IP_address" "$msg_by_IP_address" off 2> $TMP
     retval=$?
     [ $retval -eq 1 ] && exit 1
     M="$(cat $TMP)"
     case "$M" in
       "by_MAC_address") 
          gen_HOST_MAC_TABLE
          select_hosts_by_MAC $ANS_TMP
          ;;
       "by_IP_address")
          select_hosts_by_IP $ANS_TMP
          ;;
     esac 
  else
     choose_by_IP="available"
     select_hosts_by_IP $ANS_TMP
  fi
  [ -f "$TMP" ] && rm -f $TMP
  return 0
}

select_hosts_by_IP() {
  # Note!!! We must use common tmp file (ANS_TMP) to pass the result, 
  # we can NOT
  # just echo result in the end of this function. Otherwise in newer
  # dialog (FC3), the script will wait for the end of function, then it 
  # shows the result. 
  # There is nothing in the screen when function is called if we just 
  # use echo the result to stdout.
  ANS_TMP=$1
  local TMP=`mktemp /tmp/ocs.XXXXXX`
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
  local hostlist=""
  local numhosts=0
  # Part I: list the IP group
  for igrp in $drbl_syscfg/client-IP-group-*; do
    [ ! -e "$igrp" ] && continue
    #if [ ! -f $igrp ]; then continue; fi
    host_grp="$(basename $igrp)"
    hostinfo="$(head -n 2 $igrp)"
    hostinfo="$(echo $hostinfo | tr " " "_")..."
    hostlist="$hostlist $host_grp $hostinfo off"
    numhosts=`expr $numhosts + 1`
  done
  # Part II: list the nodes
  for ihost in $drblroot/*; do
    # only directory ..., not file
    if [ ! -d $ihost ]; then continue; fi
    host_ip="$(basename $ihost)"
    hostinfo="$($DRBL_SCRIPT_PATH/bin/parse_net_conf -h $host_ip all HOSTNAME)"
    hostlist="$hostlist $host_ip $hostinfo off"
    numhosts=`expr $numhosts + 1`
  done
  if [ $numhosts -gt 0 ]; then
    if [ $numhosts -lt $MAX_DIALOG_HEIGHT ]; then
      height=$numhosts
    else
      height=$MAX_DIALOG_HEIGHT
    fi
    $DIA \
      --separate-output \
      --backtitle "$msg_nchc_free_software_labs" \
      --title "$msg_drbl_title" \
      --checklist "$msg_specify_hosts:" 0 60 \
      $height $hostlist 2> $TMP
    retval=$?
    [ $retval -eq 1 ] && exit 1
    # Convert the selected group to IP lists
    if grep -q "client-IP-group" $TMP; then
      selected_grp="$(grep -Eo "client-IP-group-[0-9]*" $TMP)"
      for i in $selected_grp; do
        perl -p -i -e "s/$i//g" $TMP
	cat $drbl_syscfg/$i >> $TMP
      done
    fi
    target_hosts=$(cat $TMP)
  else
    echo "No hosts in $drblroot!!!... Abort!"
    exit 1
  fi
  echo $target_hosts > $ANS_TMP
  [ -f "$TMP" ] && rm -f $TMP
  return 0
}

gen_HOST_MAC_TABLE() {
  # host fc3-101 {
  #     hardware ethernet  00:02:e3:53:34:60;
  #     fixed-address 192.168.232.1;
  # }
  # clean the IP-MAC table
  [ ! -f $HOST_MAC_TABLE ] && rm -f $IP_MAC_TABLE
  # To get the hostname - IP table.
  /opt/drbl/bin/parse_dhcpd_conf $HOST_MAC_TABLE
}

select_hosts_by_MAC() {
  # Note!!! We must use common tmp file (ANS_TMP) to pass the result, 
  # we can NOT
  # just echo result in the end of this function. Otherwise in newer
  # dialog (FC3), the script will wait for the end of function, then it 
  # shows the result. 
  # There is nothing in the screen when function is called if we just 
  # use echo the result to stdout.
  local TMP=`mktemp /tmp/drbl-mac-tmp.XXXXXX`
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
  hostlist=""
  # Part I: list the IP group
  for igrp in $drbl_syscfg/client-MAC-group-*; do
    [ ! -e "$igrp" ] && continue
    host_grp="$(basename $igrp)"
    hostinfo="$(head -n 2 $igrp)"
    hostinfo="$(echo $hostinfo | tr " " "_")..."
    hostlist="$hostlist $host_grp $hostinfo off"
    echo $hostlist > $TMP
  done
  # Part II: list the nodes
  awk -F" " '!/^#/ {print $3, $1"("$2")", "off"}' $HOST_MAC_TABLE >> $TMP
  MAC_list="$(cat $TMP)"

  numhosts=$(echo $MAC_list | grep -o off | wc -w)
  if [ $numhosts -gt 0 ]; then
    if [ $numhosts -lt $MAX_DIALOG_HEIGHT ]; then
      height=$numhosts
    else
      height=$MAX_DIALOG_HEIGHT
    fi
    $DIA \
      --separate-output \
      --backtitle "$msg_nchc_free_software_labs" \
      --title "$msg_drbl_title" \
      --checklist "$msg_specify_hosts:" 0 70 \
      $height $MAC_list 2> $TMP
    retval=$?
    [ $retval -eq 1 ] && exit 1
    # Convert the selected group to MAC lists
    if grep -q "client-MAC-group" $TMP; then
      selected_grp="$(grep -Eo "client-MAC-group-[0-9]*" $TMP)"
      for i in $selected_grp; do
        perl -p -i -e "s/$i//g" $TMP
	cat $drbl_syscfg/$i >> $TMP
      done
    fi
    target_hosts=$(cat $TMP)
  else
    echo "No MAC address in dhcpd.conf! Abort! "
    exit 1
  fi

  echo $target_hosts > $ANS_TMP
  [ -f "$TMP" ] && rm -f $TMP
  return 0
}

usage() {
  echo "Description:"
  echo "Select DRBL clients by IP or MAC address"
  echo "Usage: `basename $0` [Options] RESULT_FILE"
  echo "Options:"
  language_help_prompt_by_idx_no
  language_help_prompt_by_idx_name
  echo "RESULT_FILE     The file to store the selected hosts"

}

# option
while [ $# -gt 0 ]; do
  case "$1" in
    -l|--language)
		shift;
                if [ -z "$(echo $1 |grep ^-.)" ]; then
                  # skip the -xx option, in case 
	          language_opt="$1"
                fi
		shift ;;
    -*)		echo "${0}: ${1}: invalid option" >&2
		usage >& 2
		exit 2 ;;
    *)		break ;;
  esac
done

#
HOST_FILE=$1
[ -z "$HOST_FILE" ] && echo "You must specify the target file!!! Program terminated!!!" && exit 1

#
ask_and_load_lang_set $language_opt

# Note!!! We must use common tmp file (ANS_TMP) to pass the result, 
# we can NOT
# just echo result in the end of this function. Otherwise in newer
# dialog (FC3), the script will wait for the end of function, then it 
# shows the result. 
# There is nothing in the screen when function is called if we just 
# use echo the result to stdout.
ANS_TMP=`mktemp /tmp/mac_ip_ans.XXXXXX`
trap "[ -f "$ANS_TMP" ] && rm -f $ANS_TMP" HUP INT QUIT TERM EXIT
ask_by_MAC_or_IP $ANS_TMP
target_hosts="$(cat $ANS_TMP)"
echo $target_hosts > $HOST_FILE
[ -f "$ANS_TMP" ] && rm -f $ANS_TMP
exit 0
