#!/bin/sh
# Filename:      grml-pptp-xdsl-students
# Purpose:       connect via pptp to inode ["students-setup"] (www.inode.at)
# Authors:       grml-team (grml.org), (c) Martin Hecher <hecka@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
# Latest change: Sam Jän 26 00:13:11 CET 2008 [mika]
################################################################################

################################################################################
# Notes:
# This script is based on 'knoppix-pptp' (version 0.3.1)
# by Wolfgang Scheicher
# and based on grml-pptp-inode
# by Andreas 'Jimmy' Gredler
################################################################################

export LANG=C
DIALOG="dialog"

if [ "$UID" != 0 ];  then
  sudo $0
  exit
fi

setroute()
{
set $(grep "option routers" /var/lib/dhcp3/dhclient.leases | tail -1 | cut -f5 -d' ' | tr -d ";")

ROUTER="$1"
ANT="10.0.0.138"

if route -n | grep $ANT >>/dev/null
  then
    /sbin/route del $ANT
  fi

/sbin/route add $ANT gw $ROUTER

if ifconfig | grep ppp0 >>/dev/null
   then
      set $(ifconfig ppp0 | grep P-t-P | tr -s [:blank:] | cut -f4 -d' ' | cut -b7-)
      GATEWAY="$1"
      /sbin/route del default
      /sbin/route add default gw $GATEWAY
      exit 0
   fi
}

runit(){

/sbin/dhclient ${NET_DEVICE}

echo "name ${VPNUSERNAME}" > /etc/ppp/peers/inode
cat >> /etc/ppp/peers/inode << "EOF"
remotename XDSL
defaultroute
noipdefault
noauth
persist
logfile /var/log/pptp.log

EOF

echo "${VPNUSERNAME} XDSL ${VPNPASSWORD} *" >> /etc/ppp/pap-secrets
chmod 0600 /etc/ppp/pap-secrets

echo -e "#!/bin/sh\nLANG=C\nVPNSERVER=\"${VPNSERVER}\"\n" > /etc/init.d/pptp

cat >> /etc/init.d/pptp << "EOF"

PPTP="/usr/sbin/pptp $VPNSERVER call inode"

case "$1" in
  start)
      echo "Starting up XDSL: pptp"
      start-stop-daemon --start --exec $PPTP
      if [ $? == 0 ]; then
      	echo "."
      fi
    ;;
  stop)
      echo "Shutting down XDSL: pptp"
      start-stop-daemon --stop --exec $PPTP
      if [ $? == 0 ]; then
        /sbin/route del -host $VPNSERVER
        /sbin/route add default gw $GATEWAY
        echo "."
      fi
    ;;
  reconnect)
    echo -n "Reconnecting XDSL: pptd"
    start-stop-daemon --start --exec $PPTP
    echo "."
    ;;
  *)
      echo "Usage: /etc/init.d/pptp {start|stop|reconnect}"
      exit 1
    ;;
esac

exit 0
EOF

chmod +x /etc/init.d/pptp
touch /var/log/pptp.log
/etc/init.d/pptp start
$DIALOG --title "Information" --no-cancel --msgbox "Please click OK and wait until remote IP is assigned." 0 0
$DIALOG --title "PPTP Log" --no-cancel --tailbox /var/log/pptp.log 0 0
}

[ -n "$VPNSERVER"   ] || VPNSERVER="10.0.0.138"
[ -n "$VPNUSERNAME" ] || VPNUSERNAME="$(cat /etc/ppp/pap-secrets | grep XDSL | cut -d " " -f1)"
[ -n "$VPNPASSWORD" ] || VPNPASSWORD="$(cat /etc/ppp/pap-secrets | grep XDSL | cut -d " " -f3)"

if [ -z "$VPNUSERNAME" ] || [ -z "$VPNPASSWORD" ] ; then

  COMMAND1=$($DIALOG --stdout --title "Inode XDSL Graz" --inputbox \
  "Account name (e.g. grml@tug):" 0 35) || exit 0
  COMMAND2=$($DIALOG --stdout --title "Inode XDSL Graz" --passwordbox "Account password (hidden typing):" 0 40) || exit 0

  TMP=$(mktemp)

  bailout(){
  rm -f "$TMP"
  exit $1
  }

  NETDEVICES="$(cat /proc/net/dev | awk -F: '/[0-9]:/{print $1}')"

  wireless(){
   case "$(cat /proc/net/wireless 2>/dev/null)" in *$1*) return 0;; esac
   # Card exists but is not configured yet
   [ -n "$(iwconfig $1 2>/dev/null | head -1)" ] && return 0
   return 1
  }

  if [ -z "$NETDEVICES" ]; then
    $DIALOG --msgbox "No network devices found." 15 45
    bailout
  fi

  count="$(echo "$NETDEVICES" | wc -w)"

  if [ "$count" -gt 1 ]; then
    DEVICELIST=""
    for DEVICE in $NETDEVICES; do
      wireless "$DEVICE" && DEVICELIST="$DEVICELIST ${DEVICE} Wireless" || DEVICELIST="$DEVICELIST ${DEVICE} LAN"
    done
    rm -f "$TMP"
    $DIALOG --menu "Network device to connect from:" 18 45 12 $DEVICELIST 2>"$TMP" || bailout
    read NET_DEVICE <"$TMP" ; rm -f "$TMP"
    else
    # Remove additional spaces
    NET_DEVICE="$(echo $NETDEVICES)"
  fi

  VPNUSERNAME=${COMMAND1%/*}
  VPNPASSWORD=${COMMAND2#*/}
  [ ! -z "$VPNUSERNAME" ] || exit 1
  [ ! -z "$VPNPASSWORD" ] || exit 1
  [ ! -z "$NET_DEVICE" ] || exit 1
  runit
  setroute
else
  $DIALOG --title "Inode XDSL Graz" --msgbox "Found already configured
  account.\nIf the settings are wrong, delete the entries in
  /etc/ppp/pap-secrets" 10 45
  runit
fi

## END OF FILE #################################################################
