#!/sbin/runscript
# Copyright 2008-2012 Calculate Ltd. http://www.calculate-linux.org
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

depend() {
	after calculate
	after net
}

start() {
	ebegin "Start calculate core"

	if ! /usr/sbin/cl-core --check &>/dev/null
	then
		ADMINUSER=`/usr/sbin/cl-core-variables-show --only-value install.cl_migrate_user[0]`
		[[ -n $ADMINUSER ]] || ADMINUSER=root
		LANG=C /usr/sbin/cl-core --bootstrap $ADMINUSER
	fi

	start-stop-daemon --background --start --quiet --pidfile /var/run/cl_core.pid \
		--exec /usr/sbin/cl-core -- --pid-file /var/run/cl_core.pid --start
	for waiting in 0.1 0.2 0.5 1 2 4
	do
		[[ -f /var/run/cl_core.pid ]] && [[ -d /proc/`cat /var/run/cl_core.pid` ]] && break
		sleep $waiting
	done
	[[ -f /var/run/cl_core.pid ]] && [[ -d /proc/`cat /var/run/cl_core.pid` ]]
	eend $?
}

stop(){
	ebegin "Stop calculate core"
	start-stop-daemon -R6 --stop --signal 2 --quiet --pidfile /var/run/cl_core.pid
	start-stop-daemon -R6 --stop --quiet --pidfile /var/run/cl_core.pid
	/usr/bin/pkill -f "cl-core.*--start"
	eend 0
}

# vim:ts=4
