#!/sbin/runscript

rundir=${rundir:-/var/run/sauerbraten}

depend() {
	use net
}

start() {
	[ $(echo ${RUNMASTER} | tr '[:upper:]' '[:lower:]') = "yes" ] && start_master
	[ $(echo ${RUNSERVER} | tr '[:upper:]' '[:lower:]') = "yes" ] && start_server
	true
}

start_server() {
	ebegin "Starting Sauerbraten dedicated server"

	# Make sure necessary parameters are set
	SERVERCONFIG="${SERVERCONFIG:-/var/lib/games/sauerbraten}"

	if [ ! -d ${SERVERCONFIG} ]; then
		einfo "No ${SERVERCONFIG} around. Creating new..."
		mkdir ${SERVERCONFIG}
		chown ${USER}:${GROUP} ${SERVERCONFIG}
	fi

	[ -d ${rundir} ] || {
		mkdir -p "${rundir}"
		chown -R ${USER}:${GROUP} "${rundir}"
	}

	start-stop-daemon \
		--quiet --start --user ${USER}:${GROUP} --chdir ${SERVERCONFIG} \
		--make-pidfile --pidfile "${rundir}"/server.pid \
		--background --exec /usr/libexec/sauerbraten/sauer_server -- ${SERVERARGS}
	eend $?
}

start_master() {
	ebegin "Starting Sauerbraten master server"

	# Make sure necessary parameters are set
	MASTERCONFIG="${MASTERCONFIG:-/var/lib/games/sauerbraten}"
	[ -n "${MASTERIP}" ] && MASTERPORT="${MASTERPORT:-28787}"
	MASTERARGS="${MASTERCONFIG}/ ${MASTERPORT} ${MASTERIP}"

	if [ ! -d ${MASTERCONFIG} ]; then
		einfo "No ${MASTERCONFIG} around. Creating new..."
		mkdir ${MASTERCONFIG}
		chown ${USER}:${GROUP} ${MASTERCONFIG}
	fi

	[ -d ${rundir} ] || {
		mkdir -p "${rundir}"
		chown -R ${USER}:${GROUP} "${rundir}"
	}

	start-stop-daemon \
		--quiet --start --user ${USER}:${GROUP} \
		--make-pidfile --pidfile "${rundir}"/master.pid \
		--background --exec /usr/libexec/sauerbraten/sauer_master -- ${MASTERARGS}
	eend $?
}

stop() {
	[ $(echo ${RUNSERVER} | tr '[:upper:]' '[:lower:]') = "yes" ] && stop_server
	[ $(echo ${RUNMASTER} | tr '[:upper:]' '[:lower:]') = "yes" ] && stop_master
	true
}

stop_server() {
	ebegin "Stopping Sauerbraten dedicated server"
	start-stop-daemon --stop --retry 3 --pidfile "${rundir}"/server.pid
	eend $?
}

stop_master() {
	ebegin "Stopping Sauerbraten master server"
	start-stop-daemon --stop --retry 3 --pidfile "${rundir}"/master.pid
	eend $?
}

restart() {
	svc_stop
	svc_start
}
