#!/bin/sh
#
# General start/stop script:
# - Set the correct name and path below
# - Set USAGE_STRING below and check the allowed states in the case block
# - adjust check_config to fail or copy a config file if it is missing
# - finally adjust everything if really necessary...
#
SERVICE_NAME="createstatm"
BINARY_FILE_NAME=create_statm.sh
BINARY_FILE_PATH="/opt/gira/bin"
CONFIG_FILE_NAME=
CONFIG_FILE_PATH="/opt/userdata/log"
PRE_START_SCRIPT=
POST_STOP_SCRIPT=


# if service check is true, start is only possible, if the enable file is present
# remember to activate allowed states enable / disable
CHECK_SERVICE_ENABLED=true
# The allowed services for the usage message. Eg. "start|stop|restart|enable|disable|status"
# restart_on_failure is only used internally, if the IscWebService is configured properly.
USAGE_STRING="start|stop|enable|disable"

#### general parameter

CONFIG_FILE="${CONFIG_FILE_PATH}/${CONFIG_FILE_NAME}"
BINARY_FILE="${BINARY_FILE_PATH}/${BINARY_FILE_NAME}"
SERVICE_ENABLED="/opt/userdata/.${SERVICE_NAME}-enabled"
PID_FILE="/var/run/${SERVICE_NAME}.server.pid"

START_PARAMETER="&"

log() {
	if [ "$(type logger)" != "" ]; then
		logger -t "${SERVICE_NAME}" "$1"
	else
		echo "$1"
	fi
}

log_std() {
	if [ "$(type logger)" != "" ]; then
		logger -t "${SERVICE_NAME}" "$1"
		echo "$1"
	else
		echo "$1"
	fi
}

# Check that the service exists.
if [ ! -f ${BINARY_FILE} ]; then
	log "Service not installed!"
	log_std "failure"
	exit 1
fi

check_config() {
	# if directory is missing, create it... the next test will fail.
	# check config directory
	if [ -d "${CONFIG_FILE_PATH}" ]; then
		log "${CONFIG_FILE_PATH} found."
	else
		log "${CONFIG_FILE_PATH} not found."
		log "Creating ${CONFIG_FILE_PATH}."
		mkdir -p "${CONFIG_FILE_PATH}"
	fi
}

start() {
	log "Try to start service ${SERVICE_NAME}."
	if [ "${CHECK_SERVICE_ENABLED}" == "true" ]; then
		if [ ! -f ${SERVICE_ENABLED} ]; then
			log "Service is disabled!"
			log_std "failure"
			exit 1
		fi
	fi
	check_config
	log "Try stop previous ${SERVICE_NAME}."
	stop
	log "Starting ${SERVICE_NAME}"
	${BINARY_FILE} &
	RETVAL=$?
	pidof ${BINARY_FILE_NAME} > ${PID_FILE}
	if [ "$?" != "0" ]; then
		log_std "failed"
		return $RETVAL
	fi
	log_std "done"
	return $RETVAL
}

stop() {
	log "Stopping service ${BINARY_FILE_NAME}."
	rm -f ${PID_FILE}
	killall -9 ${BINARY_FILE_NAME}
	RETVAL=$?
	log_std "done"
	return $RETVAL
}

disable() {
	log "Disable and stopping service ${SERVICE_NAME}."
	stop
	user_disable_script
	if [ "${CHECK_SERVICE_ENABLED}" == "true" ]; then
		rm -rf ${SERVICE_ENABLED}
		if [ "$?" == "0" ]; then
			log_std "done"
		else
			log_std "failure"
			exit 1
		fi
	else
		log_std "not available"
	fi
}

enable() {
	log "Enable service ${SERVICE_NAME}."
	user_enable_script
	if [ "${CHECK_SERVICE_ENABLED}" == "true" ]; then
		touch ${SERVICE_ENABLED}
		if [ "$?" == "0" ]; then
			log_std "done"
		else
			log_std "failure"
			exit 1
		fi
	else
		log_std "not available"
	fi
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  enable)
        enable
        ;;
  disable)
        disable
        ;;
  *)
        log_std "Usage: $0 {${USAGE_STRING}}"
        exit 1
esac

exit $?
