#!/ffp/bin/sh

# PROVIDE: twonky
# REQUIRE: LOGIN

. /ffp/etc/ffp.subr
name="twonky"
start_cmd="twonky_start"
stop_cmd="twonky_stop"

twonky_start()
{
	# Add a route for twonky to be published via UPNP
	route add -net 224.0.0.0 netmask 240.0.0.0 dev egiga0 >/dev/null 2>/dev/null </dev/null &
	cd /ffp/opt/twonky
	mkdir -p /ffp/opt/twonky/data/ &
	# Twonky requires one handle per monitored filesystem if inotify is
	# used. On many devices, this is set to 8192, which might be too low
	# if music or photos are scanned. Therefor this value is raised to
	# the doubled amount. If more is required, change the value below.
	# Thanks Michael for reporting this!
	/ffp/sbin/sysctl fs.inotify.max_user_watches=16384 >/dev/null 2>/dev/null </dev/null &
	# The line below is without logging (logs are written to /dev/null).
        # If you want logging, exchange it with the line below the current one.
        /ffp/opt/twonky/twonkystarter -logfile /dev/null -appdata /ffp/opt/twonky/data/ >/dev/null 2>/dev/null </dev/null &
        # /ffp/opt/twonky/twonkystarter -logfile /ffp/opt/twonky/data/TwonkyMediaServer-log.txt -appdata /ffp/opt/twonky/data/ >/dev/null 2>/dev/null </dev/null &
}

twonky_stop()
{
	# Kill Twonky
	if [ -n "`pidof twonkyserver`" -o -n "`pidof twonkystarter`" ]; then
		echo "Stopping twonkyserver"
		kill -9 `pidof twonkyserver` `pidof twonkystarter`
        fi

	if [ -n "`pidof twonkyproxy`" ]; then
	        echo "Stopping twonkyproxy"
	        kill -9 `pidof twonkyproxy`
	fi

	if [ -n "`pidof twonkywebdav`" ]; then
	        echo "Stopping twonkywebdav"
	        kill -9 `pidof twonkywebdav`
	fi
	# Remove the route
	route del -net 224.0.0.0 netmask 240.0.0.0 dev egiga0 >/dev/null 2>/dev/null </dev/null &
}

run_rc_command "$1"
