#!/bin/sh
#
# isdn start/stop connect/disconnect script
#
# Be sure you edit /etc/isdn/isdn.conf before using this script!
#
# (c) 1996/97 by Bjarne Pohlers 
#     <bjarne%libelle.uucp%von-neumann@math.uni-muenster.de>
#
# USE THIS FILE AT YOUR OWN RISK!
#

PATH=/usr/sbin:/sbin:/bin:/usr/bin

#the isdn.conf file
ISDNCONF=/etc/isdn/isdn.conf

#Set this to 'yes' if you want to enable call out connections at startup
addoutphone=no

#Set this to 'yes' if you want to remove phonenumbers when shutting down
#the system (normally yes)  
deloutphone=yes

#This really should be 'yes', otherwise the isdn-system won't start up
othercontrol=yes

ctrl()
{
  cmd="$1"
  shift
  if test "$cmd" = addphone -a "$1" = out
  then
    if test "$addoutphone" = yes; then isdnctrl $cmd $interface "$@"
    fi
  else
    if test "$othercontrol" = yes; then isdnctrl $cmd $interface "$@"
    fi
  fi
}


delctrl()
{
  cmd="$1"
  shift
  case "$cmd" in
    addphone )
	       if test "$1" = out; then
		 if test "$deloutphone" = yes; then 
		   echo "isdnctrl delphone $interface $@"
		 fi
	       else
		 if test "$othercontrol" = yes; then
		   echo "isdnctrl delphone $interface $@"
		 fi
	       fi ;;
  esac
}

delroute()
{
  target=""
  while test -n "$1"; do
    case "$1" in
      add ) target=add; shift;;
      -* ) shift; continue;;
      * ) target=$1; break;;
    esac
  done
  test -n "$target" && echo "route del $target"
}
	
check_interface()
{
  if test $# -eq 0; then return 0; fi
  for i in "$@"; do
    if test "$i" = "$interface"; then return 0; fi
  done
  return 1
}  


case "$0" in
  *disconnect ) set stop "$@"
		othercontrol=no
		deloutphone=yes ;;
  *connect ) set start "$@"
	     othercontrol=no
	     addoutphone=yes ;;
esac

case "$1" in
start )
shift
ifaces="$@"

exec <${ISDNCONF}
while read r; do
  eval set -- "$r"
  case "$1" in
    interface ) interface=$2
		test "$othercontrol" = yes && 
		check_interface $ifaces && isdnctrl addif $interface ;;
    ifconfig ) shift
	       test "$othercontrol" = yes && 
	       check_interface $ifaces && ifconfig $interface "$@" ;;
    route ) shift
	    test "$othercontrol" = yes && 
	    check_interface $ifaces && route "$@" $interface ;;
    ctrl ) shift
	   check_interface $ifaces && for i in "$@"; do
	     ctrl $i
	   done ;;
  esac
done
  

;;
  stop )
	 shift
	 ifaces="$@"
	 exec <${ISDNCONF}
	 ifcmd=""
	 ctcmd="true"
	 rocmd="true"
	 ifccmd="true"
	 while read r; do
	   eval set -- "$r"
	   case "$1" in
	     interface ) check_interface $ifaces && 
			 {
			   eval "$rocmd"
			   eval "$ifccmd"
			   eval "$ctcmd"
			   eval "$ifcmd"
			 }
			 interface=$2
			 ctcmd="true"
			 rocmd=""
			 ifccmd="true"
			 check_interface $ifaces && isdnctrl hangup $interface
			 test "$othercontrol" = yes &&
			 ifcmd="isdnctrl delif $interface" ;;
	     ifconfig ) test "$othercontrol" = yes &&
			ifccmd="ifconfig $interface down" ;;
	     route ) shift; tmprocmd=`delroute "$@"` 
		     test "$othercontrol" = yes &&
		     test -n "$tmprocmd" && rocmd="$tmprocmd ; $rocmd"
		     ;;
	     ctrl )
		    shift
		    for i in "$@"; do
		      tmpcmd=`delctrl $i`
		      test -n "$tmpcmd" && ctcmd="$ctcmd ; $tmpcmd"
		    done ;;
	   esac
	 done
	 check_interface $ifaces &&
	 {
	   eval "$rocmd"
	   eval "$ifccmd"
	   eval "$ctcmd"
	   eval "$ifcmd"
	 }
	 ;;
  * )
      echo usage: "$0 [start | stop ] " >&2 ;;
esac
