#!/usr/bin/sh
# lamboot-inspect-alternative register|unregister DIR
#
# Two packages install lamboot-inspect: LamBoot's own, and lamboot-tools,
# which mirrors it. Neither owns /usr/bin/lamboot-inspect or its man page;
# each keeps its copy in its own directory (DIR) and its package scripts call
# this to put the command on PATH. So both install together, the newer copy
# is the one that runs, and removing either leaves the other working.
#
# Where update-alternatives exists (Debian, Fedora, openSUSE) it does the
# work, with the copy's version as its priority. Where it doesn't (Arch) the
# links are made here, by the same rule.
#
# ROOT, when set, is prefixed to every path. It exists for the tests.
set -eu

action="${1:-}"
dir="${2:-}"
root="${ROOT:-}"
link=/usr/bin/lamboot-inspect
man_link=/usr/share/man/man1/lamboot-inspect.1.gz
known_dirs="/usr/share/lamboot /usr/lib/lamboot-tools"

case "$action" in register|unregister) ;; *) echo "usage: $0 register|unregister DIR" >&2; exit 2 ;; esac
[ -n "$dir" ] || { echo "usage: $0 register|unregister DIR" >&2; exit 2; }

# 0.17.3 -> 1703: the newer copy gets the higher priority.
priority_of() {
    v=$(sed -n 's/^__version__ = "\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)".*/\1 \2 \3/p' \
        "${root}$1/lamboot_inspect/__init__.py" 2>/dev/null | head -1)
    [ -n "$v" ] || { echo 0; return; }
    # shellcheck disable=SC2086
    set -- $v
    echo $(( $1 * 10000 + $2 * 100 + $3 ))
}

have_alternatives() {
    [ -z "$root" ] && command -v update-alternatives >/dev/null 2>&1
}

# Without update-alternatives: point the links at the newest copy present,
# leaving out `skip` (the copy being removed). No copy left: no links.
relink_by_hand() {
    skip="${1:-}"
    best=""; best_prio=-1
    for d in $known_dirs; do
        [ "$d" = "$skip" ] && continue
        [ -x "${root}${d}/lamboot-inspect" ] || continue
        p=$(priority_of "$d")
        if [ "$p" -gt "$best_prio" ]; then best="$d"; best_prio="$p"; fi
    done
    if [ -z "$best" ]; then
        [ -L "${root}${link}" ] && rm -f "${root}${link}"
        [ -L "${root}${man_link}" ] && rm -f "${root}${man_link}"
        return 0
    fi
    mkdir -p "${root}$(dirname "$link")" "${root}$(dirname "$man_link")"
    # A real file here belongs to someone else; it is not replaced.
    if [ ! -e "${root}${link}" ] || [ -L "${root}${link}" ]; then
        ln -sfn "${best}/lamboot-inspect" "${root}${link}"
    fi
    if [ -f "${root}${best}/man/lamboot-inspect.1.gz" ]; then
        if [ ! -e "${root}${man_link}" ] || [ -L "${root}${man_link}" ]; then
            ln -sfn "${best}/man/lamboot-inspect.1.gz" "${root}${man_link}"
        fi
    fi
}

case "$action" in
    register)
        [ -x "${root}${dir}/lamboot-inspect" ] || { echo "$0: no lamboot-inspect in $dir" >&2; exit 1; }
        if have_alternatives; then
            if [ -f "${dir}/man/lamboot-inspect.1.gz" ]; then
                update-alternatives --install "$link" lamboot-inspect "${dir}/lamboot-inspect" "$(priority_of "$dir")" \
                    --slave "$man_link" lamboot-inspect.1.gz "${dir}/man/lamboot-inspect.1.gz" >/dev/null
            else
                update-alternatives --install "$link" lamboot-inspect "${dir}/lamboot-inspect" "$(priority_of "$dir")" >/dev/null
            fi
        else
            relink_by_hand
        fi
        ;;
    unregister)
        if have_alternatives; then
            update-alternatives --remove lamboot-inspect "${dir}/lamboot-inspect" >/dev/null 2>&1 || true
        else
            relink_by_hand "$dir"
        fi
        ;;
esac
exit 0
