#!/bin/sh
# @package      hubzero-hubgraph
# @file         hubgraph-server.init
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2014 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2014 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

#
# hubgraph-server HUBzero Search Engine
#
# chkconfig:   2345 85 15
# description: hubgraph-server is the search engine for the HUBzero CMS
#

### BEGIN INIT INFO
# Provides:          hubgraph-server
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: HUBzero Search Engine
# Description:       Controls execution of the HUBzero HubGraph search engine
### END INIT INFO

PATH=/usr/sbin:/usr/bin:/bin:/sbin
DESC="HUBzero graph search engine"
NAME=hubgraph-server
DAEMON=/usr/bin/hubgraph-monitor
DAEMON_ARGS=""
SCRIPTNAME=/etc/init.d/$NAME
LOGDIR=/var/log/hubgraph
RUNDIR=/var/run/hubgraph
PIDFILE=$RUNDIR/$NAME.pid
HUBGRAPH_DEBUG=0

# Exit if the daemon is not installed
[ -x "$DAEMON" ] || exit 0

# Load Debian configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load RedHat configuration variable file if it is present
[ -r /etc/sysconfig/$NAME ] && ./etc/sysconfig/$NAME

# Load Debian rcS variables (sets VERBOSE flag as appropriate)
if [ -r /lib/init/vars.sh ]
then
	. /lib/init/vars.sh
else
	VERBOSE=yes
fi

# Load RedHat LSB function library
if [ -r /etc/rc.d/init.d/functions ]
then
	. /etc/rc.d/init.d/functions

	log_success_msg () {
	    echo -n $*; success "$*"; echo
	}
	log_failure_msg () {
	    echo -n $*; failure "$*"; echo
	}
	log_warning_msg () {
	    echo -n $*; warning "$*"; echo
	}
	log_daemon_msg () {
	    echo -n $*
	}
fi

# Load Debian LSB function library
if [ -r /lib/lsb/init-functions ]
then
	. /lib/lsb/init-functions
fi

# Create log directories if needed
if [ ! -d $LOGDIR ]
then
	mkdir $LOGDIR
	chown -R hubgraph:hubgraph $LOGDIR
fi

export HUBGRAPH_DEBUG

start() 
{
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"

	if id --user hubgraph > /dev/null 2>&1
	then
		if ! id --group hubgraph > /dev/null 2>&1
		then
			[ "$VERBOSE" != no ] && log_failure_msg
			echo "$NAME... group 'hubgraph' does not exist"
			return 1
		fi
	else
		[ "$VERBOSE" != no ] && log_failure_msg 
		echo "$NAME... user 'hubgraph' does not exist"
		return 1
	fi

	if [ ! -d /var/lib/hubgraph ]
	then
		[ "$VERBOSE" != no ] && log_failure_msg
		echo "$NAME... /var/lib/hubgraph does not exist"
		return 1
	fi

	if [ ! -f /etc/hubgraph.conf ]
	then
		[ "$VERBOSE" != no ] && log_failure_msg 
		echo "$NAME: disabled... /etc/hubgraph.conf does not exist"
		return 0
	fi

	# Create pidfile directories if needed
	if [ ! -d $RUNDIR ]
	then
		mkdir $RUNDIR
		chown -R hubgraph:hubgraph $RUNDIR
	fi

	# Check configuration
	if [ -x /usr/bin/hzhubgraph-config ]
	then
		/usr/bin/hzhubgraph-config status 2>&1 > /dev/null

		if [ $? -ne 0 ]
		then
			[ "$VERBOSE" != no ] && log_failure_msg
			echo "$NAME: disabled (invalid configuration)"
			return 0
		fi
	fi

	export NODE_PATH=/usr/lib/hubzero-hubgraph/node_modules

	cd /var/lib/hubgraph

	if [ -x /sbin/runuser ]
	then
		/sbin/runuser -g hubgraph -c "$DAEMON start" hubgraph >> $LOGDIR/hubgraph-monitor.log 2>&1 &
	elif [ -x /sbin/start-stop-daemon ]
	then
		start-stop-daemon --pidfile $PIDFILE --exec $DAEMON -u hubgraph -c hubgraph:hubgraph --startas $DAEMON --start >> $LOGDIR/hubgraph-monitor.log 2>&1 &
	else
		su -s /bin/sh -c "$DAEMON start" hubgraph  >> $LOGDIR/hubgraph-monitor.log 2>&1 &
	fi

	if [ -d /var/lock/subsys ]
	then
		touch /var/lock/subsys/$NAME > /dev/null 2>&1

		if [ $? -ne 0 ]
		then
			log_warning_msg "Failed to touch /var/lock/subsys/$NAME"
			return 0
		fi
	fi

	[ "$VERBOSE" != no ] && log_success_msg

	return 0
}

stop () 
{
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"

	$DAEMON stop >> $LOGDIR/hubgraph-monitor.log 2>&1

	if [ $? -eq 0 ]
	then
		if [ -d /var/lock/subsys ]
		then
			rm -f /var/lock/subsys/$NAME
		fi

		[ "$VERBOSE" != no ] && log_success_msg

		return 0
	fi

	[ "$VERBOSE" != no ] && log_failure_msg

	return 1

}

reload ()
{
	[ "$VERBOSE" != no ] && log_daemon_msg "Reloading $DESC" "$NAME"

	$DAEMON reload >> $LOGDIR/hubgraph-monitor.log 2>&1

	if [ $? -eq 0 ]
	then
		[ "$VERBOSE" != no ] && log_success_msg

		return 0
	fi

	status

	if [ $? -ne 0 ]
	then
		if [ -d /var/lock/subsys ]
		then
			rm -f /var/lock/subsys/$NAME
		fi
	fi

	[ "$VERBOSE" != no ] && log_failure_msg

	return 1
}



# 0:	program is running or service is OK
# 1:	program is dead and /var/run pid file exists
# 2:	program is dead and /var/lock lock file exists
# 3:	program is not running
# 4:	program or service status is unknown
# 5-99:	reserved for future LSB use
# 100-149:	reserved for distribution use
# 150-199:	reserved for application use
# 200-254:	reserved

status() {
	# hubgraph-monitor status returns 0 or 3

	$DAEMON status >> $LOGDIR/hubgraph-monitor.log 2>&1

	return $?
}

case "$1" in

  start)
	status

	RETVAL=$?

	if [ $RETVAL = 0 ]
	then
		exit 0
	fi

	start

	RETVAL=$?

	exit $RETVAL

	;;

  stop)
	stop

	RETVAL=$?

	exit $RETVAL

	;;

  status)
	status

	RETVAL=$?

	if [ $RETVAL -eq 0 ]
	then
		echo "$NAME is running..."
	else
		echo "$NAME is stopped"
	fi
	
	exit $RETVAL

	;;

  reload)
	exit 3
	;;

  restart|force-reload)
	stop
	
	RETVAL=$?

	if [ $RETVAL -eq 0 ]
	then
		start

		RETVAL=$?
	fi

	exit $RETVAL

	;;

  condrestart|try-restart)

	status

	RETVAL=$?

	if [ $RETVAL -eq 0 ]
	then

		stop
	
		RETVAL=$?

		if [ $RETVAL -eq 0 ]
		then
			start

			RETVAL=$?
		fi
	fi

	;;
 
   *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload|condrestart|try-restart}" >&2
	exit 3
	;;
esac

:
