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

PROTOBUF_REPO="https://github.com/google/protobuf"
PROTOBUF_COMMIT='b97a4a53cdd55be74c30badefeb132a091764f53'

LIBREVAULT_REPO="https://github.com/Librevault/librevault"
LIBREVAULT_COMMIT='86a6aefcb5cc458f4d42195368fbcff2871f98e3'
LIBREVAULT_PORT=42345

librevault_variables=(PROTOBUF_REPO
                      PROTOBUF_COMMIT
                      LIBREVAULT_REPO
                      LIBREVAULT_COMMIT
                      LIBREVAULT_PORT)

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

function change_password_librevault {
    echo -n ''
}

function reconfigure_librevault {
    echo -n ''
    # TODO
}

function upgrade_librevault {
    function_check set_repo_commit

    if [ -d $INSTALL_DIR/protobuf ]; then
        if ! grep -q "Protobuf commit:$PROTOBUF_COMMIT" $COMPLETION_FILE; then
            set_repo_commit $INSTALL_DIR/protobuf "Protobuf commit" "$PROTOBUF_COMMIT" $PROTOBUF_REPO
            cd $INSTALL_DIR/protobuf
            ./autogen.sh
            ./configure
            make
            make check
            make install
            ldconfig
        fi
    fi

    if [ -d $INSTALL_DIR/librevault/build ]; then
        if ! grep -q "Librevault commit:$LIBREVAULT_COMMIT" $COMPLETION_FILE; then
            set_repo_commit $INSTALL_DIR/librevault "Librevault commit" "$LIBREVAULT_COMMIT" $LIBREVAULT_REPO
            cd $INSTALL_DIR/librevault
            git submodule update --init --recursive
            cd $INSTALL_DIR/librevault/build
            cmake ..
            cmake --build .
            make install
        fi
    fi
}

function backup_local_librevault {
    echo -n ''
    # TODO
}

function restore_local_librevault {
    echo -n ''
    # TODO
}

function backup_remote_librevault {
    echo -n ''
    # TODO
}

function restore_remote_librevault {
    echo -n ''
    # TODO
}

function remove_librevault {
    firewall_remove ${LIBREVAULT_PORT}
    systemctl stop librevault
    systemctl disable librevault
    rm /etc/systemd/system/librevault.service
    remove_completion_param install_librevault
    remove_completion_param configure_firewall_for_librevault
}

function configure_firewall_for_librevault {
    if [[ $(is_completed $FUNCNAME) == "1" ]]; then
        return
    fi
    firewall_add Librevault ${LIBREVAULT_PORT}
    mark_completed $FUNCNAME
}

function mesh_install_librevault {
    install_tracker
    install_cmake
    install_cryptopp

    chroot "$rootdir" apt-get -yq install bittornado nginx
    chroot "$rootdir" apt-get -yq install build-essential
    chroot "$rootdir" apt-get -yq install autoconf automake libtool curl make unzip

    # A workaround which allows c++14 to be installed
    sed -i 's|jessie|stretch|g' $rootdir/etc/apt/sources.list
    sed -i 's|stretch-backports|jessie-backports|g' $rootdir/etc/apt/sources.list
    chroot "$rootdir" apt-get update
    chroot "$rootdir" apt-get -yq install g++ gcc-6 g++-6 libboost-all-dev libssl-dev
    chroot "$rootdir" apt-get -yq install protobuf-compiler libprotobuf-dev
    chroot "$rootdir" apt-get -yq install qtbase5-dev libqt5svg5-dev libqt5websockets5-dev
    chroot "$rootdir" apt-get -yq install libsqlite3-dev qttools5-dev qttools5-dev-tools libnatpmp-dev
    sed -i 's|stretch|jessie|g' $rootdir/etc/apt/sources.list
    chroot "$rootdir" apt-get update

    if [ ! -d $rootdir$INSTALL_DIR ]; then
        mkdir -p $rootdir$INSTALL_DIR
    fi

    git clone $PROTOBUF_REPO $rootdir$INSTALL_DIR/protobuf
    cd $rootdir$INSTALL_DIR/protobuf
    git checkout $PROTOBUF_COMMIT -b $PROTOBUF_COMMIT

    cat <<EOF > $rootdir/root/install_protobuf
cd $INSTALL_DIR/protobuf
./autogen.sh
./configure
make
make install
ldconfig
EOF
    chroot "$rootdir" chmod +x /root/install_protobuf
    chroot "$rootdir" /root/install_protobuf

    git clone $LIBREVAULT_REPO $rootdir$INSTALL_DIR/librevault
    cd $rootdir$INSTALL_DIR/librevault
    git checkout $LIBREVAULT_COMMIT -b $LIBREVAULT_COMMIT
    git submodule update --init --recursive

    # rabin seems particularly broken
    rm -rf $rootdir$INSTALL_DIR/librevault/contrib/rabin
    git clone https://github.com/bashrc/rabin $rootdir$INSTALL_DIR/librevault/contrib/rabin
    cd $rootdir$INSTALL_DIR/librevault/contrib/rabin
    git checkout bashrc/fixup2

    mkdir $rootdir$INSTALL_DIR/librevault/build

    cat <<EOF > $rootdir/root/install_librevault
cd $INSTALL_DIR/librevault/build
cmake -BUILD_UPDATER=OFF ..
which Meta_s.pb.cc
cmake --build $INSTALL_DIR/librevault/build
make install
EOF
    chroot "$rootdir" chmod +x /root/install_librevault
    chroot "$rootdir" /root/install_librevault
    chroot "$rootdir" rm /root/install_librevault

    LIBREVAULT_DAEMON=$rootdir/etc/systemd/system/librevault-daemon.service
    echo '[Unit]' > $LIBREVAULT_DAEMON
    echo 'Description=Librevault - Peer-to-peer, decentralized and open source file sync (multi-instance-master)' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Service]' >> $LIBREVAULT_DAEMON
    echo 'Type=oneshot' >> $LIBREVAULT_DAEMON
    echo 'RemainAfterExit=yes' >> $LIBREVAULT_DAEMON
    echo 'ExecStart=/bin/true' >> $LIBREVAULT_DAEMON
    echo 'ExecReload=/bin/true' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Install]' >> $LIBREVAULT_DAEMON
    echo 'WantedBy=multi-user.target' >> $LIBREVAULT_DAEMON

    LIBREVAULT_DAEMON=$rootdir/etc/systemd/system/librevault-daemon@.service
    echo '[Unit]' > $LIBREVAULT_DAEMON
    echo 'Description=Librevault - Peer-to-peer, decentralized and open source file sync (instance for %I)' >> $LIBREVAULT_DAEMON
    echo 'After=network.target' >> $LIBREVAULT_DAEMON
    echo 'PartOf=librevault-daemon.service' >> $LIBREVAULT_DAEMON
    echo 'ReloadPropagatedFrom=librevault-daemon.service' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Service]' >> $LIBREVAULT_DAEMON
    echo 'User=%i' >> $LIBREVAULT_DAEMON
    echo 'ExecStart=/usr/bin/librevault-daemon' >> $LIBREVAULT_DAEMON
    echo 'Restart=on-failure' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Install]' >> $LIBREVAULT_DAEMON
    echo 'WantedBy=multi-user.target' >> $LIBREVAULT_DAEMON

    chroot "$rootdir" systemctl enable librevault-daemon
}

function install_librevault {
    if [ $INSTALLING_MESH ]; then
        mesh_install_librevault
        return
    fi

    install_tracker
    install_cmake
    install_cryptopp

    apt-get -yq install build-essential
    apt-get -yq install autoconf automake libtool curl make unzip

    # A workaround which allows c++14 to be installed
    sed -i 's|jessie|stretch|g' /etc/apt/sources.list
    sed -i 's|stretch-backports|jessie-backports|g' /etc/apt/sources.list
    apt-get update
    apt-get -yq install protobuf-compiler libprotobuf-dev
    apt-get -yq install g++ gcc-6 g++-6 libboost-all-dev libssl-dev
    apt-get -yq install qtbase5-dev libqt5svg5-dev libqt5websockets5-dev
    apt-get -yq install libsqlite3-dev qttools5-dev qttools5-dev-tools libnatpmp-dev
    apt-get -yq install gcc-6 g++-6 libboost-all-dev
    sed -i 's|stretch|jessie|g' /etc/apt/sources.list
    apt-get update

    if [ ! -d $INSTALL_DIR ]; then
        mkdir -p $INSTALL_DIR
    fi
    git_clone $PROTOBUF_REPO $INSTALL_DIR/protobuf
    cd $INSTALL_DIR/protobuf
    git checkout $PROTOBUF_COMMIT -b $PROTOBUF_COMMIT
    ./autogen.sh
    ./configure
    make
    make check
    make install
    ldconfig

    set_completion_param "Protobuf commit" "$PROTOBUF_COMMIT"

    git_clone $LIBREVAULT_REPO $INSTALL_DIR/librevault
    cd $INSTALL_DIR/librevault
    git checkout $LIBREVAULT_COMMIT -b $LIBREVAULT_COMMIT
    git submodule update --init --recursive

    # rabin seems particularly broken
    rm -rf $INSTALL_DIR/librevault/contrib/rabin
    git clone https://github.com/bashrc/rabin $INSTALL_DIR/librevault/contrib/rabin
    cd $INSTALL_DIR/librevault/contrib/rabin
    git checkout bashrc/fixup2

    mkdir $INSTALL_DIR/librevault/build
    cd $INSTALL_DIR/librevault/build
    cmake -BUILD_UPDATER=OFF ..
    cmake --build .
    make install

    set_completion_param "Librevault commit" "$LIBREVAULT_COMMIT"

    LIBREVAULT_DAEMON=/etc/systemd/system/librevault-daemon.service
    echo '[Unit]' > $LIBREVAULT_DAEMON
    echo 'Description=Librevault - Peer-to-peer, decentralized and open source file sync (multi-instance-master)' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Service]' >> $LIBREVAULT_DAEMON
    echo 'Type=oneshot' >> $LIBREVAULT_DAEMON
    echo 'RemainAfterExit=yes' >> $LIBREVAULT_DAEMON
    echo 'ExecStart=/bin/true' >> $LIBREVAULT_DAEMON
    echo 'ExecReload=/bin/true' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Install]' >> $LIBREVAULT_DAEMON
    echo 'WantedBy=multi-user.target' >> $LIBREVAULT_DAEMON

    LIBREVAULT_DAEMON=/etc/systemd/system/librevault-daemon@.service
    echo '[Unit]' > $LIBREVAULT_DAEMON
    echo 'Description=Librevault - Peer-to-peer, decentralized and open source file sync (instance for %I)' >> $LIBREVAULT_DAEMON
    echo 'After=network.target' >> $LIBREVAULT_DAEMON
    echo 'PartOf=librevault-daemon.service' >> $LIBREVAULT_DAEMON
    echo 'ReloadPropagatedFrom=librevault-daemon.service' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Service]' >> $LIBREVAULT_DAEMON
    echo 'User=%i' >> $LIBREVAULT_DAEMON
    echo 'ExecStart=/usr/bin/librevault-daemon' >> $LIBREVAULT_DAEMON
    echo 'Restart=on-failure' >> $LIBREVAULT_DAEMON
    echo '' >> $LIBREVAULT_DAEMON
    echo '[Install]' >> $LIBREVAULT_DAEMON
    echo 'WantedBy=multi-user.target' >> $LIBREVAULT_DAEMON

    systemctl enable librevault-daemon
    systemctl start librevault-daemon
    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
