#!/bin/sh
#
# Compatibility shim for the location the helper programs occupied up to 0.16.0.
#
# get-remote and updateconf used to be installed in /usr/share/waarp-transfer.
# They are compiled binaries, so /usr/share was the wrong place for them and
# both lintian and rpmlint reject it. They now live in the packager's libexec
# directory, and this script keeps the old absolute paths working for rules and
# integration scripts that hardcoded them.
#
# TO BE REMOVED IN 1.0. Until then the paths above stay functional, per
# semantic versioning. See doc/source/changelog.rst.
#
# The same file is installed under both old names; it works out which helper is
# wanted from its own name.
#
# Three things here are load-bearing and must not be "cleaned up":
#
#   * `exec`, not a plain call. Waarp's EXEC, EXECMOVE and EXECOUTPUT tasks
#     halt a timed-out program by signalling the DIRECT CHILD only (haltExec,
#     pkg/tasks/exec_unix.go in the waarp-gateway dependency). `exec` keeps the
#     PID, so the signal reaches the real binary. Without it SIGINT lands on
#     this shell, which ignores it while waiting on its child; haltExec never
#     escalates to SIGKILL, cmd.Wait() never returns, and the task goroutine
#     hangs forever with the helper leaked.
#
#   * The warning goes to stderr and never to stdout. EXECMOVE reads the last
#     line of stdout as the transferred file's new path, and EXECOUTPUT parses
#     it for a NEWFILENAME: prefix; a line on stdout would corrupt both.
#
#   * "$@" stays quoted. Waarp has already split the arguments with shlex, so
#     an unquoted $@ would re-split any path or option containing a space.
#

# Parameter expansion rather than basename(1): this script is exec'd once per
# transfer for rules still on the old paths, and the fork is a large share of
# everything it does before handing over.
name=${0##*/}

for dir in /usr/libexec/waarp-transfer /usr/lib/waarp-transfer; do
    if [ -x "$dir/$name" ]; then
        echo "WARNING: /usr/share/waarp-transfer/$name is deprecated and will be removed" >&2
        echo "         in Waarp Transfer 1.0. The program now lives in $dir/$name." >&2
        echo "         Prefer invoking it as plain \"$name\": that resolves correctly on" >&2
        echo "         package, portable and container installations alike." >&2

        exec "$dir/$name" "$@"
    fi
done

echo "ERROR: $name was found in neither /usr/libexec/waarp-transfer nor" >&2
echo "       /usr/lib/waarp-transfer. The installation looks broken." >&2

exit 127
