#!/bin/sh
# Start script for FreeBSD, contributed by user Fabian Abplanalp
# Put this script in /usr/local/etc/rc.d then adjust LOKI_BIN and
# LOKI_CFG values in /etc/defaults/loki

LOKI_BIN="/usr/local/bin/loki-freebsd-amd64"
LOKI_CONFIG="/usr/local/etc/loki.config.yml"
LOKI_PID="/var/run/loki.pid"

# Read configuration variable file if it is present
[ -r /etc/defaults/loki ] && . /etc/defaults/loki

case "$1" in
"start")
	echo "Starting loki..."

	${LOKI_BIN} --config.file ${LOKI_CONFIG} &
	echo $! >${LOKI_PID}
	echo "done"
	;;

"stop")
	echo "Stopping loki..."
	if [ -f ${LOKI_PID} ]; then
		kill $(cat ${LOKI_PID})
		rm ${LOKI_PID}
		echo "done"
	else
		echo "not running?"
	fi
	;;

"restart")
	echo "Restarting loki..."
	$0 stop
	sleep 2
	$0 start
	;;

*)
	echo "$0 [start|stop|restart]"
	;;

esac
