#!/bin/bash
#
# DEPRECATED backward-compatible wrapper - use the real command instead:
#
#   $> reposmgr reclone-repo <repo-name>
#
# All the logic lives in the "reposmgr" Python CLI; this only forwards
# to it. New subcommands do not get a wrapper - only the ones that
# existed as bin/reposmgr-* scripts before the rewrite have one.
#
# "reposmgr" is looked for next to this script first, so the wrapper
# works when it is installed in a virtualenv that has not been
# activated - that bin/ holds both this script and the console script.
#
# Everything here is a bash builtin except readlink, so the wrapper
# still resolves itself when PATH is minimal or wrong - which is
# exactly the situation it exists to survive.
#
set -e

dir_of () {
    case "$1" in
        */*) printf '%s\n' "${1%/*}" ;;
        *) printf '%s\n' "." ;;
    esac
}

# This script's real directory, following symlinks ("readlink -f" is
# GNU-only, so resolve the chain by hand for macOS/BSD).
SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
    SOURCE_DIR="$(cd -P "$(dir_of "$SOURCE")" && pwd)"
    TARGET="$(readlink "$SOURCE")" || break
    case "$TARGET" in
        /*) SOURCE="$TARGET" ;;
        *) SOURCE="$SOURCE_DIR/$TARGET" ;;
    esac
done
HERE="$(cd -P "$(dir_of "$SOURCE")" && pwd)"

if [ -z "${REPOSMGR_NO_DEPRECATION_WARNING:-}" ]; then
    echo "[WARNING] ${0##*/} is deprecated, use: reposmgr reclone-repo" >&2
    echo "[WARNING] silence this with REPOSMGR_NO_DEPRECATION_WARNING=1" >&2
fi

if [ -x "$HERE/reposmgr" ]; then
    exec "$HERE/reposmgr" reclone-repo "$@"
fi

if command -v reposmgr > /dev/null 2>&1; then
    exec reposmgr reclone-repo "$@"
fi

echo "[ERROR] cannot find the \"reposmgr\" command." >&2
echo "        Looked next to this script ($HERE), then on PATH." >&2
echo "        Activate the virtualenv reposmgr is installed in, or call" >&2
echo "        it directly: <venv>/bin/reposmgr reclone-repo" >&2
exit 127
