Branch oldpackages for 14.07
[14.07/packages.git] / net / uanytun / files / uanytun.init
1 #!/bin/sh /etc/rc.common
2 START=50
3
4 BIN=uanytun
5 DAEMON=/usr/sbin/$BIN
6 DESC=$BIN
7 RUN_D=/var/run
8
9
10 option_cb() {
11   local varname="$1"
12   local value="$2"
13
14   if ! echo "$CONFIG_OPTIONS" | grep " $varname " > /dev/null; then
15     CONFIG_OPTIONS="$CONFIG_OPTIONS $varname "
16   fi
17 }
18
19 foreach_config_forced() {
20   foreach_config $1 "forced"
21 }
22
23 foreach_config() {
24   local cfg="$1"
25   local name
26   local option
27   local value
28   local args=""
29   local forced=0
30
31   if [ -n "$2" ] && [ "x$2" == "xforced" ]; then
32     forced=1
33   fi
34
35   config_get name "$cfg" TYPE
36   for option in $CONFIG_OPTIONS
37   do
38     config_get value "$cfg" "$option"
39     if [ "x$option" == "xdisabled" ]; then
40       if [ $forced -eq 0 ] && [ $value -eq 1 ]; then
41         echo -n " $name(disabled)"
42         return
43       fi
44       continue
45     fi
46
47     option=`echo $option | tr '_' '-'`
48     if [ -n "$value" ]; then
49       args="$args --$option $value"
50     fi
51   done
52   echo -n " $name"
53   local status="OK"
54   $DAEMON --write-pid "$RUN_D/$BIN.$name.pid" $args || status="failed"
55   echo -n "($status)"
56 }
57
58 stop_vpn() {
59   local name=$1
60   local pidfile=$RUN_D/$BIN.$name.pid
61   echo -n " $name"
62   local status="OK"
63   if [ ! -f "$pidfile" ]; then
64     status="tunnel not active"    
65   else
66     kill `cat $pidfile` > /dev/null 2>&1 || status="failed"
67     rm -f $pidfile
68   fi
69   echo -n "($status)"
70 }
71
72 start() {
73   echo -n "Starting $DESC:"
74   config_load $BIN  
75   if [ $# -gt 0 ]; then
76     while [ $# -gt 0 ]; do
77       config_foreach foreach_config_forced "$1"
78       shift
79     done
80   else
81     config_foreach foreach_config ""
82   fi
83   echo "."
84 }
85
86 stop() {
87   echo -n "Stopping $DESC:"
88   local name
89   local pidfile
90
91   if [ $# -gt 0 ]; then
92     while [ $# -gt 0 ]; do
93       stop_vpn $1
94       shift
95     done
96   else
97     for pidfile in `ls $RUN_D/$BIN.*.pid 2> /dev/null`; do
98       name=${pidfile%%.pid}
99       name=${name##$RUN_D/$BIN.}
100       stop_vpn $name
101     done
102   fi
103   echo "."
104 }