#!/bin/bash
# ━━━━━━━━━━━━━━━
# flow-downstream - Flow downstream all newly commited changes of the present repository
# ━━━━━━━━━━━━━━━
#
# Automating the *publication* steps of `~/work/maintenance_manual.brec` for repositories
# both of`~/work/` and `~/code/WP3/` working directories.

    working_repo=`realpath $PWD` # This (command substitution) implicitly strips any trailing newline.
    project_repo=~/project/`project-proper-path`
    project_repo=`realpath $project_repo`
    echo
    if [ $project_repo != $working_repo ]; then
        # The project and working repositories differ.
        echo 'Go to project repo'
        echo '━━━━━━━━━━━━━'
        pushd $project_repo/ || exit 1
        echo

        echo 'Pull from working repo'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        git fetch origin master --verbose || exit 1
        read -p '...'
        echo

        echo 'Merge the changes'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        (git checkout master && git merge --ff-only FETCH_HEAD) || exit 1
        read -p '...'
        echo

        echo 'Rebase branch `reluk.ca` atop the merger'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        (git checkout reluk.ca && git rebase master) || exit 1
        read -p '...'
        echo

        echo 'Push to working repo the rebase'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        git push origin +reluk.ca --force-with-lease || exit 1
        read -p '...'
        echo

        echo 'Return to working repo'
        echo '━━━━━━━━━━━━━━━━━'
        popd || exit 1
        echo

        echo 'Push to public repo'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        git push public reluk.ca --force-with-lease || exit 1
        echo
    else # The `~/project/` repository is a mere soft-link to the `~/work/` repository.
        echo 'Push to public repo'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        fi
    git push public --branches || exit 1
      # needs also `--force-with-lease` if commits are to be clobbered (e.g. from rebase or deletion)
    if [ $working_repo == '/home/mike/work/MathJax/Emacs' ]; then
        read -p '...'
        echo
        echo 'Thence to Git Lab'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        (cd ~/var/repo/git/`project-token-name`.git/ && git push gitlab --mirror) || exit 1
  # elif [ $working_repo != '/home/mike/SECRET-REPO' ]; then
    else
        read -p '...'
        echo
        echo 'Thence to Git Hub'
        echo '┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈'
        (cd ~/var/repo/git/`project-token-name`.git/ && git push github --mirror) || exit 1
        fi
    echo