#!/bin/bash
# Filename:      terminalserver-config
# Purpose:       configuration program for grml-terminalserver
# Authors:       grml-team (grml.org), (c) Michael Gebetsroither <gebi@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################


###
### __INCLUDES
###
. /etc/grml/sh-lib
#. /etc/grml/sysexits-sh



###
### __VARIABLES
###

verbose_=0

# this file holds all variable definitions
SHARED_PROG_VARS_="/usr/share/grml-terminalserver/shared_prog_vars"
isExistent $SHARED_PROG_VARS_ die
. $SHARED_PROG_VARS_

# variables used in the config file for grml-terminalserver
INTERFACE_=""
IP_=""
NETMASK_=""
GW_=""
NAMESERVERS_=""
IPRANGE_FROM_=""
IPRANGE_TO_=""
NETWORK_=""
OPTIONS_=""
BOOT_ARGS_=""
NAT_INTERFACE_=""


###
### __FUNCTIONS
###

function printUsage
{
  cat <<EOT
Usage: "$PROG_NAME__" [OPTIONS] <command>

$PROG_NAME__ is the config program for the terminalserver coming with grml.

COMMANDS:

   help             This help text
   interactive      Interactive Configuration of the grml-terminalserver
   initrd           Only create the initrd
   clean            Remove all configfiles created during user configuration
   <default>        interactive mode

OPTIONS:
   -v               verbose (show what is going on, v++)
   -h               this help text

EOT
}


function writeConfig
{
  local date_=""

  if [ -f $CONF_FILE_ ]; then
    mv -fb $CONF_FILE_ ${CONF_FILE_}-old
  fi

  date_=`date`
  cat <<EOT > $CONF_FILE_
# GRML TERMINAL-SERVER CONFIG
# created on "$date_"
INTERFACE_="$INTERFACE_"
IP_="$IP_"
NETWORK_="$NETWORK_"
NETMASK_="$NETMASK_"
GW_="$GW_"
NAMESERVERS_="$NAMESERVERS_"
IPRANGE_FROM_="$IPRANGE_FROM_"
IPRANGE_TO_="$IPRANGE_TO_"
OPTIONS_="$OPTIONS_"
BOOT_ARGS_="$BOOT_ARGS_"
NAT_INTERFACE_="$NAT_INTERFACE_"

EOT
  notice "config successfully safed to \"$CONF_FILE_\""
}


# AUTOMATIC CONFIGURATION  {{{
function checkParamArg
{
  local param_name="$1"
  local arg="$2"

  #eval "echo $`echo $test`"
  echo $arg |grep "^[-|+]" &>/dev/null || return

  die "Argument from $param_name looks like another parameter \"$arg\"" 1
}

function actionAutoconf
{
  checkParamArg "-i" "$interface_"
}
# }}}

# INITRD {{{
function actionMkInitrd
{
  echo "Installing initrd $PATH_/initrd.img:"
  # until we have a stable file location API let's use
  # an according heuristic
  initrd_=initrd.img-"$(uname -r)"
  cp /boot/"$initrd_" "$PATH_"/initrd.img || die "Could not copy /boot/$initrd_"
}

# }}}

# INTERACTIVE CONFIGURATION  {{{

function actionInteractive
{
  local i=""

  dprint "running in interactive mode"

  local card_title_="Choose network device connected to client network"
  local card_message_="Available network devices:"
  local iprange_title_="IP Address range for clients"
  local iprange_message_="
Please enter the desired IP-Range of addresses that should be allocated by clients, separated by a single space.

Example:
          192.168.0.101 192.168.0.200

for addresses from 192.168.0.101 to (and including) 192.168.0.200.

"
  local runconfig_title_="Networkcard config"
  local runconfig_message_="Would you like to configure your interfaces now?"

  # on which interfaces should we listen
  local netdevices_="$(grep -ve 'lo:' -ve 'Inter-|' -ve 'face |bytes' /proc/net/dev | awk -F: '{print $1}')"
  local device_list_=""
  for INTERFACE_ in $netdevices_; do device_list_="$device_list_ ${INTERFACE_} Networkcard_${INTERFACE_##eth}"; done
    echo -n "" >"$TMP_"
    $DIALOG_ --backtitle "$BACK_TITLE_" --title "$card_title_" --menu "$card_message_" \
      0 0 18 $device_list_ 2>"$TMP_" || warn "could not get network-interface"
  INTERFACE_="$(<$TMP_)" ; echo -n "" >"$TMP_"

  while true; do
    IP_=`netGetIp "$INTERFACE_" warn`
    NETMASK_=`netGetNetmask "$INTERFACE_" warn`
    netValidIp "$IP_" warn && break
    $DIALOG_ --backtitle "$BACK_TITLE_" --title "$runconfig_title_" --yesno "$runconfig_message_" 18 45 && \
      netcardconfig || die "Could not get interface" $?
  done

  IPRANGE_FROM_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMin/{print $2}'`
  # if we have x.x.x.1 as starting range address provide
  # x.x.x.10 instead so we avoid possible conflicts with
  # default gateway
  if echo $IPFROM | grep -c '\.1$' ; then
    IPFROM="${IPFROM%%\.1}.10"
  fi
  IPRANGE_TO_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMax/{print $2}'`
  NETWORK_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/Network:/{print $2}'`
  NETWORK_=${NETWORK_%/*}
  local iprange_=""
  while [ -z "$IPRANGE_FROM_" -o -z "$IPRANGE_TO_" -o -z "$iprange_" ]; do
    iprange_="$IPRANGE_FROM_ $IPRANGE_TO_"
    echo -n "" >"$TMP_"
    $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$iprange_title_ ($INTERFACE_=$IP_/$NETMASK_)" \
      --inputbox "$iprange_message_" 18 75 "$iprange_" 2>"$TMP_" || die "problems getting network range" $?

    iprange_="$(<$TMP_)"
    IPRANGE_FROM_="${iprange_%% *}"
    IPRANGE_TO_="${iprange_##* }"

    for i in "$IPRANGE_FROM_" "$IPRANGE_TO_"; do
      netValidIp "$i" warn || iprange_=""
    done
  done

  NAMESERVERS_=`netGetNameservers warn`
  GW_=`netGetDefaultGateway warn`
  GW_DEV_=`/sbin/ip route get "$GW_" | sed 's/^local //' | awk '{ print $3; exit; }'`
  if [ "$GW_DEV_" != "$INTERFACE_" ] && [ "$GW_DEV_" != "" ] && [ "$GW_DEV_" != "lo" ]; then
    # GW_DEV_ of server is not the same device as the one serviced by dhcpd
    # so it doesn't make sense to provide the GW_ address to the clients
    local do_nat_="YES"
    local do_nat_title_="Network Address Translation"
    local do_nat_message_="
Do you want to set up NAT so that clients booting from this
grml-terminalserver can use this machine also as gateway to
the internet?"

    $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$do_nat_title_" --yesno "$do_nat_message_" 15 75 || \
      do_nat_="NO"
    if [ "$do_nat_" = "YES" ]; then
      # user wants NAT, we give the clients the server address as
      # gateway as well
      GW_="$IP_"
      NAT_INTERFACE_="$GW_DEV_"
    else
      # no NAT, no sensible gateway
      GW_=""
      NAT_INTERFACE_=""
    fi
  fi

  echo -n "" >"$TMP_"
  local OPTIONS_BOOTARG_MESSAGE_="Here you can add additional boot arguments for the clients seperated by spaces:

Quite useful examples:

ssh=<pw>              - Start ssh server and set password of user grml to pw
services=<1,2,3>      - Execute /etc/init.d/{1,2,3} start
console=ttyS0,9600n8  - Initialise serial console
startx                - Boot into X


"
  $DIALOG_ --clear --no-collapse --backtitle "$BACK_TITLE_" --title "$OPTIONS_TITLE_" --inputbox "$OPTIONS_BOOTARG_MESSAGE_" 0 0\
    2>$TMP_ || die "problems getting additional boot arguments"
  BOOT_ARGS_="$BOOT_ARGS_ $(<$TMP_)"

  writeConfig
  actionMkInitrd
  notice "GRML terminalserver successfully configured"
}
# }}}

function removeTmpFiles
{
  execute "rm -f $TMP_" warn
}

function actionClean
{
  for i in dhcpd.conf initrd.img; do
    execute "rm -f $PATH_/$i*"
  done

  for i in $CARDS_DETECTED_BY_DISCOVER $CONF_FILE_; do
    execute "rm -f $i"
  done

}


###
### __MAIN
###

while getopts "i:hv" opt; do
  case "$opt" in
    i) interface_=$OPTARG ;;
    h) printUsage; exit ;;
    v) let verbose_=$verbose_+1 ;;
    ?) printUsage; exit 64 ;;
  esac
done
shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
setVerbose $verbose_

case "$1" in
  help)   printUsage; exit 0 ;;
esac

checkRoot die 'You have to be root to use this program'
disableSyslog

execute "mkdir -p $PATH_" die

TMP_=`mktemp -t grml-terminalserver-config.XXXXXX` || die "Could not create tmpfile" $?
setExitFunction 'removeTmpFiles'


. $DEFAULT_CONFIG_
. $CONFIG_

case "$1" in
  interactive)  actionInteractive ;;
  initrd) actionMkInitrd ;;
  clean)  actionClean ;;
  *)    actionInteractive ;;
esac

removeTmpFiles
# END OF FILE
################################################################################
# vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2
