#!/bin/sh

# Copyright 2015-2018 Rivoreo

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

. /lib/svc/share/smf_include.sh

export PATH=/usr/local/bin:/usr/bin:/usr/sbin

if ! smf_present; then
	echo "This script requires smf(5)" 1>&2
	exit $SMF_EXIT_ERR_NOSMF
fi
if [ -z "$SMF_FMRI" ]; then
	echo "This script must be run by smf(5)" 1>&2
	exit $SMF_EXIT_ERR_NOSMF
fi

MINECRAFT_HOME=`svcprop -p config/home $SMF_FMRI`
MINECRAFT_USER_NAME=`svcprop -p config/user $SMF_FMRI`
[ -z "$MINECRAFT_HOME" ] && MINECRAFT_HOME="`getent passwd $MINECRAFT_USER_NAME | cut -d : -f 6`"
if [ ! -d "$MINECRAFT_HOME" ]; then
	printf 'Home directory %s is not available\n' "$MINECRAFT_HOME" 1>&2
	exit $SMF_EXIT_ERR_CONFIG
fi
MINECRAFT_START_COMMAND="`svcprop -p config/start_command $SMF_FMRI | sed 's/\\\\ / /g'`"
if [ -z "$MINECRAFT_START_COMMAND" ]; then
	echo "property config/start_command empty" 1>&2
	exit $SMF_EXIT_ERR_CONFIG
fi

get_servers() {
	servers="`svcprop -p config/servers $SMF_FMRI`"
	[ -z "$servers" ] && exit $SMF_EXIT_ERR_CONFIG
	printf %s\\n "$servers"
}

is_running() {
	pid_file="$MINECRAFT_HOME/$1/server.pid"
	[ -f "$pid_file" ] && PID="`cat \"$pid_file\"`" && [ -n "$PID" ] && [ "$PID" != 0 ] && ps -o comm= -p $PID | /usr/gnu/bin/grep -Fq java && return 0
	rm -f "$pid_file"
	return 1
}

minecraft_start() {
	r=$SMF_EXIT_OK
	for i in `get_servers`; do
		if is_running "$i"; then
			printf "Server %s is already running\\n" "$i"
			continue
		fi
		if ! cd "$MINECRAFT_HOME/$i"; then
			#r=$SMF_EXIT_MON_DEGRADE
			continue
		fi
		if [ `svcprop -p config/use_tmux $SMF_FMRI` = true ]; then
			su "$MINECRAFT_USER_NAME" -c "exec tmux -L \"`svcprop -p config/session_name $SMF_FMRI`-$i\" new-session -d \"$MINECRAFT_START_COMMAND\""
		else
			su "$MINECRAFT_USER_NAME" -c "$MINECRAFT_START_COMMAND" < /dev/null >> "`svcprop -p config/alt_log_file $SMF_FMRI`" 2>&1 & sh_pid=$!
		fi
		sleep 1
		if is_running "$i"; then
			if [ `svcprop -p config/use_tmux $SMF_FMRI` = false ] && [ $PID != $sh_pid ]; then
				printf "Minecraft server %s appeared to be started, but PID mismatch (%s!=%s)\\n" "$i" $PID $sh_pid
			fi
		else
			printf "Failed to start Minecraft server %s\\n" "$i"
			#r=$SMF_EXIT_MON_DEGRADE
		fi
	done
	return $r
}

minecraft_stop() {
	for i in `get_servers`; do
		if ! is_running "$i"; then
			printf "Server %s is not running\\n" "$i"
			continue
		fi
		if [ `svcprop -p config/use_tmux $SMF_FMRI` = true ]; then
			su -m "$MINECRAFT_USER_NAME" -c "tmux -L \"`svcprop -p config/session_name $SMF_FMRI`-$i\" send-keys -t 0 C-u stop ENTER"
			j=12
			while [ $j -gt 0 ]; do
				j=$((j-1))
				sleep 0.5
				is_running "$1" || continue 2
			done
		fi
		kill "$PID"		
	done
}

case "$1" in
	start)
		minecraft_start
		;;
	stop)
		minecraft_stop
		;;
	*)
		printf 'Usage: %s { start | stop }\n' "$0" 1>&2
		exit 1
		;;
esac
