Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / examples / isdn / contrib / isdnctl
1 #!/bin/sh
2
3 # isdnctl
4 # Control the ISDN line based on usage
5 #
6 # This script can control the state of your ISDN line.  It counts
7 # how many scripts/users currently use the ISDN line and uses
8 # "ifconfig down" if noone uses it any more.
9 # I use this script for cronjobs that fetch mail and news and run cvsup.
10 # If I'm still using the line, the script won't close the connection,
11 # but if not, it saves a lot of phone costs.
12 #
13 # ----------------------------------------------------------------------------
14 # "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
15 # Alexander Langer <alex@big.endian.de> wrote this file.  As long as you retain
16 # this notice you can do whatever you want with this stuff. If we meet some
17 # day, and you think this stuff is worth it, you can buy me a beer in return.
18 #
19 # Alexander Langer
20 # ----------------------------------------------------------------------------
21 #
22 # $FreeBSD: src/share/examples/isdn/contrib/isdnctl,v 1.1.2.1 2001/08/10 14:59:48 obrien Exp $
23 #
24
25
26 usage () {
27         echo "Usage: $0 [-i interface] [-f /path/to/users.file] [up|down|show]"
28 }
29
30 # Defaults
31 INTERFACE=isp0
32 USERSFILE=
33
34 # Getopt stuff
35 args=`getopt i:f: $*`
36 if [ $? != 0 ]; then
37                 usage
38                 exit 2
39         fi
40 set -- $args
41 for i; do
42         case "$i" in
43                 -i)
44                         INTERFACE="$2"
45                         shift; shift
46                         ;;
47                 -f)
48                         USERSFILE="$2"
49                         shift; shift
50                         ;;
51                 --)
52                         shift
53                         break
54                         ;;
55         esac
56 done
57
58 if [ -z $USERSFILE ]; then
59         USERSFILE=/var/run/isdn.users.$INTERFACE
60 fi
61
62 if [ -z $1 ]; then
63         usage
64         exit 2
65 fi
66
67 # Does Usersfile exist?
68 if [ ! -f $USERSFILE ]; then
69         # Try to create it
70         if ! /sbin/ifconfig $INTERFACE | grep "^$INTERFACE.*<UP," > /dev/null; then
71                 echo "0" > $USERSFILE || exit 1
72         else
73                 echo "1" > $USERSFILE || exit 1
74         fi
75 elif [ ! -w $USERSFILE ]; then
76         echo "Error: $USERSFILE not writeable!"
77         exit 1
78 fi
79
80 if ! /sbin/ifconfig $INTERFACE | grep "^$INTERFACE.*<UP," > /dev/null; then
81         if ! cat $USERSFILE | grep '^0$' > /dev/null ; then 
82                 echo "Interface down, removing number from file";
83                 echo "0" > $USERSFILE
84         fi;
85 fi;
86
87 case "$1" in
88                 show)
89                         echo "`cat $USERSFILE` users online"
90                         ;;
91                 up)
92                         expr `cat $USERSFILE` + 1 > $USERSFILE
93                         /sbin/ifconfig $INTERFACE up
94                         echo "`cat $USERSFILE` users online"
95                         ;;
96                 down)
97                         if cat $USERSFILE | grep '^0$' > /dev/null ; then 
98                                 echo "Already down"
99                                 exit 0
100                         fi
101                         expr `cat $USERSFILE` - 1 > $USERSFILE
102                         if cat $USERSFILE | grep '^0$' > /dev/null ; then 
103                                 echo "`cat $USERSFILE` users online, interface down"
104                                 /sbin/ifconfig $INTERFACE down
105                                 exit 0
106                         fi
107                         echo "`cat $USERSFILE` users online"
108                         ;;
109 esac
110
111 exit 0