#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @author    Marcel Beck <marcel.beck@mbeck.org>
# @copyright Copyright (c) 2009-2012 Volker Theile
# @copyright Copyright (c) 2011-2012 Marcel Beck
#
# OpenMediaVault 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
# any later version.
#
# OpenMediaVault 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 OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_TRANSMISSIONBT_CONFIG=${OMV_TRANSMISSIONBT_CONFIG:-"/etc/transmission-daemon/settings.json"}
OMV_TRANSMISSIONBT_DEFAULT=${OMV_TRANSMISSIONBT_DEFAULT:-"/etc/default/transmission-daemon"}
OMV_TRANSMISSIONBT_RPCWHITELIST=${OMV_TRANSMISSIONBT_RPCWHITELIST:-"*.*.*.*"}
OMV_TRANSMISSIONBT_RPCWHITELISTENABLED=${OMV_TRANSMISSIONBT_RPCWHITELISTENABLED:-"true"}

# Create '/etc/default/transmission-daemon' file
cat <<EOF > ${OMV_TRANSMISSIONBT_DEFAULT}
# defaults for transmission-daemon
# sourced by /etc/init.d/transmission-daemon

EOF

xmlstarlet sel -t -m "//services/transmissionbt" \
  -v "concat('ENABLE_DAEMON=',enable)" -n \
  -o "CONFIG_DIR=&quot;/var/lib/transmission-daemon/info&quot;" -n \
  -o "OPTIONS=&quot;--config-dir \$CONFIG_DIR" \
  -i "rpcauthenticationrequired[. = '1']" -o " --auth" -b \
  -i "rpcauthenticationrequired[. = '0']" -o " --no-auth" -b \
  -o "&quot;" \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc >> ${OMV_TRANSMISSIONBT_DEFAULT}

# Create '/etc/transmission-daemon/settings.json' file
xmlstarlet sel -t -m "//services/transmissionbt" \
  -o "{" -n \
  -o "&quot;download-dir&quot;: &quot;" ${OMV_XMLSTARLET_GET_SHAREDFOLDER_PATH} -o "/" -v modules/locationsandfiles/download-dir -o "&quot;," -n \
  -v "concat('&quot;encryption&quot;: ',encryption,',')" -n \
  -o "&quot;dht-enabled:&quot;: " \
  -i "dhtenabled[. = '0']" -o "false" -b \
  -i "dhtenabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;blocklist-enabled&quot;: " \
  -i "blocklistenabled[. = '0']" -o "false" -b \
  -i "blocklistenabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;pex-enabled&quot;: " \
  -i "pexenabled[. = '0']" -o "false" -b \
  -i "pexenabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;lazy-bitfield-enabled&quot;: " \
  -i "lazy-bitfield-enabled[. = '0']" -o "false" -b \
  -i "lazy-bitfield-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;umask&quot;: ',umask,',')" -n \
  -o "&quot;lpd-enabled&quot;: " \
  -i "lpd-enabled[. = '0']" -o "false" -b \
  -i "lpd-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;scrape-paused-torrents-enabled&quot;: " \
  -i "scrape-paused-torrents-enabled[. = '0']" -o "false" -b \
  -i "scrape-paused-torrents-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;utp-enabled&quot;: " \
  -i "utp-enabled[. = '0']" -o "false" -b \
  -i "utp-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;message-level&quot;: ',message-level,',')" -n \
  -v "concat('&quot;cache-size-mb&quot;: ',cache-size-mb,',')" -n \
  -v "concat('&quot;rpc-enabled&quot;: ','true',',')" -n \
  -o "&quot;rpc-authentication-required&quot;: " \
  -i "rpcauthenticationrequired[. = '0']" -o "false" -b \
  -i "rpcauthenticationrequired[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;rpc-password&quot;: &quot;',rpcpassword,'&quot;,')" -n \
  -v "concat('&quot;rpc-port&quot;: ',rpcport,',')" -n \
  -v "concat('&quot;rpc-url&quot;: &quot;',rpcurl,'&quot;,')" -n \
  -v "concat('&quot;rpc-username&quot;: &quot;',rpcusername,'&quot;,')" -n \
  -o "&quot;rpc-whitelist&quot;: &quot;${OMV_TRANSMISSIONBT_RPCWHITELIST}&quot;," -n \
  -o "&quot;rpc-whitelist-enabled&quot;: ${OMV_TRANSMISSIONBT_RPCWHITELISTENABLED}," -n \
  -o "&quot;incomplete-dir-enabled&quot;: " \
  -i "modules/locationsandfiles/incomplete-dir-enabled[. = '0']" -o "false" -b \
  -i "modules/locationsandfiles/incomplete-dir-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;incomplete-dir&quot;: &quot;" ${OMV_XMLSTARLET_GET_SHAREDFOLDER_PATH} -o "/" -v modules/locationsandfiles/incomplete-dir -o "&quot;," -n \
  -o "&quot;watch-dir-enabled&quot;: " \
  -i "modules/locationsandfiles/watch-dir-enabled[. = '0']" -o "false" -b \
  -i "modules/locationsandfiles/watch-dir-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;watch-dir&quot;: &quot;" ${OMV_XMLSTARLET_GET_SHAREDFOLDER_PATH} -o "/" -v modules/locationsandfiles/watch-dir -o "&quot;," -n \
  -v "concat('&quot;preallocation&quot;: ',modules/locationsandfiles/preallocation,',')" -n \
  -o "&quot;rename-partial-files&quot;: " \
  -i "modules/locationsandfiles/rename-partial-files[. = '0']" -o "false" -b \
  -i "modules/locationsandfiles/rename-partial-files[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;start-added-torrents&quot;: " \
  -i "modules/locationsandfiles/start-added-torrents[. = '0']" -o "false" -b \
  -i "modules/locationsandfiles/start-added-torrents[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;trash-original-torrent-files&quot;: " \
  -i "modules/locationsandfiles/trash-original-torrent-files[. = '0']" -o "false" -b \
  -i "modules/locationsandfiles/trash-original-torrent-files[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;speed-limit-down&quot;: ',modules/bandwidth/speed-limit-down,',')" -n \
  -o "&quot;speed-limit-down-enabled&quot;: " \
  -i "modules/bandwidth/speed-limit-down-enabled[. = '0']" -o "false" -b \
  -i "modules/bandwidth/speed-limit-down-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;speed-limit-up&quot;: ',modules/bandwidth/speed-limit-up,',')" -n \
  -o "&quot;speed-limit-up-enabled&quot;: " \
  -i "modules/bandwidth/speed-limit-up-enabled[. = '0']" -o "false" -b \
  -i "modules/bandwidth/speed-limit-up-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;upload-slots-per-torrent&quot;: ',modules/bandwidth/upload-slots-per-torrent,',')" -n \
  -o "&quot;alt-speed-enabled&quot;: " \
  -i "modules/bandwidth/alt-speed-enabled[. = '0']" -o "false" -b \
  -i "modules/bandwidth/alt-speed-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;alt-speed-up&quot;: ',modules/bandwidth/alt-speed-up,',')" -n \
  -v "concat('&quot;alt-speed-down&quot;: ',modules/bandwidth/alt-speed-down,',')" -n \
  -o "&quot;queue-stalled-enabled&quot;: " \
  -i "modules/queuing/queue-stalled-enabled[. = '0']" -o "false" -b \
  -i "modules/queuing/queue-stalled-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;queue-stalled-minutes&quot;: ',modules/queuing/queue-stalled-minutes,',')" -n \
  -o "&quot;download-queue-enabled&quot;: " \
  -i "modules/queuing/download-queue-enabled[. = '0']" -o "false" -b \
  -i "modules/queuing/download-queue-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;download-queue-size&quot;: ',modules/queuing/download-queue-size,',')" -n \
  -o "&quot;seed-queue-enabled&quot;: " \
  -i "modules/queuing/seed-queue-enabled[. = '0']" -o "false" -b \
  -i "modules/queuing/seed-queue-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;seed-queue-size&quot;: ',modules/queuing/seed-queue-size,',')" -n \
  -v "concat('&quot;bind-address-ipv4&quot;: &quot;',modules/peer/bind-address-ipv4,'&quot;,')" -n \
  -v "concat('&quot;bind-address-ipv6&quot;: &quot;',modules/peer/bind-address-ipv6,'&quot;,')" -n \
  -v "concat('&quot;peer-limit-global&quot;: ',modules/peer/peer-limit-global,',')" -n \
  -v "concat('&quot;peer-limit-per-torrent&quot;: ',modules/peer/peer-limit-per-torrent,',')" -n \
  -v "concat('&quot;peer-socket-tos&quot;: &quot;',modules/peer/peer-socket-tos,'&quot;,')" -n \
  -v "concat('&quot;peer-port&quot;: ',modules/peer/peer-port,',')" -n \
  -v "concat('&quot;peer-port-random-high&quot;: ',modules/peer/peer-port-random-high,',')" -n \
  -v "concat('&quot;peer-port-random-low&quot;: ',modules/peer/peer-port-random-low,',')" -n \
  -o "&quot;port-forwarding-enabled&quot;: " \
  -i "modules/peer/port-forwarding-enabled[. = '0']" -o "false" -b \
  -i "modules/peer/port-forwarding-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;peer-port-random-on-start&quot;: " \
  -i "modules/peer/peer-port-random-on-start[. = '0']" -o "false" -b \
  -i "modules/peer/peer-port-random-on-start[. = '1']" -o "true" -b \
  -o "," -n \
  -o "&quot;alt-speed-time-enabled&quot;: " \
  -i "modules/scheduling/alt-speed-time-enabled[. = '0']" -o "false" -b \
  -i "modules/scheduling/alt-speed-time-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;alt-speed-time-begin&quot;: ',modules/scheduling/alt-speed-time-begin,',')" -n \
  -v "concat('&quot;alt-speed-time-end&quot;: ',modules/scheduling/alt-speed-time-end,',')" -n \
  -v "concat('&quot;alt-speed-time-day&quot;: ',modules/scheduling/alt-speed-time-day,',')" -n \
  -v "concat('&quot;idle-seeding-limit&quot;: ',modules/scheduling/idle-seeding-limit,',')" -n \
  -o "&quot;idle-seeding-limit-enabled&quot;: " \
  -i "modules/scheduling/idle-seeding-limit-enabled[. = '0']" -o "false" -b \
  -i "modules/scheduling/idle-seeding-limit-enabled[. = '1']" -o "true" -b \
  -o "," -n \
  -v "concat('&quot;ratio-limit&quot;: ',modules/scheduling/ratio-limit,',')" -n \
  -o "&quot;ratio-limit-enabled&quot;: " \
  -i "modules/scheduling/ratio-limit-enabled[. = '0']" -o "false" -b \
  -i "modules/scheduling/ratio-limit-enabled[. = '1']" -o "true" -b \
  -o "}" \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_TRANSMISSIONBT_CONFIG}


enable=$(omv_config_get "//services/transmissionbt/enable")

watch_dir_enable=$(omv_config_get "//services/transmissionbt/modules/locationsandfiles/watch-dir-enabled")
if [ 1 = ${enable} -a  1 = ${watch_dir_enable} ]; then
	watch_dir=$(omv_config_get "//services/transmissionbt/modules/locationsandfiles/watch-dir")
	watch_dir_full=`xmlstarlet sel -t -m "//services/transmissionbt" ${OMV_XMLSTARLET_GET_SHAREDFOLDER_PATH} -o "/" -v "modules/locationsandfiles/watch-dir" ${OMV_CONFIG_FILE}`
	mkdir -p ${watch_dir_full}
fi

# Create transmission cron file
blocklistsyncenabled=$(omv_config_get "//services/transmissionbt/blocklistsyncenabled")
blocklistsyncfrequency=$(omv_config_get "//services/transmissionbt/blocklistsyncfrequency")
cronfile="/etc/cron.${blocklistsyncfrequency}/transmissionbt"

# Remove all previous cron files. Due we do not know if the frequency
# have been modified we have to scan all /etc/cron.xxx directories.
for crondir in $(ls -d /etc/cron.*ly)
do
	[ -e "${crondir}/transmissionbt" ] && rm "${crondir}/transmissionbt"
done

if [ 1 = ${enable} -a 1 = ${blocklistsyncenabled} ]; then
	blocklisturl=$(omv_config_get "//services/transmissionbt/blocklisturl")
	cat <<EOF > ${cronfile}
#!/bin/sh
#
# cron script to update TransmissionBT blocklists
# by Volker Theile <volker.theile@openmediavault.org>

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

# Download blocklist archive
cd /var/lib/transmission-daemon/info/blocklists
if wget ${blocklisturl} 1>/dev/null 2>&1 ; then
	rm -f level1 && gunzip level1.gz
	omv_log -t transmission "Blocklist updated"
else
	omv_log -t transmission "Failed to update blocklist"
fi
# Restart transmission-daemon to use the latest blocklist
invoke-rc.d transmission-daemon restart 1>/dev/null 2>&1 || true
EOF
	chmod 755 ${cronfile}
fi