performs service plc stop before uninstalling
[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.3 2006/04/07 17:12:52 mlhuang Exp $
10 #
11
12 PATH=/sbin:/bin:/usr/bin:/usr/sbin
13
14 # Source function library.
15 . /etc/init.d/functions
16
17 # Source configuration
18 if [ -f /etc/sysconfig/plc -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then
19     . /etc/sysconfig/plc
20 fi
21
22 # Total number of errors
23 ERRORS=0
24
25 # Count the exit status of the last command
26 check ()
27 {
28     ERRORS=$(($ERRORS+$?))
29 }
30
31 mounted ()
32 {
33     if cut -d' ' -f2 /proc/mounts | grep -q "$1" ; then
34         return 0
35     else
36         return 1
37     fi
38 }
39
40 start ()
41 {
42     echo -n $"Mounting PLC: "
43
44     if ! mounted $PLC_ROOT ; then
45         if ! e2fsck -a $PLC_ROOT.img | logger -t "PLC" ; then
46             e2fsck $PLC_ROOT.img
47         fi
48         mount -o loop $PLC_ROOT.img $PLC_ROOT
49         check
50     fi
51     if ! mounted $PLC_ROOT/data ; then
52         mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data
53         check
54     fi
55     if ! mounted $PLC_ROOT/proc ; then
56         mount -t proc none $PLC_ROOT/proc
57         check
58     fi
59
60     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
61     echo
62
63     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $*
64     check
65 }
66
67 stop ()
68 {
69     if mounted $PLC_ROOT ; then
70         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
71         check
72     fi
73
74     echo -n $"Unmounting PLC: "
75
76     for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do
77         if mounted $dir ; then
78             umount $dir
79             check
80         fi
81     done
82
83     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
84     echo
85 }
86
87 # Get command
88 shift $(($OPTIND - 1))
89 command=$1
90
91 # Get step(s)
92 shift 1
93
94 case "$command" in
95     start|stop)
96         $command $*
97         ;;
98
99     restart)
100         stop $*
101         start $*
102         ;;
103
104     *)
105         echo "Usage: $0 {start|stop|restart}"
106         RETVAL=1
107         ;;
108 esac
109
110 exit $ERRORS