#!/usr/bin/env bash

## - On gateway server adjust sudoers file. Edit that
## - file by typing 'visudo' as root user. 
## - 
## - Add lines:
## -    Cmnd_Alias REBOOT = /sbin/reboot
## -    Cmnd_Alias UTILS = /bin/cp

## --- Some functions
## ---
echononl(){
   echo X\\c > /tmp/shprompt$$
   if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
      echo "$*\\c" 1>&2
   else
       echo -e -n "$*" 1>&2
   fi
   rm /tmp/shprompt$$
}

clear

company="Sprachenatelier Berlin"
gateway_ip=192.168.92.254

echo ""
echo -e "\tRestart $company Gateway"
echo ""
echo -e "Choose type of internet connection\n"

_opt1="DSL"
_opt2="VDSL"
_opt3="Abort program"
options=("$_opt1" "$_opt2" "$_opt3" )
PS3="Choose type of connection [1-3]: "

select opt in "${options[@]}"
do
   case $opt in
      $_opt1)
         line=DSL
         break
         ;;
      $_opt2)
         line=VDSL
         break
         ;;
      $_opt3)
         echo -e "\n\tExiting now..\n"
         exit 1
         break
         ;;
      *)
         echo ""
         echo "!! INVALID CHOISE !!"
         echo ""
         echo "1) $_opt1"
         echo "2) $_opt2"
         echo "3) $_opt3"
         echo ""
         ;;
    esac
done

ssh sysadm@$gateway_ip sudo /bin/cp /etc/network/interfaces.$line /etc/network/interfaces
ssh sysadm@$gateway_ip sudo /bin/cp /etc/ppp/peers/dsl-provider.$line /etc/ppp/peers/dsl-provider

echo ""
echo "--"
echo ""
echo ""
reboot=
while [ "X${reboot}" = "X" ]; do
   echononl "Reboot Gateway? [yes/no]: "
   read reboot
   if [ "$reboot" != "yes" -a "$reboot" != "no" ];then
      echo ""
      echo "Please type \"yes\" for rebooting gateway system, \"no\" to abort"
      echo ""
   fi
done

if [ "$reboot" = "yes" ]; then
   ssh sysadm@$gateway_ip sudo /sbin/reboot
   echo -e "\n\tRestarting Gateway \"$company\" with $line line..\n"
else
   echo ""
   echo -e "\tWarning!"
   echo -e "\tThe Gateway System is now configured for \"$line\". Reconecting/Rebooting"
   echo -e "\twill fail, if internet line isn't connected via \"$line\"."
   echo
fi
exit
