#!/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/>.


startdmn=0
newsettings=0
userlist=/usr/local/etc/transmission.users
libdir=/var/packages/transmission/target/usr/local/lib

if [ "$1" = "-s" ]; then
	shift
	startdmn=1
fi
if [ "$1" = "-f" ]; then
	shift
	newsettings=1
fi

if [ "$1" = "" -o "$2" = "" ]; then
	echo "`basename $0` [-s] [-f] <username> <webui port> [<webui passwd>]"
	echo "<webui passwd> defaults to <username>!!!"
	echo "Use -s to also start the transmission daemon for only this user."
	echo "Use -f to force overwrite of existing settings."
	echo "If you use -s and -f at the same time, you have to specify -s first."
	exit 1
fi

# Tricky way to figure out where the package is installed.  Follow where
# the symbolic link to this script points to.  Then use sed to weed out the
# useless info in the ls output that comes before the "->" when listing a
# symbolic link.
pkgdir=`ls -cl $0 | sed -r "s/.*-> (.*)/\1/g"`

# Now take just the directory name.
pkgdir=`dirname $pkgdir`

username=$1
port=$2
userpwd=$3

grep "^$username$" $userlist 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
	echo "User already has permission to use transmission!"
	exit 2
fi

# If the user password is not provided create a default password by adding !!!
# to the username.
if [ "$userpwd" = "" ]; then
	userpwd=$username!!!
fi

# Find the path to the user's home dir.
userhome=`su "$username" -s /bin/sh -c "echo \\$HOME"`
if [ $? -ne 0 ]; then
	echo "Invalid user name $username !"
	exit 1
fi

# Check that it's present.
if [ "$userhome" = "" -o ! -d "$userhome" ]; then
	echo "User $username has no home directory!"
	echo "Did you enable User Home service from the Management UI?"
	echo "Should be under Management UI -> Privileges -> User -> User home button."
	exit 1
fi

# Generate the path to the dir where transmission stores the settings.
# Create it if it's not there already.
configdir=$userhome/.config/transmission-daemon
dldir=$userhome/transmission
su "$username" -s /bin/sh -c "mkdir -p $configdir"
su "$username" -s /bin/sh -c "mkdir -p $dldir"

if [ ! -f $configdir/settings.json -o $newsettings -eq 1 ]; then

	sed -r "s/%RPCUSERNAME%/$username/g; s/%RPCPORT%/$port/g; s/%RPCPASSWD%/$userpwd/g; s|%DLDIR%|$dldir|g" /$pkgdir/settings.json > $configdir/settings.json
	chown "$username":users $configdir/settings.json

	# Intentionally not using transmission-wrapper since we are killing the
	# daemon immediately.
	echo -n "Scrambling password... "
	su "$username" -s /bin/sh -c "LD_LIBRARY_PATH=$libdir transmission-daemon"
	sleep 1
	su "$username" -s /bin/sh -c "killall -q transmission-daemon"
	sleep 1
	echo "Done."
else
	echo "Using preexisting user settings."
	echo "The webui port and password you provided were ignored!"
fi

if [ $startdmn -eq 1 ]; then
	echo -n "Starting transmission daemon for this user... "
	su "$username" -s /bin/sh -c "transmission-wrapper &"
	echo "Done."
fi

echo "$username" >> $userlist
