#!/bin/sh
#
# License: Copyright 2007 SpinetiX S.A. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Starting/stopping SpinetiX's Bonsai content player administrative interface
# Description: Starting/stopping SpinetiX's Bonsai content player administrative interface
### END INIT INFO
# NOTE: this should be started before apache and stopped after apache
# chkconfig: 235 19 81

# Init script information
INIT_NAME=raperca-admin
DESC="Bonsai admin interface"

# Load init script configuration
[ -f /etc/default/$INIT_NAME ] && . /etc/default/$INIT_NAME

# Source the init script functions
. /etc/init.d/init-functions

# Set up the path variables
. /etc/raperca/init-functions
init_dirs "$2"

# Each init script action is defined below...

start() {
	local RET ERROR=

	log_status_msg "Starting $DESC: " -n

	log_status_msg "shadow dir" -n
	ERROR=
	mount_shadow_dir
	RET=$?
	if [ $RET -eq 0 ]; then
		log_success_msg ", " -n
	else
		log_failure_msg " failed ($RET: $ERROR), " -n
	fi

	log_status_msg "content dir" -n
	if [ ! -d "$SHADOWCONTENTDIR" ]; then
	    copy_default_content "$SHADOWCONTENTDIR"
	fi
	log_status_msg ", " -n

	log_status_msg "clean dav tmp" -n
	clean_dav_tmp "$CONTENTDIR"
	log_status_msg ", " -n

	log_status_msg "empty boot-id" -n
	: > /var/cache/raperca/www/docs/boot-id
	log_status_msg ", " -n

	log_status_msg "clean dav locks" -n
	clean_davlockdb
	log_status_msg ", " -n

	log_status_msg "clean tmp content" -n
	[ -d "$TMPCONTENTDIR" ] && clean "$TMPCONTENTDIR"
	[ -d "$SHADOWTMPCONTENTDIR" ] && clean "$SHADOWTMPCONTENTDIR"
	log_status_msg "."

	if [ -e "$SPOOLDIR"/run-update-jobs ]; then
	    php-cgi -f /usr/share/raperca/www/protected/utils/UpdateJobs.php
	    rm "$SPOOLDIR"/run-update-jobs
	fi

	log_status_msg ""
	return $RET
}

stop () {
	local RET ERROR=
	local RETF

	log_status_msg "Stopping $DESC: " -n
	RETF=0

	# Nothing to do

	log_status_msg ""
	return $RETF
}

restart() {
	local RET

	log_status_msg "Restarting $DESC..."
	stop
	sleep 2
	start
	RET=$?

	return $RET
}

tryrestart() {
	local RET

	pidstatus $BASENAME1
	RET=$?
	if [ $RET -eq 0 ]; then
		restart
		RET=$?
	else
		RET=7
	fi

	return $RET
}

#
# if the service does not support reload return code 3 should
# be the result...
#
reload() {
	local RET

	log_status_msg "Reloading $DESC configuration..." -n
	# killproc $BASENAME1 -HUP
	#
	# repeat as necessary...
	#
	log_success_msg "done."

	# No support for reload
	return 3
}

forcereload() {
	local RET

	reload
	RET=$?
	if [ $RET -ne 0 ]; then
		restart
		RET=$?
	fi
	
	return $RET
}

status() {
	local RET
	
	printstatus $BASENAME1
	RET=$?

	return $RET
}

#
# Everything after this should be the same for all init scripts
#
# See the policy manual for information on actions and return codes.
#

parse() {
	case "$1" in
  start)
			start
			return $?
	;;
  stop)
			stop
			return $?
	;;
		restart)
			restart
			return $?
			;;
		try-restart)
			tryrestart
			return $?
			;;
		reload)
			reload
			return $?
			;;
		force-reload)
			forcereload
			return $?
			;;
		status)
			status
			return $?
	;;
  *)
			echo "Usage: $INIT_NAME " \
			"{start|stop|restart|try-restart|reload|" \
			"force-reload|status}" >&2
	;;
	esac
	
	return 1
}

parse $@



