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

PROMTAIL_BIN="/usr/local/bin/promtail"
PROMTAIL_CFG="/usr/local/etc/promtail.config.yml"
PROMTAIL_PID="/var/run/promtail.pid"

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

case "$1" in
"start")
	echo "Starting promtail..."
	${PROMTAIL_BIN} --config ${PROMTAIL_CFG} --daemon &
	echo $! >${PROMTAIL_PID}
	echo "done"
	;;

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

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

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

esac
