#!/bin/sh
#
# Test reposurgeon branch naming issue with hg repo including merge and tags
#
# This test cannot use the usual hg-to-fi script because it
# needs an hg repo with actual hg branches, not hg bookmarks;
# the hg convert utility converts git branches in a fast-import
# stream to hg bookmarks, so the hg-regress test target only
# tests correct handling of hg bookmarks, not hg branches
#
# The REPOSURGEON environment variable can be used to substitute in a
# different implementation.

# shellcheck disable=SC2086
cd "$(readlink -f "$(dirname $0)")" || ( echo "$0: cd failed"; exit 1 )
# shellcheck disable=SC1091
. ./common-setup.sh

need hg

# Required because $PWD seems to be undefined in Gitlab's CI environment
BIN="${BINDIR:-$(realpath ..)}"

build=True
stream=True
cleanup=True

pecho() { printf %s\\n "$*"; }
log() { pecho "$@"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$@"; exit 1; }
try() { "$@" || fatal "'$*' failed"; }

while getopts nr opt
do
    case $opt in
    n) build=True; stream=False ; cleanup=False ;;
    r) build=False; stream=True  ; cleanup=False ;;
    *) echo "$0: unknown option $opt"; exit 1;;
    esac
done
# shellcheck disable=SC2004
shift $(($OPTIND - 1))

testrepo=${1:-/tmp/test-repo$$}

USER='"J. Random Hacker" <jrh@foobar.com>'

# Should we build the repo?
if [ $build = True ]
then
    # Build hg test repo with multiple hg branches
    try rm -fr $testrepo
    try hg init "$testrepo" || exit 1
    try cd "$testrepo" >/dev/null
    # The weird --date incantation in the hg commits is to ensure that the commit
    # timestamps match those in the .fi file; the 18000 is because hg wants the time zone
    # offset in seconds west of UTC, for what reason I know not--I know there are weird
    # time zones in the world but I didn't think any of them got down to one-second
    # granularity in offsets...
    # shellcheck disable=2094
    (
	# shellcheck disable=SC2238
        try echo tail > tail
        try hg add tail
        try hg commit --user "$USER" --date "1456976347 18000" -m 'Tail' > /dev/null
	try hg init nest
	try echo nest = this/should/never/be/a/valid/path > .hgsub
	try hg add .hgsub
        try hg commit --user "$USER" --date "1456976347 18000" -m 'Add Subrepo' > /dev/null
	try echo subfile > nest/subfile
	try hg add -R nest nest/subfile
        try hg commit -S --user "$USER" --date "1456976347 18000" -m 'Subrepo Commit' > /dev/null
	try hg update -C null >/dev/null
        try cd "$testrepo" >/dev/null     # Workaround for error getting current working directory
	try rm -rf nest # make the subrepo unavailable
    ) || exit 1
    try cd - >/dev/null
fi

# Should we stream the repo?
if [ $stream = True ]
then
    echo "#reposurgeon fescription: from an hg repo, trying out subrepo feature."
    # shellcheck disable=SC2086
    "${BIN}/${REPOSURGEON:-reposurgeon}" "${BUILDOPT}" ${TESTOPT} "set flag quiet" "read $testrepo" "select git" "write -"
fi

# Should we clean up the test directory
if [ $cleanup = True ]
then
    try rm -fr $testrepo
fi
