#! /bin/sh
#
# $FreeBSD: www/tt-rss/files/ttrssd.in 300897 2012-07-14 14:29:18Z beat $
#

# PROVIDE: ttrssd
# REQUIRE: LOGIN mysql postgresql
# KEYWORD: shutdown

# Add the following line to /etc/rc.conf to enable `ttrssd':
#
#ttrssd_enable="YES"
#

. /etc/rc.subr

name="ttrssd"
rcvar=ttrssd_enable

# read settings, set default values
load_rc_config "${name}"
: ${ttrssd_enable="NO"}

required_files="/usr/local/www/tt-rss/config.php"
pidfile="/var/run/${name}.pid"
phpcli="/usr/local/bin/php"
command_interpreter=$phpcli
long_name="Tiny Tiny RSS updating feeds daemon."
phpupd="update_daemon2.php"
ttrssd_log="/var/log/${name}.log"
ttrssd_user="www"

start_cmd=${name}_start
stop_cmd=${name}_stop

ttrssd_start() {
	[ -x $phpcli ] || (echo "$phpcli not found"; exit 1)
	[ -f $pidfile ] && (echo "$name already running?"; exit 2)
	echo "Starting $long_name"
	(cd /usr/local/www/tt-rss && su -fm $ttrssd_user -c "./$phpupd" > $ttrssd_log &) && \
	echo $$ > $pidfile
}

ttrssd_stop() {
	[ -f $pidfile ] || (echo "$name not running?"; exit 1)
	echo "Stopping $long_name"
	kill `ps ax | grep $phpupd | grep -v grep | awk '{print $1}'`
	rm -f $pidfile
}

run_rc_command "$1"
