#!/bin/bash

### BEGIN INIT INFO
# Provides:		svends-server1
# Default-Start:	2 3 4 5
# Default-Stop:		1
# Short-Description:	Sven Co-op dedicated server: Profile server1
### END INIT INFO

set -e

USER=$(whoami)
CHROOT="/home/${USER}/svends"
DAEMON="${CHROOT}/svends_run"
NAME="svends-server1"
PATH="/sbin:/bin:/usr/sbin:/usr/bin:${CHROOT}"
LOGFILE="/var/log/${NAME}.log"

test -x "${DAEMON}" || exit 1

if [ ! -e "${LOGFILE}" ]; then
	touch "${LOGFILE}"
	chmod 640 "${LOGFILE}"
	chown root:adm "${LOGFILE}"
fi

if [ ! -d "${CHROOT}" ]; then
	mkdir -p -m 0755 "${CHROOT}"
fi



BIN_INTERFACE="/usr/bin/screen -AdmS ${NAME}"

DIR_GAME="${CHROOT}"
BIN_GAME="${DAEMON}"
MOD_NAME="svencoop"
PROFILE_NAME="${NAME##*-}"

SERVICE_USER="svends"
SERVICE_SCRIPT="${DIR_GAME}/${MOD_NAME}/servers/${PROFILE_NAME}/service.sh"
SERVICE_SCRIPT_FILE="/etc/init.d/${NAME}"

FILE_SVENDS_PID="${DIR_GAME}/${MOD_NAME}/servers/${PROFILE_NAME}/svends.pid"
FILE_SCREEN_PID="${DIR_GAME}/${MOD_NAME}/servers/${PROFILE_NAME}/screen.pid"

LISTEN_ADDRESS="128.66.0.1"
LISTEN_PORT=27015
MAX_PLAYERS=12
MAP_NAME="_server_start"

LAUNCH_PARAMS="-console -norestart -game ${MOD_NAME} +ip \"${LISTEN_ADDRESS}\" -port ${LISTEN_PORT} +maxplayers ${MAX_PLAYERS} \
	+servercfgfile \"servers/${PROFILE_NAME}/server.cfg\" +logsdir \"servers/${PROFILE_NAME}/logs\" -pidfile \"${FILE_SVENDS_PID}\" \"
	+log on +map \"${MAP_NAME}\""

case "$1" in
	start)
		printf "Starting \"${NAME}\"...\n\n"

		if [ $EUID -ne 0 ] && [ "${USER}" != "${SERVICE_USER}" ]; then
			printf "[Error] Cannot start: Must be root or user ${SERVICE_USER}.\n"
			exit 100
		fi

		if [ -f "${FILE_SCREEN_PID}" ]; then
			printf "[Error] Cannot start: Already running.\n"
			exit 1
		fi

		if [ ! -f "${BIN_GAME}" ] || [ ! -x "${BIN_GAME}" ]; then
			printf "[Error] Cannot start: Server binary missing or not executable.\n"
			exit 1
		fi

		cd "${DIR_GAME}"
		if [ $EUID -ne 0 ]; then
			$BIN_INTERFACE "${BIN_GAME}" $LAUNCH_PARAMS
		else
			sudo -H -u "${SERVICE_USER}" $BIN_INTERFACE "${BIN_GAME}" $LAUNCH_PARAMS
		fi

		sleep 1
		ps -ef | grep SCREEN | grep "${NAME}" | grep -v grep | awk '{ print $2 }' > "${FILE_SCREEN_PID}"
		printf "Screen process ID file: ${FILE_SCREEN_PID}\n"
	;;

	stop)
		printf "Stopping \"${NAME}\"...\n\n"

		if [ $EUID -ne 0 ] && [ "${USER}" != "${SERVICE_USER}" ]; then
			printf "[Error] Cannot stop: Must be root or user ${SERVICE_USER}.\n"
			exit 100
		fi

		if [ ! -f "${FILE_SCREEN_PID}" ]; then
			printf "[Error] Cannot stop: Not running.\n"
			exit 1
		fi

		for PID in $(cat "${FILE_SCREEN_PID}"); do
			printf "Killing process ID ${PID}...\n"
			kill -TERM "${PID}"
			break
		done

		printf "Removing Screen process ID file...\n"
		rm -f "${FILE_SCREEN_PID}"

		screen -wipe 1> /dev/null 2> /dev/null
	;;

	restart)
		$0 stop
		$0 start
	;;

	status)
		if [ -f "${FILE_SCREEN_PID}" ]; then
			printf "Service is running.\n"
		else
			printf "Service is not running.\n"
		fi
	;;

	install)
		if [ $EUID -ne 0 ]; then
			printf "[Error] Service installation can only be done as root.\n"
			exit 100
		fi

		if [ -f "${SERVICE_SCRIPT_FILE}" ]; then
			printf "[Warning] Service already installed, reinstalling.\n"

			if [ -f "${FILE_SCREEN_PID}" ]; then
				$0 stop
			fi
		fi

		cp "${SERVICE_SCRIPT}" "${SERVICE_SCRIPT_FILE}"
		printf "Service \"${NAME}\" installed.\n"
	;;

	uninstall)
		if [ $EUID -ne 0 ]; then
			printf "[Error] Service uninstallation can only be done as root.\n"
			exit 100
		fi

		if [ ! -f "${SERVICE_SCRIPT_FILE}" ]; then
			printf "[Error] Cannot uninstall: Service not installed.\n"
		fi

		if [ -f "${FILE_SCREEN_PID}" ]; then
			$0 stop
		fi

		rm "${SERVICE_SCRIPT_FILE}"
		printf "Service \"${NAME}\" uninstalled.\n"
	;;

	*)
		if [ $EUID -ne 0 -a "${USER}" != "${SERVICE_USER}" ]; then
			printf "[Error] Cannot use: Must be root or user ${SERVICE_USER}.\n"
			exit 100
		fi

		if [ $EUID -ne 0 ]; then
			printf "Usage: ${0} start|stop|restart|status\n"
		else
			printf "Usage: ${0} start|stop|restart|status|install|uninstall\n"
		fi
	;;
esac
