#!/bin/bash

# Control script for Linux RCE daemon installation and maintenance.
#
# Copyright (C) 2014 DLR, Germany
# All rights reserved
# http://www.rcenvironment.de/
#
# Author: Robert Mischke (unless noted otherwise)

PROFILE_ID=default
DAEMON_BASENAME=rce
DAEMON_NAME="$DAEMON_BASENAME-$PROFILE_ID"
DAEMON_USER=rce-daemon
ETC_INIT_D_SCRIPT="/etc/init.d/$DAEMON_NAME"

USAGE="Usage: rce-daemon {install|uninstall|locate|remove-old|start|stop|status|restart|kill}"

if [ $# == 0 ]; then echo $USAGE; exit 1; fi

if [[ $UID -ne 0 ]]; then
	echo "This script must be run with root permissions"
	exit 1
fi

# determine actual script location; source and credit: http://stackoverflow.com/questions/59895/
unset CDPATH
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# end of snippet

# determine OS variant
case `cat /proc/version` in
	*Debian* | *Ubuntu*) # Debian, Ubuntu, Mint
		METHOD=update-rc.d
		;;
	*Red\ Hat*) # Red Hat, CentOS
		METHOD=chkconfig-add
		;;
	*SUSE*) # SUSE
		METHOD=chkconfig-set
		;;
esac

if [ -e "$METHOD" ]; then echo "FATAL ERROR: Operating system variant not recognized from '$OS_VERSION'"; exit 1; fi

if [[ "$1" == "remove-old" ]]; then
	echo "Removing old daemon entries; note that running instances are not stopped automatically"

	echo "Deactivating old daemon entries; there may be errors for entries that don't exist"
	case "$METHOD" in
		update-rc.d)    update-rc.d -f rce remove; update-rc.d -f rcedaemon remove ;;
		chkconfig-add)  /sbin/chkconfig --del rce; /sbin/chkconfig --del rcedaemon ;;
		chkconfig-set)  /sbin/chkconfig --set rce off; /sbin/chkconfig --set rcedaemon off ;;
	esac
	
	echo "Deleting old daemon entries"
	rm -f /etc/init.d/rce /etc/init.d/rcedaemon
	exit 0
fi

if [[ -e "/etc/init.d/rce" ]]; then
	echo "Old daemon script /etc/init.d/rce found - run \"rce-daemon remove-old\" to delete it"
	exit;
fi
if [[ -e "/etc/init.d/rcedaemon" ]]; then
	echo "Old daemon script /etc/init.d/rcedaemon found - run \"rce-daemon remove-old\" to delete it"
	exit;
fi

if [[ $1 != "install" && ! -e $ETC_INIT_D_SCRIPT ]]; then
	echo "The daemon is not installed yet - run \"rce-daemon install\" to install it"
	exit;
fi

case "$1" in
	start) $ETC_INIT_D_SCRIPT start; exit 0;;
	stop) $ETC_INIT_D_SCRIPT stop; exit 0;;
	status) $ETC_INIT_D_SCRIPT status; exit 0;;
	restart) $ETC_INIT_D_SCRIPT restart; exit 0;;
	kill) $ETC_INIT_D_SCRIPT kill; exit 0;;
	install)
		echo "Creating daemon entry \"$ETC_INIT_D_SCRIPT\""
		cp -f "$SCRIPT_DIR/wrapper" $ETC_INIT_D_SCRIPT
		chmod +x $ETC_INIT_D_SCRIPT

		echo Activating daemon entry
		case "$METHOD" in
			update-rc.d)    update-rc.d $DAEMON_NAME start 80 2 3 4 5 . stop 20 0 1 6 . ;;
			chkconfig-add)  /sbin/chkconfig --add $DAEMON_NAME; /sbin/chkconfig $DAEMON_NAME on;;
			chkconfig-set)  /sbin/chkconfig --set $DAEMON_NAME on ;;
		esac

		echo Starting daemon instance		
		$ETC_INIT_D_SCRIPT start
		;;
	uninstall)
		echo Stopping daemon instance
		$ETC_INIT_D_SCRIPT stop

		echo Deactivating daemon entry		
		case "$METHOD" in
			update-rc.d)    update-rc.d -f $DAEMON_NAME remove ;;
			chkconfig-add)  /sbin/chkconfig $DAEMON_NAME off; /sbin/chkconfig --del $DAEMON_NAME;;
			chkconfig-set)  /sbin/chkconfig --set $DAEMON_NAME off ;;
		esac

		echo "Deleting daemon entry \"$ETC_INIT_D_SCRIPT\""
		rm "$ETC_INIT_D_SCRIPT"
		;;
	locate)
		# identify daemon user home directory
		DAEMON_USER_HOME=`eval echo ~$DAEMON_USER`
		FIRST_CHAR_OF_RESULT=${DAEMON_USER_HOME:0:1}
		if [[ "$FIRST_CHAR_OF_RESULT" == '~' ]]; then echo "No home directory found for daemon user $DAEMON_USER - did you create it?"; exit 1; fi
		# compose path
		PROFILE_DIR="$DAEMON_USER_HOME/.rce/$PROFILE_ID"
		echo "Profile directory:     $PROFILE_DIR"
		echo "Configuration file:    $PROFILE_DIR/configuration.json"
		echo "Warnings log file:     $PROFILE_DIR/warnings.log"
		echo "Full/debug log file:   $PROFILE_DIR/debug.log"
		;;
	*) 
		echo $USAGE
		exit 1
		;;
esac
