summaryrefslogtreecommitdiffstats
path: root/abs/core/zoneminder/zm.rc.d
blob: 60f684b0aa92bf6ecc62d2dccd48eb459f79a675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash

# Source function library.
. /etc/rc.conf
. /etc/rc.d/functions

prog=ZoneMinder
ZM_VERSION="1.24.0"
ZM_PATH_BIN="/usr/lib/zm/bin"
ZM_CONFIG="/etc/zm.conf"
command="$ZM_PATH_BIN/zmpkg.pl"

loadconf()
{
	if [ -f $ZM_CONFIG ]; then
		. $ZM_CONFIG
	else
		echo "ERROR: $ZM_CONFIG not found.
" 
		return 1
	fi
}

# Check for old config and update if needed
checkcfg()
{
	# Check config
	if [ "$ZM_DB_HOST" = "" -o "$ZM_DB_NAME" = "" -o "$ZM_DB_USER" = "" -o "$ZM_DB_PASS" = "" ]; then
			echo "In $ZM_CONFIG there should not be null values.
"
			return 1
	loadconf
	fi
}

checkdb()
{
	# Check database exisits and version
	GetVer="select Value from Config where Name='ZM_DYN_DB_VERSION'"
	OLD_VERSION=`echo $GetVer | mysql -B -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME | grep -v '^Value'`
	if [ "$OLD_VERSION" = "" ]; then
		GetVer="select Value from Config where Name='ZM_DYN_CURR_VERSION'"
		OLD_VERSION=`echo $GetVer | mysql -B -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME | grep -v '^Value'`
		if [ "$OLD_VERSION" = "" ]; then
			echo "You must run $ZM_PATH_BIN/zminit manually
"
			return 1
		fi	
	fi
	RETVAL=$?
	
	if [ $RETVAL != 0 ]; then
		tbls=`mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS -s -e 'show tables' $ZM_DB_NAME`
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			echo "Initialize $prog database:
"
			echo tbls | grep Config >/dev/null 2>&1
			RETVAL=$?
			if [ $RETVAL != 0 ]; then
				mysql -B -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME < $ZM_PATH_UPDATE/zm_create.sql 
				RETVAL=$?
				[ $RETVAL = 0 ] && stat_done
				[ $RETVAL != 0 ] && stat_fail
				echo
				return $RETVAL
			fi
			( cd $ZM_PATH_UPDATE; perl $ZM_PATH_BIN/zmupdate.pl -f )
			RETVAL=$?
			[ $RETVAL = 0 ] && stat_done
			[ $RETVAL != 0 ] && stat_fail
			echo
			return $RETVAL
		else
			echo "No access to ZoneMinder database. 
Run $ZM_PATH_BIN/zminit manually.
"
			return $RETVAL
		fi
	else
		[ "$ZM_VERSION" = "$OLD_VERSION" ] && return 0
		echo "Upgrade %s database: " "$prog"
		$ZM_PATH_BIN/zmupdate.pl --version=$OLD_VERSION --noi && ( cd $ZM_PATH_UPDATE; perl $ZM_PATH_BIN/zmupdate.pl -f )
		RETVAL=$?
		[ $RETVAL = 0 ] && stat_done
		[ $RETVAL != 0 ] && stat_fail
		echo
		return $RETVAL
	fi
}

start()
{
	loadconf || return $?
	checkcfg || return $?
	checkdb || return $?
	stat_busy "Starting $prog"
	[ -d /var/run/zm ] || mkdir -m 774 /var/run/zm
	chown -R $ZM_WEB_USER:$ZM_WEB_GROUP /var/run/zm
	GetPath="select Value from Config where Name='ZM_PATH_SOCKS'"
	ZM_PATH_SOCK=`echo $GetPath | mysql -B -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME | grep -v '^Value'`
	[ -d $ZM_PATH_SOCK ] || mkdir -m 774 -p $ZM_PATH_SOCK 
	sudo -u  $ZM_WEB_USER test -O  $ZM_PATH_SOCK \
	|| chown -R $ZM_WEB_USER:$ZM_WEB_GROUP $ZM_PATH_SOCK
	# Setup the log dir
	[ -d /var/log/zm ] || mkdir /var/log/zm
	for logs in zmaudit.log  zmdc.log  zmfilter.log  zmpkg.log  zmupdate.log  zmwatch.log; do
	touch /var/log/zm/$logs
	done
	chown -R $ZM_WEB_USER:$ZM_WEB_GROUP  /var/log/zm
	$command start
	RETVAL=$?
	[ $RETVAL = 0 ] && stat_done
	[ $RETVAL != 0 ] && stat_fail
	#[ $RETVAL = 0 ] && touch /var/lock/subsys/zm
	return $RETVAL
}

stop()
{
	stat_busy "Stopping $prog"
	$command stop
	RETVAL=$?
	[ $RETVAL = 0 ] && stat_done
	[ $RETVAL != 0 ] && stat_fail
	#[ $RETVAL = 0 ] && rm -f /var/lock/subsys/zm
}

status()
{
	result=`$command status`
	if [ "$result" = "running" ]; then
		echo "ZoneMinder is running
"
		RETVAL=0
	else
		echo "ZoneMinder is stopped
"
		RETVAL=1
	fi
}

case "$1" in
	'start')
		start
		;;
	'stop')
		stop
		;;
	'restart')
		stop
		start
		;;
	'condrestart')
		loadconf
		checkcfg
		result=`$ZM_PATH_BIN/zmdc.pl check`
		if [ "$result" = "running" ]; then
			$ZM_PATH_BIN/zmdc.pl shutdown > /dev/null
			start
		fi
		;;
	'status')
		status
		;;
	*)
		echo "Usage: zm { start | stop | restart | condrestart | status }
"
		RETVAL=1
		;;
esac
exit $RETVAL