- fix version number, bump release number, added changelog
[myplc.git] / host.init
1 #!/bin/bash
2 #
3 # plc   Manages all PLC services on this machine
4 #
5 # chkconfig: 2345 99 5
6 #
7 # description:  Manages all PLC services on this machine
8 #
9 # $Id: host.init,v 1.9 2006/07/17 21:30:33 mlhuang Exp $
10 #
11
12 PATH=/sbin:/bin:/usr/bin:/usr/sbin
13
14 # Source function library.
15 if [ -f /etc/init.d/functions ] ; then
16     . /etc/init.d/functions
17 fi
18
19 # If success() or failure() are not defined
20 if ! type -type success >/dev/null || ! type -type failure >/dev/null ; then
21     success() {
22         echo -ne "[  OK  ]\r"
23         return 0
24     }
25     failure() {
26         echo -ne "[FAILED]\r"
27         return 1
28     }
29 fi
30
31 # Source configuration
32 SERVICE=$(basename $0)
33 if [ ! -f /etc/sysconfig/$SERVICE ] ; then
34     SERVICE=plc
35 fi
36 if [ -f /etc/sysconfig/$SERVICE -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then
37     . /etc/sysconfig/$SERVICE
38 fi
39
40 # Total number of errors
41 ERRORS=0
42
43 # Count the exit status of the last command
44 check ()
45 {
46     ERRORS=$(($ERRORS+$?))
47 }
48
49 mounted ()
50 {
51     if cut -d' ' -f2 /proc/mounts | grep -q "$1" ; then
52         return 0
53     else
54         return 1
55     fi
56 }
57
58 mount_plc ()
59 {
60     echo -n $"Mounting PLC: "
61
62     if ! mounted $PLC_ROOT ; then
63         if ! e2fsck -a $PLC_ROOT.img | logger -t "PLC" ; then
64             e2fsck $PLC_ROOT.img
65         fi
66         mount -o loop $PLC_ROOT.img $PLC_ROOT
67         check
68     fi
69     if ! mounted $PLC_ROOT/data ; then
70         mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data
71         check
72     fi
73     if ! mounted $PLC_ROOT/proc ; then
74         mount -t proc none $PLC_ROOT/proc
75         check
76     fi
77
78     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
79     echo
80
81 }
82
83 start ()
84 {
85     # Starting everything
86     if [ -z "$1" ] ; then
87         mount_plc 
88     fi
89
90     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $*
91     check
92 }
93
94 umount_plc ()
95 {
96     echo -n $"Unmounting PLC: "
97
98     for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do
99         if mounted $dir ; then
100             umount $dir
101             check
102         fi
103     done
104
105     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
106     echo
107 }
108
109 stop ()
110 {
111     if mounted $PLC_ROOT ; then
112         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
113         check
114     fi
115
116     # Stopped everything
117     if [ -z "$1" ] ; then
118         umount_plc
119     fi
120 }
121
122 mountstatus_plc ()
123 {
124     for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do
125         if mounted $dir ; then
126             echo $dir
127         fi
128     done
129 }
130
131 # Get command
132 shift $(($OPTIND - 1))
133 command=$1
134
135 # Get step(s)
136 shift 1
137
138 case "$command" in
139     start|stop)
140         $command $*
141         ;;
142
143     restart)
144         stop $*
145         start $*
146         ;;
147
148     reload)
149         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS reload $*
150         ;;      
151
152     mount|umount|mountstatus)
153         ${command}_plc $*
154         ;;
155   
156     *)
157         echo "Usage: $0 {start|stop|restart|reload|mount|umount|mountstatus}"
158         RETVAL=1
159         ;;
160 esac
161
162 exit $ERRORS