Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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 # $DragonFly: src/share/examples/isdn/contrib/isdnctl,v 1.2 2003/06/17 04:36:57 dillon Exp $
24 #
25
26
27 usage () {
28         echo "Usage: $0 [-i interface] [-f /path/to/users.file] [up|down|show]"
29 }
30
31 # Defaults
32 INTERFACE=isp0
33 USERSFILE=
34
35 # Getopt stuff
36 args=`getopt i:f: $*`
37 if [ $? != 0 ]; then
38                 usage
39                 exit 2
40         fi
41 set -- $args
42 for i; do
43         case "$i" in
44                 -i)
45                         INTERFACE="$2"
46                         shift; shift
47                         ;;
48                 -f)
49                         USERSFILE="$2"
50                         shift; shift
51                         ;;
52                 --)
53                         shift
54                         break
55                         ;;
56         esac
57 done
58
59 if [ -z $USERSFILE ]; then
60         USERSFILE=/var/run/isdn.users.$INTERFACE
61 fi
62
63 if [ -z $1 ]; then
64         usage
65         exit 2
66 fi
67
68 # Does Usersfile exist?
69 if [ ! -f $USERSFILE ]; then
70         # Try to create it
71         if ! /sbin/ifconfig $INTERFACE | grep "^$INTERFACE.*<UP," > /dev/null; then
72                 echo "0" > $USERSFILE || exit 1
73         else
74                 echo "1" > $USERSFILE || exit 1
75         fi
76 elif [ ! -w $USERSFILE ]; then
77         echo "Error: $USERSFILE not writeable!"
78         exit 1
79 fi
80
81 if ! /sbin/ifconfig $INTERFACE | grep "^$INTERFACE.*<UP," > /dev/null; then
82         if ! cat $USERSFILE | grep '^0$' > /dev/null ; then 
83                 echo "Interface down, removing number from file";
84                 echo "0" > $USERSFILE
85         fi;
86 fi;
87
88 case "$1" in
89                 show)
90                         echo "`cat $USERSFILE` users online"
91                         ;;
92                 up)
93                         expr `cat $USERSFILE` + 1 > $USERSFILE
94                         /sbin/ifconfig $INTERFACE up
95                         echo "`cat $USERSFILE` users online"
96                         ;;
97                 down)
98                         if cat $USERSFILE | grep '^0$' > /dev/null ; then 
99                                 echo "Already down"
100                                 exit 0
101                         fi
102                         expr `cat $USERSFILE` - 1 > $USERSFILE
103                         if cat $USERSFILE | grep '^0$' > /dev/null ; then 
104                                 echo "`cat $USERSFILE` users online, interface down"
105                                 /sbin/ifconfig $INTERFACE down
106                                 exit 0
107                         fi
108                         echo "`cat $USERSFILE` users online"
109                         ;;
110 esac
111
112 exit 0