#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Distributed storage
#
# 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/>.

VARIANTS=''

IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1

TAHOELAFS_REPO="https://github.com/tahoe-lafs/tahoe-lafs"
TAHOELAFS_COMMIT='30b421d48b3d1dae2ef507bf27a1f3816300cd92'

TAHOELAFS_PORT=50213
TAHOELAFS_WEB_PORT=3456
TAHOELAFS_STORAGE_SPACE=1G
TAHOELAFS_SHARED_DIR='Shared'
TAHOE_COMMAND="cd /var/lib/tahoelafs && venv/bin/tahoe"

tahoelafs_variables=(TAHOELAFS_REPO
                     TAHOELAFS_COMMIT
                     TAHOELAFS_PORT)

function install_interactive_tahoelafs {
    echo -n ''
    APP_INSTALLED=1
}

function reconfigure_tahoelafs {
    for d in /home/*/ ; do
        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
        if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
            if [ -d /home/$USERNAME/.tahoe ]; then
                su -c "$TAHOE_COMMAND stop" - $USERNAME
                rm -rf /home/$USERNAME/.tahoe
            fi

            if [ -d /home/$USERNAME/${TAHOELAFS_SHARED_DIR} ]; then
                rm -rf /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
            fi

            su -c "cd $TAHOE_COMMAND create-node" - $USERNAME

            if [ ! -d /home/$USERNAME/.tahoe ]; then
                exit 63722
            fi

            # create a shared directory
            if [ ! -d /home/$USERNAME/${TAHOELAFS_SHARED_DIR} ]; then
                mkdir /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
                chown -R $USERNAME:$USERNAME /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
            fi

            TAHOELAFS_CONFIG=/home/$USERNAME/.tahoe/tahoe.cfg
            echo '[node]' > $TAHOELAFS_CONFIG
            echo "nickname = $USERNAME" >> $TAHOELAFS_CONFIG
            echo "web.port = tcp:$TAHOELAFS_WEB_PORT:interface=127.0.0.1" >> $TAHOELAFS_CONFIG
            echo 'web.static = public_html' >> $TAHOELAFS_CONFIG
            echo "tub.port = tcp:$TAHOELAFS_PORT" >> $TAHOELAFS_CONFIG
            echo "tub.location = tcp:$HOSTNAME:$TAHOELAFS_PORT" >> $TAHOELAFS_CONFIG
            echo 'timeout.keepalive = 240' >> $TAHOELAFS_CONFIG
            echo 'timeout.disconnect = 1800' >> $TAHOELAFS_CONFIG
            echo '' >> $TAHOELAFS_CONFIG
            echo '[client]' >> $TAHOELAFS_CONFIG
            echo 'introducer.furl = None' >> $TAHOELAFS_CONFIG
            echo '#helper.furl =' >> $TAHOELAFS_CONFIG
            echo '' >> $TAHOELAFS_CONFIG
            echo 'shares.needed = 1' >> $TAHOELAFS_CONFIG
            echo 'shares.happy = 1' >> $TAHOELAFS_CONFIG
            echo 'shares.total = 1' >> $TAHOELAFS_CONFIG
            echo '' >> $TAHOELAFS_CONFIG
            echo '[storage]' >> $TAHOELAFS_CONFIG
            echo 'enabled = true' >> $TAHOELAFS_CONFIG
            echo "reserved_space = $TAHOELAFS_STORAGE_SPACE" >> $TAHOELAFS_CONFIG
            echo '#expire.enabled = true' >> $TAHOELAFS_CONFIG
            echo '#expire.mode =' >> $TAHOELAFS_CONFIG
            echo '' >> $TAHOELAFS_CONFIG
            echo '[helper]' >> $TAHOELAFS_CONFIG
            echo 'enabled = false' >> $TAHOELAFS_CONFIG
            echo '' >> $TAHOELAFS_CONFIG
            echo '[magic_folder]' >> $TAHOELAFS_CONFIG
            echo 'enabled = true' >> $TAHOELAFS_CONFIG
            echo "local.directory = /home/${USERNAME}/${TAHOELAFS_SHARED_DIR}" >> $TAHOELAFS_CONFIG
            chown $USERNAME:$USERNAME $TAHOELAFS_CONFIG

            su -c "$TAHOE_COMMAND start" - $USERNAME
            su -c "$TAHOE_COMMAND magic-folder create $HOSTNAME" - $USERNAME
        fi
    done
}

function upgrade_tahoelafs {
    echo -n ''
    # TODO
}

function backup_local_tahoelafs {
    source_directory=/home/tahoelafs/.tahoe-introducer
    if [ ! -d $source_directory ]; then
        return
    fi
    systemctl stop tahoelafs
    dest_directory=tahoelafs
    function_check backup_directory_to_usb
    backup_directory_to_usb $source_directory $dest_directory
    systemctl start tahoelafs
}

function restore_local_tahoelafs {
    echo $"Restoring Tahoe-LAFS introducer"
    systemctl stop tahoelafs
    temp_restore_dir=/root/temptahoelafs
    restore_directory_from_usb $temp_restore_dir tahoelafs
    mv /home/tahoelafs/.tahoe-introducer /home/tahoelafs/.tahoe-introducer-old
    cp -r $temp_restore_dir/home/tahoelafs/.tahoe-introducer /home/tahoelafs/
    if [ ! "$?" = "0" ]; then
        mv /home/tahoelafs/.tahoe-introducer-old /home/tahoelafs/.tahoe-introducer
        exit 246833
    fi
    rm -rf /home/tahoelafs/.tahoe-introducer
    chown -R tahoelafs:tahoelafs /home/tahoelafs
    systemctl start tahoelafs
    echo $"Restore complete"
}

function backup_remote_tahoelafs {
    source_directory=/home/tahoelafs/.tahoe-introducer
    if [ ! -d $source_directory ]; then
        return
    fi
    systemctl stop tahoelafs
    dest_directory=tahoelafs
    function_check backup_directory_to_usb
    backup_directory_to_friend $source_directory $dest_directory
    systemctl start tahoelafs
}

function restore_remote_tahoelafs {
    echo $"Restoring Tahoe-LAFS introducer"
    systemctl stop tahoelafs
    temp_restore_dir=/root/temptahoelafs
    restore_directory_from_friend $temp_restore_dir tahoelafs
    mv /home/tahoelafs/.tahoe-introducer /home/tahoelafs/.tahoe-introducer-old
    cp -r $temp_restore_dir/home/tahoelafs/.tahoe-introducer /home/tahoelafs/
    if [ ! "$?" = "0" ]; then
        mv /home/tahoelafs/.tahoe-introducer-old /home/tahoelafs/.tahoe-introducer
        exit 623925
    fi
    rm -rf /home/tahoelafs/.tahoe-introducer-old
    chown -R tahoelafs:tahoelafs /home/tahoelafs
    systemctl start tahoelafs
    echo $"Restore complete"
}

function remove_tahoelafs {
    firewall_remove ${TAHOELAFS_PORT}

    for d in /home/*/ ; do
        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
        if [ -f /home/$USERNAME/.tahoe ]; then
            su -c "$TAHOE_COMMAND stop" - $USERNAME
            rm -rf /home/$USERNAME/.tahoe
        fi
    done

    rm -rf /var/lib/tahoelafs

    remove_completion_param install_tahoelafs
    remove_completion_param configure_firewall_for_tahoelafs
}

function configure_firewall_for_tahoelafs {
    if [[ $(is_completed $FUNCNAME) == "1" ]]; then
        return
    fi
    firewall_add Tahoe-LAFS ${TAHOELAFS_PORT}
    mark_completed $FUNCNAME
}

function tahoelafs_update_script {
    update_script_filename=$1

    echo '#!/bin/bash' > $update_script_filename
    echo 'TOX_USERNAME=$(toxid --showuser)' >> $update_script_filename
    echo 'PEERS_FILE=$(mktemp /tmp/tahoelafs-peers.XXXXXX)' >> $update_script_filename
    echo -n 'avahi-browse -atl | grep "Workstation" | ' >> $update_script_filename
    echo -n "awk -F ' ' '{print \$4}' " >> $update_script_filename
    echo '| sort -u > $PEERS_FILE' >> $update_script_filename
    echo 'if [ ! "$?" = "0" ]; then' >> $update_script_filename
    echo '    exit 1' >> $update_script_filename
    echo 'fi' >> $update_script_filename
    echo '' >> $update_script_filename
    echo 'if [ ! -f $PEERS_FILE ]; then' >> $update_script_filename
    echo '    exit 0' >> $update_script_filename
    echo 'fi' >> $update_script_filename
    echo '' >> $update_script_filename
    echo 'furl=""' >> $update_script_filename
    echo 'while IFS="" read -r line || [[ -n "$line" ]]; do' >> $update_script_filename
    echo '    if [[ $furl != "" ]]; then' >> $update_script_filename
    echo -n '        furl="$furl,$line:' >> $update_script_filename
    echo -n "$TAHOELAFS_PORT" >> $update_script_filename
    echo '"' >> $update_script_filename
    echo '    else' >> $update_script_filename
    echo -n '        furl="$line:' >> $update_script_filename
    echo -n "$TAHOELAFS_PORT" >> $update_script_filename
    echo '"' >> $update_script_filename
    echo '    fi' >> $update_script_filename
    echo 'done < "$PEERS_FILE"' >> $update_script_filename
    echo '' >> $update_script_filename
    echo 'rm $PEERS_FILE' >> $update_script_filename
    echo '' >> $update_script_filename
    echo 'for d in /home/*/ ; do' >> $update_script_filename
    echo -n '    USERNAME=$(echo "$d" | ' >> $update_script_filename
    echo "awk -F '/' '{print \$3}')'" >> $update_script_filename
    echo '    if [ -f /home/$USERNAME/.tahoe/tahoe.cfg ]; then' >> $update_script_filename
    echo '        if ! grep -q "introducer.furl = ${furl}" /home/$USERNAME/.tahoe/tahoe.cfg; then' >> $update_script_filename
    echo '            sed -i "s|introducer.furl =.*|introducer.furl = ${furl}|g" /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
    echo '            sed -i "s|nickname =.*|nickname = ${TOX_USERNAME}|g" /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
    echo '            chown $USERNAME:$USERNAME /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
    echo "            su -c '$TAHOE_COMMAND restart' - \$USERNAME" >> $update_script_filename
    echo '        fi' >> $update_script_filename
    echo '    fi' >> $update_script_filename
    echo 'done' >> $update_script_filename
    echo '' >> $update_script_filename

    echo 'exit 0' >> $update_script_filename
    chmod +x $update_script_filename
}

function mesh_install_tahoelafs {
    chroot "$rootdir" apt-get -yq install build-essential python-pip python-dev
    chroot "$rootdir" apt-get -yq install libffi-dev libssl-dev python-virtualenv

    git clone $TAHOELAFS_REPO $rootdir/var/lib/tahoelafs
    cd $rootdir/var/lib/tahoelafs
    git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT

    cat <<EOF > $rootdir/root/install_tahoelafs
cd /var/lib/tahoelafs
virtualenv venv
venv/bin/pip install --editable .
EOF
    chroot "$rootdir" chmod +x /root/install_tahoelafs
    chroot "$rootdir" /root/install_tahoelafs
    chroot "$rootdir" rm /root/install_tahoelafs

    tahoelafs_update_script $rootdir/usr/bin/update-tahoelafs

    if ! grep -q "update-tahoelafs" $rootdir/etc/crontab; then
        echo "*/1            * *   *   *   root /usr/bin/update-tahoelafs 2> /dev/null" >> $rootdir/etc/crontab
    fi
}

function install_tahoelafs {
    if [ $INSTALLING_MESH ]; then
        mesh_install_tahoelafs
        return
    fi

    apt-get -yq install build-essential python-pip python-dev libffi-dev libssl-dev python-virtualenv

    # create a user to run the introducer
    if [ ! -d /home/tahoelafs ]; then
        # add a gogs user account
        adduser --disabled-login --gecos 'tahoe-lafs' tahoelafs
    fi

    if [ -d /home/tahoelafs/Maildir ]; then
        rm -rf /home/tahoelafs/Maildir
    fi

    if [ ! -d /home/tahoelafs/.tahoe-introducer ]; then
        mkdir /home/tahoelafs/.tahoe-introducer
    fi

    git_clone $TAHOELAFS_REPO /home/tahoelafs/tahoelafs
    cd /home/tahoelafs/tahoelafs
    git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT
    virtualenv venv
    venv/bin/pip install --editable .
    configure_firewall_for_tahoelafs

    su -c "cd $TAHOE_COMMAND create-introducer /home/tahoelafs/.tahoe-introducer" - tahoelafs
    TAHOELAFS_CONFIG=/home/tahoelafs/.tahoe-introducer/tahoe.cfg
    if [ ! -f $TAHOELAFS_CONFIG ]; then
        exit 62831
    fi

    sed -i "s|nickname =.*|nickname = $HOSTNAME|g" $TAHOELAFS_CONFIG

    if ! grep -q "[storage]" $TAHOELAFS_CONFIG; then
        echo '' >> $TAHOELAFS_CONFIG
        echo '[storage]' >> $TAHOELAFS_CONFIG
        echo 'enabled = true' >> $TAHOELAFS_CONFIG
        echo "reserved_space = $TAHOELAFS_STORAGE_SPACE" >> $TAHOELAFS_CONFIG
    fi

    chown -R tahoelafs:tahoelafs /home/tahoelafs

    TAHOELAFS_DAEMON_FILE=/etc/systemd/system/tahoelafs.service
    echo '[Unit]' > $TAHOELAFS_DAEMON_FILE
    echo 'Description=Tahoe-LAFS introducer' >> $TAHOELAFS_DAEMON_FILE
    echo 'After=syslog.target' >> $TAHOELAFS_DAEMON_FILE
    echo 'After=network.target' >> $TAHOELAFS_DAEMON_FILE
    echo '' >> $TAHOELAFS_DAEMON_FILE
    echo '[Service]' >> $TAHOELAFS_DAEMON_FILE
    echo 'Type=simple' >> $TAHOELAFS_DAEMON_FILE
    echo "User=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
    echo "Group=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
    echo "WorkingDirectory=/home/tahoelafs" >> $TAHOELAFS_DAEMON_FILE
    echo "ExecStart=venv/bin/tahoe start /home/tahoelafs/.tahoe-introducer" >> $TAHOELAFS_DAEMON_FILE
    echo "ExecStop=venv/bin/tahoe stop /home/tahoelafs/.tahoe-introducer" >> $TAHOELAFS_DAEMON_FILE
    echo 'Restart=on-failure' >> $TAHOELAFS_DAEMON_FILE
    echo "Environment=\"USER=tahoelafs\" \"HOME=/home/tahoelafs\"" >> $TAHOELAFS_DAEMON_FILE
    echo '' >> $TAHOELAFS_DAEMON_FILE
    echo '[Install]' >> $TAHOELAFS_DAEMON_FILE
    echo 'WantedBy=multi-user.target' >> $TAHOELAFS_DAEMON_FILE
    systemctl enable tahoelafs
    systemctl daemon-reload
    systemctl start tahoelafs
    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
