4619e4dfd87e401c193c5a5aa719cea7dc104bbe
[dragonfly.git] / sbin / rcrun / rcrun.sh
1 #!/bin/sh
2 #
3 # rcng command
4 #
5 # $DragonFly: src/sbin/rcrun/rcrun.sh,v 1.9 2008/07/21 23:42:02 swildner Exp $
6
7 dostart()
8 {
9     arg=$1
10     shift
11
12     for i in $@; do
13         case X`varsym -s -q rcng_$i` in
14         Xrunning*)
15             echo "$i has already been started"
16             ;;
17         Xconfigured*)
18             echo "$i has already been configured"
19             ;;
20         *)
21             _return=0
22             for j in `rcorder -o $i /etc/rc.d/*`; do
23                 need=1
24                 for k in `rcorder -p $j`; do
25                     if [ $k = $i ]; then
26                         need=0
27                     else
28                         state=`varsym -s -q rcng_$k`
29                         case X$state in
30                         Xrunning*|Xconfigured*|Xirrelevant*|Xdisabled*)
31                             ;;
32                         *)
33                             echo "$i depends on $k, current state: $state"
34                             _return=1
35                             ;;
36                         esac
37                     fi
38                 done
39             done
40             # $j contains the last dependency, which we run
41             #
42             if [ X$j = X ]; then
43                 echo "Unable to find keyword $i"
44             elif [ $_return = 0 ]; then
45                 echo "Running $j $arg"
46                 (cd /etc/rc.d; sh $j $arg)
47                 case X`varsym -s -q rcng_$i` in
48                 Xdisabled*)
49                     echo "$i is disabled, enable in rc.conf first or use rcforce"
50                     ;;
51                 Xfailed*)
52                     echo "$i has failed to start"
53                     ;;
54                         
55                 esac
56             fi
57             ;;
58         esac
59     done
60 }
61
62 arg=$0
63 case ${0##*/} in
64 rcstart)
65     arg=start
66     ;;
67 rcstop)
68     arg=stop
69     ;;
70 rcrestart)
71     arg=restart
72     ;;
73 rcvar)
74     arg=rcvar
75     ;;
76 rcvars)
77     arg=rcvar
78     ;;
79 rclist)
80     arg=list
81     ;;
82 rcforce)
83     arg=forcestart
84     ;;
85 rcfast)
86     arg=faststart
87     ;;
88 rcenable)
89     arg=enable
90     ;;
91 rcdisable)
92     arg=disable
93     ;;
94 *)
95     arg=$1
96     shift
97     ;;
98 esac
99
100 case $arg in
101 start)
102         dostart start $@
103         ;;
104 forcestart)
105         dostart forcestart $@
106         ;;
107 faststart)
108         dostart faststart $@
109         ;;
110 stop)
111         for i in $@; do
112             j=`rcorder -o $i /etc/rc.d/* | tail -1`
113             if [ X$j = X ]; then
114                 echo "Unable to find keyword $i"
115             else
116                 (cd /etc/rc.d; sh $j stop)
117             fi
118         done
119         ;;
120 restart)
121         for i in $@; do
122             j=`rcorder -o $i /etc/rc.d/* | tail -1`
123             if [ X$j = X ]; then
124                 echo "Unable to find keyword $i"
125             else
126                 (cd /etc/rc.d; sh $j restart)
127             fi
128         done
129         ;;
130 disable|enable)
131         if [ "$arg" = "enable" ]; then
132             mode=YES
133         else
134             mode=NO
135         fi
136         for i in $@; do
137             j=`rcorder -o $i /etc/rc.d/* | tail -1`
138             if [ X$j = X ]; then
139                 echo "Unable to find provider id $i"
140             elif [ `varsym -s -q rcng_$i` = "$mode" ]; then
141                 echo "$i is already $mode"
142             else
143                 vars=`(cd /etc/rc.d; sh $j rcvar) 2>/dev/null | egrep '_enable' | sed -e 's/\\$//g' | sed -e 's/=.*//g'`
144                 cp /etc/rc.conf /etc/rc.conf.bak
145                 for k in $vars; do
146                     rm -f /etc/rc.conf.$$
147                     ( egrep -v "#rcrun ${k}" /etc/rc.conf; printf "${k}=${mode}\t#rcrun ${k}_enable\n" ) > /etc/rc.conf.$$
148                     mv -f /etc/rc.conf.$$ /etc/rc.conf
149                     echo "added/modified: ${k}=${mode}"
150                 done
151                 if [ $arg = enable ]; then
152                     rcstart $i
153                 else
154                     rcstop $i
155                 fi
156             fi
157         done
158         ;;
159 rcvar)
160         for i in $@; do
161             j=`rcorder -o $i /etc/rc.d/* | tail -1`
162             if [ X$j = X ]; then
163                 echo "Unable to find provider id $i"
164             else
165                 (cd /etc/rc.d; sh $j rcvar)
166             fi
167         done
168         ;;
169 list)
170         if [ "X$*" = X ]; then
171             for i in `varsym -a -s | egrep '^rcng_'`; do
172                 echo $i
173             done
174         else
175             for i in $@; do
176                 varsym -s rcng_$i 2>/dev/null || varsym -s rcng_$i
177             done
178         fi
179         ;;
180 *)
181         echo "usage: rcrun start|stop|restart|rcvar|list|forcestart|faststart|disable|enable"
182         echo "             script ..."
183         ;;
184 esac
185