#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Useful git functions
#
# License
# =======
#
# Copyright (C) 2014-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/>.

function git_clone {
    repo_url="$1"
    destination_dir="$2"

    echo "git clone $repo_url $destination_dir"
    git clone --recursive "$repo_url" "$destination_dir"
}

function git_pull {
    if [ ! "$1" ]; then
        echo $'git_pull no repo specified'
    fi

    git merge --abort
    git stash
    git remote set-url origin "$1"
    git checkout master
    git pull

    if [ "$2" ]; then
        # delete any existing branch
        git branch -D "$2"
        # check out the new branch
        if ! git checkout "$2" -b "$2"; then
            echo $"Unable to checkout $1 $2"
            exit 72357
        fi
    fi
}

function commit_has_changed {
    repo_dir=$1
    repo_commit_name=$2
    repo_commit=$3
    if [ -d "$repo_dir" ]; then
        if grep -q "$repo_commit_name" "$COMPLETION_FILE"; then
            CURRENT_REPO_COMMIT=$(get_completion_param "$repo_commit_name")
            if [[ "$CURRENT_REPO_COMMIT" != "$repo_commit" ]]; then
                echo "1"
                return
            fi
        else
            echo "1"
            return
        fi
    fi
    echo "0"
}

# This ensures that a given repo is on a given commit
# If it isn't then it attempts to upgrade
function set_repo_commit {
    repo_dir=$1
    repo_commit_name=$2
    repo_commit=$3
    repo_url=$4

    if [[ $(commit_has_changed "$repo_dir" "$repo_commit_name" "$repo_commit") == "1" ]]; then
        cd "$repo_dir" || exit 3856835
        git_pull "$repo_url" "$repo_commit"

        # application specific stuff after updating the repo
        if [[ $repo_dir == *"www"* ]]; then
            chown -R www-data:www-data "$repo_dir"
        fi
        if [[ $repo_dir == *"gpgit" ]]; then
            if [ ! -f /usr/bin/gpgit.pl ]; then
                cp gpgit.pl /usr/bin/gpgit.pl
            else
                HASH1=$(sha256sum gpgit.pl | awk -F ' ' '{print $1}')
                HASH2=$(sha256sum /usr/bin/gpgit.pl | awk -F ' ' '{print $1}')
                if [[ "$HASH1" != "$HASH2" ]]; then
                    cp gpgit.pl /usr/bin/gpgit.pl
                fi
            fi
        fi
        if [[ $repo_dir == *"cleanup-maildir" ]]; then
            if [ ! -f /usr/bin/cleanup-maildir ]; then
                cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
            else
                HASH1=$(sha256sum "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" | awk -F ' ' '{print $1}')
                HASH2=$(sha256sum /usr/bin/cleanup-maildir | awk -F ' ' '{print $1}')
                if [[ "$HASH1" != "$HASH2" ]]; then
                    cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
                fi
            fi
        fi
        if [[ $repo_dir == *"nginx_ensite" ]]; then
            if [ ! -f /usr/local/bin/nginx_ensite ]; then
                make install
            fi
        fi
        if [[ $repo_dir == *"inadyn" ]]; then
            ./configure
            USE_OPENSSL=1 make
            make install
            systemctl restart inadyn
        fi

        function_check set_completion_param
        set_completion_param "${repo_commit_name}" "${repo_commit}"
    fi
}

function configure_firewall_for_git {
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
        return
    fi
    if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
        # docker does its own firewalling
        return
    fi
    if [[ $ONION_ONLY != "no" ]]; then
        return
    fi

    firewall_add Git 9418 tcp
    mark_completed "${FUNCNAME[0]}"
}

# NOTE: deliberately no exit 0