#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Torrent tracker functions
#
# License
# =======
#
# Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

TRACKER_PORT=6969

function mesh_install_tracker {
    chroot "$rootdir" apt-get -yq install bittornado nginx

    TRACKER_DAEMON=$rootdir/etc/systemd/system/tracker.service
    echo '[Unit]' > $TRACKER_DAEMON
    echo 'Description=Torrent Tracker' >> $TRACKER_DAEMON
    echo 'After=syslog.target' >> $TRACKER_DAEMON
    echo 'After=network.target' >> $TRACKER_DAEMON
    echo '[Service]' >> $TRACKER_DAEMON
    echo 'Type=simple' >> $TRACKER_DAEMON
    echo 'User=tracker' >> $TRACKER_DAEMON
    echo 'Group=tracker' >> $TRACKER_DAEMON
    echo "WorkingDirectory=/var/lib/tracker" >> $TRACKER_DAEMON
    echo "ExecStart=/usr/bin/bttrack --port $TRACKER_PORT --dfile /var/lib/tracker/dstate --logfile /var/lib/tracker/tracker.log --nat_check 0 --scrape_allowed full --ipv6_enabled 0" >> $TRACKER_DAEMON
    echo '' >> $TRACKER_DAEMON
    echo 'TimeoutSec=300' >> $TRACKER_DAEMON
    echo '' >> $TRACKER_DAEMON
    echo '[Install]' >> $TRACKER_DAEMON
    echo 'WantedBy=multi-user.target' >> $TRACKER_DAEMON

    chroot "$rootdir" useradd -d /var/lib/tracker/ -s /bin/false tracker
    if [ ! -d $rootdir/var/lib/tracker ]; then
        mkdir $rootdir/var/lib/tracker
    fi
    chroot "$rootdir" chown -R tracker:tracker /var/lib/tracker

    chroot "$rootdir" systemctl enable tracker.service
}

function install_tracker {
    if [ $INSTALLING_MESH ]; then
        mesh_install_tracker
        return
    fi

    if [[ $(is_completed $FUNCNAME) == "1" ]]; then
        return
    fi

    apt-get -yq install bittornado nginx

    TRACKER_DAEMON=/etc/systemd/system/tracker.service
    echo '[Unit]' > $TRACKER_DAEMON
    echo 'Description=Torrent Tracker' >> $TRACKER_DAEMON
    echo 'After=syslog.target' >> $TRACKER_DAEMON
    echo 'After=network.target' >> $TRACKER_DAEMON
    echo '[Service]' >> $TRACKER_DAEMON
    echo 'Type=simple' >> $TRACKER_DAEMON
    echo 'User=tracker' >> $TRACKER_DAEMON
    echo 'Group=tracker' >> $TRACKER_DAEMON
    echo "WorkingDirectory=/var/lib/tracker" >> $TRACKER_DAEMON
    echo "ExecStart=/usr/bin/bttrack --port $TRACKER_PORT --dfile /var/lib/tracker/dstate --logfile /var/lib/tracker/tracker.log --nat_check 0 --scrape_allowed full --ipv6_enabled 0" >> $TRACKER_DAEMON
    echo '' >> $TRACKER_DAEMON
    echo 'TimeoutSec=300' >> $TRACKER_DAEMON
    echo '' >> $TRACKER_DAEMON
    echo '[Install]' >> $TRACKER_DAEMON
    echo 'WantedBy=multi-user.target' >> $TRACKER_DAEMON

    useradd -d /var/lib/tracker/ -s /bin/false tracker
    if [ ! -d /var/lib/tracker ]; then
        mkdir /var/lib/tracker
    fi
    chown -R tracker:tracker /var/lib/tracker

    systemctl enable tracker.service
    systemctl start tracker.service

    mark_completed $FUNCNAME
}

# NOTE: deliberately no exit 0
