#! /bin/bash
# $Id: gxdialup,v 1.2 2003/09/25 21:29:20 trm Exp trm $

## 
## Usage: gxdialup [PROVIDER_LIST]
## 
## A simple interactive ISP connection manager for a machine with
## one or more dial-up accounts. This script could be useful as a
## window manager menu item or a GNOME launcher, for example.
## 
## PROVIDER_LIST is a comma-separated list of provider names and
## screen names. For example, if /etc/ppp/peers/ contains the files
## 'provider', 'clear' and 'telecom', you might invoke gxdialup as:
## 
##     gxdialup "IHUG=provider,ClearNET=clear,XTRA=telecom"
## 
# 2003-09-25 Tim Musson <trmusson@ihug.co.nz>


PROVIDER_LIST="my ISP=provider"


TIMEOUT=40
MESSAGE_FONT="sans 14"
MESSAGE_FG="#e2e2de"
MESSAGE_BG="#446a7e"
GEOMETRY="400x90"


XMESSAGE=gxmessage


TIMESTAMP="$HOME/.gxdialup.tmp"


if [ "$1" = "-h" -o "$1" = "--help" ]; then
  sed -n '/^##/s/^## //p' $0
  exit 0
elif [ "$#" -eq 1 ]; then
  PROVIDER_LIST="$1"
elif [ "$#" -ne 0 ]; then
  echo "Try '`basename $0` --help'" >&2
  exit 64
fi


if [ -e "/var/run/ppp0.pid" ]; then
  connected=1
else
  connected=0
fi


if [ "$connected" -eq 1 ]; then
  if [ -f "$TIMESTAMP" ]; then
    message=`cat "$TIMESTAMP"`
  else
    message="Connected."
  fi
  buttons="Disconnect now:101"
else
  message="Not connected."
  buttons=""
  num=0
  IFS_tmp=$IFS
  IFS=","
  for provider in $PROVIDER_LIST; do
    if [ -n "$buttons" ]; then
      buttons="$buttons,"
    fi
    showname[$num]="${provider%%=*}"
    realname[$num]="${provider##*=}"
    buttons="${buttons}Dial ${showname[$num]}:`expr $num + 102`"
    num=`expr $num + 1`
  done
  IFS=$IFS_tmp
fi
buttons="$buttons,Okay:100"

$XMESSAGE -center                         \
          -geometry "$GEOMETRY"           \
          -title "ISP Connection Status"  \
          -fn "$MESSAGE_FONT"             \
          -fg "$MESSAGE_FG"               \
          -bg "$MESSAGE_BG"               \
          -buttons "$buttons"             \
          -default Okay                   \
          "$message"

response=$?

if [ "$response" -eq 101 ]; then
  poff >/dev/null
elif [ "$response" -ge 102 ]; then
  num=`expr $response - 102`
  if ! pon ${realname[$num]}; then exit 1; fi
  echo "Connected to ${showname[num]} since `date +%T`" > "$TIMESTAMP"
  if [ "$TIMEOUT" -gt 0 ]; then
    $XMESSAGE -center                         \
              -geometry "$GEOMETRY"           \
              -title "ISP Connection Status"  \
              -fn "$MESSAGE_FONT"             \
              -fg "$MESSAGE_FG"               \
              -bg "$MESSAGE_BG"               \
              -timeout $TIMEOUT               \
              -buttons ""                     \
              "Dialing ${showname[$num]}, please wait..." &
  fi
fi

exit 0

