#!/bin/sh
#
# singlelift - run a single Subversion conversion test

# shellcheck disable=SC1091
. ./common-setup.sh

# Use absolute path so tests that change working directory still use 
# scripts from parent directory.  Note that using $PWD seems to fail
# here under Gitlab's CI environment.
PATH=$(realpath ..):$(realpath .):${PATH}

mode=check
log=""
experimental=""
while getopts bhl:osx opt
do
    case $opt in
	b) mode=build;;
	l) log="log $OPTARG" ;;
	o) mode=dump;;
	s) serial="set serial";;
	x) experimental="set experimental" ;;
	*) cat <<EOF
singlelift - run a single Subversion conversion test

With -b, rebuild the checkfile

With -h, display this option summary.

With -l, set reposurgeon log flags (dump mode only).

With -s, force serial execution - disables parallelism.

With -o, dump the conversion to stdout rather than diffing against
the check file.

With -x, set the experimental flag.

The REPOSURGEON environment variable can be used to substitute in a
different implementation.
EOF
	   exit 0;;
    esac
done
# shellcheck disable=SC2004
shift $(($OPTIND - 1))

# Display usage and exit if no arguments
if [ -z "$1" ]
then
    exec "$0" -h
fi

# shellcheck disable=SC2068
for x in $@;
do
    case $x in
	*.svn) x=$(echo "$x" | sed '/.svn/s///');;
    esac
    # This redirect to stderr is so this script can be used to rebuild checkfiles
    (grep '^#' "${x}" 2>/dev/null) 1>&2
    case $mode in
	dump)
	    ${REPOSURGEON:-reposurgeon} "$log" "$serial" "$experimental" "read <${x}.svn" "prefer git" "write -" 2>&1;;
	build)
	    ${REPOSURGEON:-reposurgeon} "$log" "$serial" "$experimental" "read <${x}.svn" "prefer git" "write -" >"${x}.chk" 2>&1 ;;
	*)
	    ${REPOSURGEON:-reposurgeon} "$serial" "$experimental" "read <${x}.svn" "prefer git" "write -" 2>&1 | diff -u "${x}.chk" - ;;
    esac
done

# Return exit status of the last command to run.
# In particular, if the last command was a diff,
# this will return 0 for empty and 1 for nonempty.
# Otherwise you'll typically get the exit status
# of reposurgeon. 
exit $?

# end
