#!/bin/sh

# Which package manager the tree has.
#
# This fills `pkg.manager` with `zypper`, `dnf`, `apt-get` or `apk`, and it is
# the key that makes one core set work on more than one distribution: the `pkg`
# and `repo` providers pick their backend by looking for the same programs, and
# a fleet whose package is spelled differently in two families branches on this.
#
#     {% if pkg.manager == 'apt-get' %}openssh-server{% else %}openssh{% endif %}
#
# What is looked for is the program inside the tree, not the one on the machine
# asking, which is the same rule the providers follow and the reason an image
# being built reports its own manager.  It is looked for wherever a
# distribution puts it: `apk` is in `/sbin` on Alpine, the others in `/usr/bin`.
#
# `os.id` says what the distribution calls itself; this says how to
# install something on it.  They are not the same question, and a derivative
# usually keeps the second answer while changing the first.

set -eu

root=${DETC_ROOT:-/}

for manager in zypper dnf apt-get apk; do
    for dir in usr/bin usr/sbin bin sbin; do
        if [ -x "$root/$dir/$manager" ]; then
            printf 'manager: "%s"\n' "$manager"
            exit 0
        fi
    done
done

# A tree with none of them is not a tree packages are installed into, and saying
# so with a name would be inventing one
