#!/bin/sh

#
# get-ifstatus  (C) 2001 Malte J. Wetz <mail@malte-wetz.de>
#
# Script for retrieving the status of any dialup network interface.
# Result will be printed to stdout. For use with raccess4vbox.
# Optional command line argument "UP" or "DOWN" may be specified
# to change the status of the interface.
#
# To use this script for a specific network interface, simply
# create a symlink named ip-xxx (where xxx is the name of the
# interface) and call that symlink from raccess4vbox.
# Therefore, you will only need to adopt this script to your
# system.
#
# Warning: Currently, only ISDN interfaces (ippp?) are
#          supported.
#
# Example:
#     earth:/usr/share/isdn/vbox/bin # ln -s getip ip-ippp0
#     earth:/usr/share/isdn/vbox/bin # ip-ippp0
#     2 & 1 & 2 & dot & 1 & 1 & 3 & dot & 5 & dot & 1 & 0
#     earth:/usr/share/isdn/vbox/bin # _
#


################################################################
# SETTINGS: Change these parameters to meet your requirements.
################################################################

# Full path of ifconfig-binary. Use "which ifconfig" if you
# don't know it yourself.
IFCONFIG=/sbin/ifconfig

# Full path of isdnctrl-binary. Use "which isdnctrl" if you
# don't know it yourself.
ISDNCTRL=/usr/sbin/isdnctrl

# Full path of grep-binary. Use "which grep" if you
# don't know it yourself.
GREP=/usr/bin/grep

# Full path of cut-binary. Use "which cut" if you
# don't know it yourself.
CUT=/usr/bin/cut

# Full path of awk-binary. Use "which awk" if you
# don't know it yourself.
AWK=/usr/bin/awk

# What string do you want to be printed for the interface
# status (connected or not connected)? Change these values to
# adopt to your language.
#
# Uncomment these lines for german:
#CONNECTED="ist & verbunden"
#NOTCONNECTED="ist & nicht & verbunden"
#DIAL="whlt"
#DIALFAIL="kann & nicht & whlen"
#NOIFACE="Interface & nicht & verfgbar"
# Uncomment these lines for english:
CONNECTED="is & connected"
NOTCONNECTED="is & not & connected"
DIAL="dialing"
DIALFAIL="can & not & dial"
NOIFACE="Interface & does & not & exist"

################################################################
# END SETTINGS: There should be nothing to change below this
# point.
################################################################


# Get basename and parse the interface name from it.
BASE=${0##*/}
LINK=${BASE#*ifstatus-}

# Get action, if specified
test -z "$1" || case "$1" in
  UP)	ACTION=UP	;;
  DOWN)	ACTION=DOWN	;;
  *)
    echo "Unkown action. Usage: $BASE [UP|DOWN]"
    exit 1
  ;;
esac

# Check if interface name was correctly retrieved.
if [ -z "$LINK" ] || [ "$LINK" = "$BASE" ] ; then
  cat << EOF >&2

  Error. Do not call this script directly. Instead, create a symlink named
  ip-xxx pointing to $BASE, where xxx is the name of the network interface
  you want to get the IP from. See comments in $BASE for details.

EOF
  exit 1
fi

# Check if interface name is valid and supported
case "$LINK" in
  ippp?) IF=$LINK	;;
  *)
    echo "$NOIFACE: $LINK"
    exit 1
  ;;
esac

IFTEXT="ippp & ${IF#*ippp}"


# Check if interface exists
$IFCONFIG $IF >/dev/null 2>&1 || {
  echo "$NOIFACE: $LINK"
  exit 1
}

if [ -z "$ACTION" ]; then
  STAT=`$ISDNCTRL status $IF | $CUT -d ' ' -f 4`
  if [ "$STAT" != "connected" ]; then
    echo "$IFTEXT & $CONNECTED"
    # If a ip-xxx link to the get-ip script exists, use that for
    # getting the ip adress. Otherwise, do nothing.
    iplink=`dirname $0`"/ip-$IF"
    test -x "$iplink" && eval $iplink
  else
    echo "$IFTEXT & $NOTCONNECTED"
  fi
else
  case "$ACTION" in
    UP)
      $ISDNCTRL dial $IF >/dev/null 2>&1 || {
        echo "$IFTEXT & $DIALFAIL"
        exit 1
      }
      echo "$IFTEXT & $DIAL"
    ;;
    DOWN)
      $ISDNCTRL hangup $IF >/dev/null 2>&1 || {
        echo "$IFTEXT & $HUPFAIL"
        exit 1
      }
      echo "$IFTEXT & $HUP"
    ;;
  esac
fi

exit 0
