#!/bin/bash

# Publish installer language packages at gitlab.
# Merges newly generated installer strings into a local clone of moodle.git and pushes changes into a remote repository.
# upstream: install-packs-make

if [[ -z ${AMOSDATAROOT} ]]; then
    echo "Error: AMOSDATAROOT not set!"
    exit 1
fi

LOCKFILE=${AMOSDATAROOT}/install-packs-publish.lock
REPO=${AMOSDATAROOT}/repos/moodle-install/
SRCROOT=${AMOSDATAROOT}/export-install/

if set -C; echo $$ 2>/dev/null > ${LOCKFILE}; then
    trap on_exit INT TERM EXIT
else
    echo "Lock ${LOCKFILE} exists!"
    exit 1
fi

##
# To be executed on exiting.
#
on_exit() {
    rm -f ${LOCKFILE}
}

##
# Make sure that all required branches exist and are up to date.
#
# param $1 - name of the upstream branch such as master or MOODLE_311_STABLE
# param $2 - name of the install branch such as install_master or install_311_STABLE
#
prepare_branches() {
    local upstreambranch=$1
    local installbranch=$2

    cd $REPO

    if git show-ref --quiet --verify refs/heads/${upstreambranch}; then
        git checkout $upstreambranch
        git merge --ff-only upstream/${upstreambranch}
    else
        git checkout --no-guess -b ${upstreambranch} --track upstream/${upstreambranch}
    fi

    if git show-ref --quiet --verify refs/heads/${installbranch}; then
        git checkout ${installbranch}
    else
        git checkout --no-guess -b ${installbranch} --no-track upstream/${upstreambranch}
    fi

	GIT_AUTHOR_NAME="AMOS bot" GIT_AUTHOR_EMAIL="amos@moodle.org" GIT_COMMITTER_NAME="AMOS bot" GIT_COMMITTER_EMAIL="amos@moodle.org" \
        git merge ${upstreambranch}
}

##
# Commit new install strings to the install branch.
#
# param $1 - name of the directory inside ${SRCROOT} to process such as master or 311_STABLE
# param $2 - name of the install branch such as install_master or install_311_STABLE
#
commit_install_strings() {
    local dirname=$1
    local installbranch=$2

	cd ${REPO}
	git checkout ${installbranch}

	cd ${REPO}/install
	rm -rf lang
	cp -r ${AMOSDATAROOT}/export-install/${dirname}/install/lang .
	git add .
	GIT_AUTHOR_NAME="AMOS bot" GIT_AUTHOR_EMAIL="amos@moodle.org" GIT_COMMITTER_NAME="AMOS bot" GIT_COMMITTER_EMAIL="amos@moodle.org" \
        git commit -a -m "Automatically generated installer lang files"
}

##
# Push branches to remotes
#
# param $1 - name of the upstream branch such as master or MOODLE_311_STABLE
# param $2 - name of the install branch such as install_master or install_311_STABLE
#
publish_branches() {
    local upstreambranch=$1
    local installbranch=$2

    git push gitlab ${upstreambranch}:${upstreambranch}
    git push gitlab ${installbranch}:${installbranch}
}

# Execution starts here.

cd $REPO
git remote update --prune

for DIR in ${SRCROOT}/*; do
    if [[ -d ${DIR} ]]; then
        DIRNAME=$(basename ${DIR})
        if [[ ${DIRNAME} = "master" ]]; then
            UPSTREAMBRANCH="master"
            INSTALLBRANCH="install_master"
        else
            UPSTREAMBRANCH="MOODLE_${DIRNAME}"
            INSTALLBRANCH="install_${DIRNAME}"
        fi

        prepare_branches ${UPSTREAMBRANCH} ${INSTALLBRANCH}
        commit_install_strings ${DIRNAME} ${INSTALLBRANCH}
        publish_branches ${UPSTREAMBRANCH} ${INSTALLBRANCH}
    fi
done

cd ${REPO}
git gc
