#!/bin/sh
# Copyright 2009 Saravana Kannan
# <sarav dot devel [ignore this] at gmail period com>
#
# This file is part of syno-packager.
#
# syno-packager is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# syno-packager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with syno-packager.  If not, see <http://www.gnu.org/licenses/>.


start_daemon() {
	ulist=`cat /usr/local/etc/transmission.users`
	if [ "$ulist" = "" ]; then
		echo "Run transmission-adduser to add Transmission usage priviliges to at least one user." > $SYNOPKG_TEMP_LOGFILE
		return 1
	fi
	for u in $ulist
	do
		su $u -s /bin/sh -c "transmission-wrapper &"
	done
	return 0
}

stop_daemon() {
	killall transmission-wrapper
	# Need to kill 2-3 times to work
	killall transmission-daemon
	killall transmission-daemon
	killall transmission-daemon
}

daemon_status() {
	procs=`ps aux | grep transmission-daemon | grep -v grep`
	if [ "$procs" = "" ]; then
		echo Not running.
		exit 1
	else
		echo Running.
		exit 0
	fi
}

case $1 in
start)
	echo Starting...
	start_daemon
	sleep 2
	daemon_status;;
stop|killall)
	echo Stopping...
	sleep 2
	stop_daemon
	exit 0;;
restart)
	stop_daemon
	sleep 2
	start_daemon
	daemon_status;;
status)
	# 0 - running
	# 1 - stopped
	daemon_status;;
log)
	echo No logs.
	exit 0;;
esac

exit 1
