#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Recent version of guile
#
# License
# =======
#
# Copyright (C) 2017-2018 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/>.

GUILE_VERSION='2.2.0'
GUILE_HASH='c707b9cf6f97ecca3a4e3e704e62b83f95f1aec28ed1535f5d0a1d36af07a015'

EIGHTSYNC_REPO="git://git.savannah.gnu.org/8sync.git"
EIGHTSYNC_COMMIT='8cbb7f22227c0afdd3b0bd758ebec0efba2fa1e1'

function install_8sync {
    apt-get -qy install flex libunistring-dev libgc-dev autoconf texinfo

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

    git_clone $EIGHTSYNC_REPO $INSTALL_DIR/eightsync
    cd $INSTALL_DIR/eightsync
    git checkout ${EIGHTSYNC_COMMIT} -b ${EIGHTSYNC_COMMIT}

    export GUILE_BASE_PATH=/opt/guile-${GUILE_VERSION}
    export GUILE_CFLAGS="-I${GUILE_BASE_PATH}/include"
    export GUILE_LIBS="-L${GUILE_BASE_PATH}/lib -lguile -lqthreads -ldl -ltermcap -lsocket -lnsl -lm"
    ./bootstrap.sh
    sed -i 's|PKG_CHECK_MODULES|##PKG_CHECK_MODULES|g' configure
    configure
    make
    make install
    export GUILE_LOAD_COMPILED_PATH="$INSTALL_DIR/8sync"
}

function install_guile {
    # Currently this only works for x86_64
    read_config_param ARCHITECTURE
    if [[ ${ARCHITECTURE} != "x86_64" ]]; then
        return
    fi
    GUILE_ARCH='x86_64'

    apt-get -qy install flex libunistring-dev libgc-dev autoconf texinfo lzip wget

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

    if [ ! -f guile-${GUILE_VERSION}-pack-${GUILE_ARCH}-linux-gnu.tar.lz ]; then
        wget https://ftp.gnu.org/gnu/guile/guile-${GUILE_VERSION}-pack-${GUILE_ARCH}-linux-gnu.tar.lz
    fi
    if [ ! -f guile-${GUILE_VERSION}-pack-${GUILE_ARCH}-linux-gnu.tar.lz ]; then
        echo 'Unable to download guile pack'
        exit 6735238
    fi
    CURR_GUILE_HASH=$(sha256sum guile-${GUILE_VERSION}-pack-${GUILE_ARCH}-linux-gnu.tar.lz | awk -F ' ' '{print $1}')
    if [[ "$CURR_GUILE_HASH" != "$GUILE_HASH" ]]; then
        echo 'Guile hash does not match'
        exit 7237625
    fi
    cd /
    tar xvf $INSTALL_DIR/guile-${GUILE_VERSION}-pack-${GUILE_ARCH}-linux-gnu.tar.lz
    if [ ! -d /opt/guile-${GUILE_VERSION}/bin ]; then
        echo 'Guile was not installed'
        exit 825269
    fi
    echo "export GUILE_PATH=/opt/guile-${GUILE_VERSION}/bin" >> ~/.bashrc
    echo 'export PATH=$PATH:$GUILE_PATH' >> ~/.bashrc
}

# NOTE: deliberately no exit 0
