#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Mirror git repos which the project depends on
#
# License
# =======
#
# Copyright (C) 2015-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/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-mirrors
export TEXTDOMAINDIR="/usr/share/locale"

# Minimum number of characters in a password
MINIMUM_PASSWORD_LENGTH=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-passwords | grep 'MINIMUM_PASSWORD_LENGTH=' | head -n 1 | awk -F '=' '{print $2}')

CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"

# used to mirror a single application
SYNC_SINGLE_APP=

# if this is blank then just use the default repos
FRIENDS_MIRRORS_SERVER=
UTILS_REPOS=
INSTALLED_APPS_REPOS=
MY_MIRRORS_PASSWORD=
FRIENDS_MIRRORS_PASSWORD=
NEW_MIRRORS='no'
FRIENDS_MIRRORS_SSH_PORT=2222

MAIN_COMMAND=/usr/local/bin/${PROJECT_NAME}
if [ ! -f $MAIN_COMMAND ]; then
    MAIN_COMMAND=/usr/bin/${PROJECT_NAME}
fi

# local repos for utils
UTILS_REPOS=($(cat ${MAIN_COMMAND} /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-* /usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-* | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))

UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
for f in $UTILS_FILES
do
    source $f
done

# obtain the mirrors password if it exists
read_config_param MY_MIRRORS_PASSWORD
read_config_param FRIENDS_MIRRORS_SERVER
read_config_param FRIENDS_MIRRORS_PASSWORD
read_config_param FRIENDS_MIRRORS_SSH_PORT

function show_help {
    echo ''
    echo $"${PROJECT_NAME}-mirrors --sync [domain/url] -p [password]"
    echo ''
    echo $'Creates or syncs with a set of git repositories'
    echo ''
    echo $'     --help                   Show help'
    echo $'  -n|--new [yes|no]           Start a new mirrors'
    echo $"  -p|--password [password]    Friend's mirrors user password"
    echo $"  -m|--mypassword [password]  Local mirrors user password"
    echo $"     --port [number]          Friend's server ssh port number"
    echo $"  -s|--sync [domain]          Friend's server domain to sync with"
    echo ''
    exit 0
}

function create_mirrors_user {
    if [ -d /home/mirrors ]; then
        return
    fi

    create_password=1
    if [ ${#MY_MIRRORS_PASSWORD} -ge ${MINIMUM_PASSWORD_LENGTH} ]; then
        create_password=
    fi

    if [ $create_password ]; then
        MY_MIRRORS_PASSWORD="$(openssl rand -base64 20 | cut -c1-18)"
    fi

    chmod 600 /etc/shadow
    chmod 600 /etc/gshadow
    useradd -m -p "$MY_MIRRORS_PASSWORD" -s /bin/bash mirrors
    chmod 0000 /etc/shadow
    chmod 0000 /etc/gshadow

    # remove any existing user files
    rm -rf /home/mirrors/*

    # store the mirrors password
    write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
}

function enable_mirrors_via_onion {
    if ! grep -q 'Host *.onion' /home/mirrors/.ssh/config; then
        if [ ! -d /home/mirrors/.ssh ]; then
            mkdir /home/mirrors/.ssh
        fi
        echo 'Host *.onion' >> /home/mirrors/.ssh/config
        echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/mirrors/.ssh/config
        chown mirrors:mirrors /home/mirrors/.ssh
        chown mirrors:mirrors /home/mirrors/.ssh/config
    fi
}

function update_installed_single_repo {
    # only deal with a single app
    filename=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${SYNC_SINGLE_APP}
    if [ ! -f $filename ]; then
        echo $"The app $SYNC_SINGLE_APP was not found"
        exit 36822
    fi
    APP_REPOS=($(cat ${MAIN_COMMAND} $filename | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
    for line in "${APP_REPOS[@]}"
    do
        INSTALLED_APPS_REPOS+=("${line}")
    done
}

function update_installed_apps_repos {
    INSTALLED_APPS_REPOS=()

    function_check app_is_installed

    if [ $SYNC_SINGLE_APP ]; then
        update_installed_single_repo
        return
    fi

    # all apps currently installed
    FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*

    # for all the app scripts
    for filename in $FILES
    do
        app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
        if [[ "$(app_is_installed ${app_name})" == "1" ]]; then
            APP_REPOS=($(cat ${MAIN_COMMAND} $filename | grep "_REPO=\"" | grep -v "(cat " | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
            for line in "${APP_REPOS[@]}"
            do
                INSTALLED_APPS_REPOS+=("${line}")
            done
        fi
    done
}

function update_repos_from_friend_base {
    syncrepos=${1}
    new_repos=()
    for line in $syncrepos
    do
        repo_name=$(echo "$line" | awk -F '=' '{print $1}')
        mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
        friends_repo_url="ssh://mirrors@${FRIENDS_MIRRORS_SERVER}:${FRIENDS_MIRRORS_SSH_PORT}/home/mirrors/${mirrors_name}"
        new_line="${repo_name}=\"${friends_repo_url}\""
        new_repos+=($new_line)
    done
}

function update_repos_from_friend {
    if [ ! $FRIENDS_MIRRORS_SERVER ]; then
       return
    fi
    if [ ${#FRIENDS_MIRRORS_SERVER} -lt 2 ]; then
        return
    fi

    update_repos_from_friend_base "${UTILS_REPOS[@]}"
    UTILS_REPOS=("${new_repos[@]}")

    update_repos_from_friend_base "${INSTALLED_APPS_REPOS[@]}"
    INSTALLED_APPS_REPOS=("${new_repos[@]}")
}

function sync_mirrors_base {
    syncrepos=${1}
    for line in $syncrepos
    do
        repo_name=$(echo "$line" | awk -F '=' '{print $1}')
        repo_url=$(echo "$line" | awk -F '=' '{print $2}'  | awk -F '"' '{print $2}')
        mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
        if [[ ${mirrors_name} != 'debian' ]]; then
            if [[ $NEW_MIRRORS == 'yes' ]]; then
                if [ -d /home/mirrors/${mirrors_name} ]; then
                    rm -rf /home/mirrors/${mirrors_name}
                fi
            fi
            if [ ! -d /home/mirrors/${mirrors_name} ]; then
                if [[ ${repo_url} != 'ssh:'* ]]; then
                    git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
                else
                    sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git clone --mirror ${repo_url} /home/mirrors/${mirrors_name}
                fi
                if [ ! -d /home/mirrors/${mirrors_name} ]; then
                    echo $"WARNING: failed to mirror repo ${repo_url}"
                fi
            else
                cd /home/mirrors/${mirrors_name}
                git remote set-url origin ${repo_url}
                if [[ ${repo_url} != 'ssh:'* ]]; then
                    git fetch -p origin
                else
                    sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git fetch -p origin
                fi
            fi
        fi
    done
    chown -R mirrors:mirrors /home/mirrors
}

function sync_mirrors_repos {
    if [ ! $SYNC_SINGLE_APP ]; then
        sync_mirrors_base "${UTILS_REPOS[@]}"
    fi
    sync_mirrors_base "${INSTALLED_APPS_REPOS[@]}"
}

while [[ $# > 1 ]]
do
key="$1"

case $key in
    --help)
    show_help
    ;;
    -s|--sync)
    shift
    # use repos on another server
    FRIENDS_MIRRORS_SERVER="$1"
    ;;
    -m|--mypass|--mypassword)
    shift
    MY_MIRRORS_PASSWORD="$1"
    write_config_param "MY_MIRRORS_PASSWORD" "${MY_MIRRORS_PASSWORD}"
    ;;
    -p|--pass|--password)
    shift
    FRIENDS_MIRRORS_PASSWORD="$1"
    write_config_param "FRIENDS_MIRRORS_PASSWORD" "${FRIENDS_MIRRORS_PASSWORD}"
    ;;
    -n|--new)
    shift
    NEW_MIRRORS="$1"
    ;;
    --port)
    shift
    FRIENDS_MIRRORS_SSH_PORT=${1}
    ;;
    -a|--app)
    shift
    SYNC_SINGLE_APP="${1}"
    ;;
    *)
    # unknown option
    ;;
esac
shift
done

create_mirrors_user
enable_mirrors_via_onion
update_installed_apps_repos
update_repos_from_friend
sync_mirrors_repos

exit 0
