# Read parameters
REMOTE_NAME=$1
REMOTE_URL=$2
# Read the input and split it approprietly
INPUT=$(cat)
COUNTER=0
for WORD in $INPUT; do
    case $COUNTER in
        0)
            LOCAL_REF=$WORD
            ;;
        1)
            LOCAL_OBJ_NAME=$WORD
            ;;
        2)
            REMOTE_REF=$WORD
            ;;
        3)
            REMOTE_OBJ_NAME=$WORD
            ;;
    esac
    COUNTER=$((COUNTER + 1))
done

case "$LOCAL_REF" in
    refs\/tags*)
        # Apply specific code when publishing tags
        IS_TAG=1
        ;;
    *)
        # Apply specific code when publishing any branch
        IS_TAG=0
        ;;
esac

# Set failing on command fail, and undefined variable use
set -eu

# This hook is called by git-push and can be used to prevent a
# push from taking place. The hook is called with two parameters
# which provide the name and location of the destination remote,
# if a named remote is not being used both values will be the same.

# Information about what is to be pushed is provided on the hook's
# standard input with lines of the form:
# <local ref> SP <local object name> SP <remote ref> SP <remote object name> LF

# For instance, if the command git push origin master:foreign were
# run the hook would receive a line like the following:
# refs/heads/master 67890 refs/heads/foreign 12345

# although the full object name would be supplied. If the foreign ref
# does not yet exist the <remote object name> will be the all-zeroes
# object name. If a ref is to be deleted, the <local ref> will be
# supplied as (delete) and the <local object name> will be the
# all-zeroes object name. If the local commit was specified by
# something other than a name which could be expanded
# (such as HEAD~, or an object name) it will be supplied as it was
# originally given.

# If this hook exits with a non-zero status, git push will abort without
# pushing anything. Information about why the push is rejected may be
# sent to the user by writing to standard error.

# We currently use this hook to verify that any commit (to any branch)
# passes the tests configured.
# If a tag is being pushed, then, we generate the documentation
# and publish the generated folder to the corresponding docs branch
# on the origin remote.
# This particular project also updated the version of the library
# to match that of the tag.

# Show welcome message
echo "**************************"
echo "Running pre push hooks"
echo "**************************"
echo ""

# Run all the tests
echo "Running tests"
npx --no -- gobstones-scripts run test --silent

exit 0
