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

export DATA="/var/podgrab/assets"
export CONFIG="/var/podgrab/config"
export GIN_MODE="release"
export CHECK_FREQUENCY=240
export PORT=8004
cd /var/podgrab
PODGRAB_BIN="/usr/local/bin/podgrab"
PODGRAB_PID="/var/run/podgrab.pid"

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

case "$1" in
	"start")
		echo "Starting podgrab..."
		${PODGRAB_BIN} &
		echo $! >${PODGRAB_PID}
		echo "done"
		;;

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

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

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

esac
