#!/bin/sh

# $Id: poff,v 1.4 1998/05/26 14:46:08 phil Exp $
# Written by Phil Hands <phil@hands.com>, distributed under the GNU GPL

SIG=TERM DONE=stopped;

getopts rdch FLAG
case $FLAG in
 "r") SIG=HUP  DONE=signalled; shift  ;;
 "d") SIG=USR1 DONE=signalled; shift ;;
 "c") SIG=USR2 DONE=signalled; shift ;;
 "h") cat <<!EOF!
usage: $0 [options] [provider]

options:
  -r        cause pppd to drop the line and redial
  -d        toggles the state of pppd's debug option
  -c        cause pppd to renegotiate compression
  -h        this help summary
!EOF!
    exit 1
    ;;
esac

PROVIDER=$1


# Lets see how many pppds are running....
set -- `cat /var/run/ppp*.pid 2>/dev/null`

case $# in
  0) # pppd only creates a pid file once ppp is up, so let's try killing pppd
     # on the assumption that we've not got that far yet.
     kill -${SIG} `ps axw | egrep "pppd call [[:alnum:]]+" | grep -v grep | awk '{print $1}'`
     exit 0
     ;;
  1) # If only one was running then it can be killed using the pid
     kill -${SIG} $1
     exit 0
     ;;
  *) # More than one! Aieehh.. We have to use ps to figure it out.
     # If no arguments were specified, then assume the 'provider' default.
     PID=`ps axw | egrep "pppd call ${PROVIDER:-provider}[[:space:]]*\$" | grep -v grep | awk '{print $1}'`
     if [ $PID ]; then
        kill -${SIG} ${PID}
        exit 0
     else
        echo "I could not find a pppd process or provider '${PROVIDER:-provider}'. None ${DONE}"
        exit 1
     fi
     ;;
esac
