#!/bin/sh
set -e

if [ "$1" = "configure" ]; then
    # On initial installation (incus user not yet created), setup idmaps.
    if ! getent passwd incus >/dev/null; then
        [ ! -e /etc/subuid ] && touch /etc/subuid
        [ ! -e /etc/subgid ] && touch /etc/subgid

        # Manage subuid/subgid allocation for containers user namespaces
        # Based on LXD Debian package logic (https://salsa.debian.org/go-team/packages/lxd/-/blob/debian/sid/debian/lxd.preinst)
        #
        # Run in a sub-shell to avoid messing with the script arguments.
        (
            FOUND_UID=0
            NEXT_UID=1000000
            while read line; do
                OLD_IFS=$IFS
                IFS=":"
                set -- $line
                IFS=$OLD_IFS
                [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] && continue

                [ "$1" = "root" ] && [ "$3" -ge 65536 ] && FOUND_UID=1

                LAST=$(($2+$3))
                [ "$LAST" -gt "$NEXT_UID" ] && NEXT_UID=$LAST
            done < /etc/subuid

            FOUND_GID=0
            NEXT_GID=1000000
            while read line; do
                OLD_IFS=$IFS
                IFS=":"
                set -- $line
                IFS=$OLD_IFS
                [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] && continue

                [ "$1" = "root" ] && [ "$3" -ge 65536 ] && FOUND_GID=1

                LAST=$(($2+$3))
                [ "$LAST" -gt "$NEXT_GID" ] && NEXT_GID=$LAST
            done < /etc/subgid

            if [ "${FOUND_UID}" = "0" ] || [ "${FOUND_GID}" = "0" ]; then
                usermod --add-subuids ${NEXT_UID}-$(($NEXT_UID+999999999)) root
                usermod --add-subgids ${NEXT_GID}-$(($NEXT_GID+999999999)) root
            fi
        )
    fi

    # Apply the sysctls.
    [ -d /run/systemd/system ] && systemctl restart systemd-sysctl || true

    # Cleanup old systemd units.
    for unit in incus.service incus.socket incus-user.service incus-user.socket incus-startup.service incus-lxcfs.service; do
        [ -L "/etc/systemd/system/${unit}" ] || continue
        [ "$(readlink -f "/etc/systemd/system/${unit}")" = "/dev/null" ] && continue

        rm -f "/etc/systemd/system/${unit}"
    done

    # Cleanup old sysctl.d.
    rm -f /etc/sysctl.d/50-incus.conf
fi

# Automatically added by dh_installsysusers/13.11.4
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
   systemd-sysusers ${DPKG_ROOT:+--root="$DPKG_ROOT"} incus-base.conf
fi
# End automatically added section


if [ "$1" = "configure" ]; then
    # Apply systemd units (done after systemd-sysusers has run).
    if [ -d /run/systemd/system ]; then
        # On running systems, enable and start the units.
        for unit in incus-lxcfs.service incus.socket incus-user.socket incus-startup.service; do
            STATE="$(systemctl is-enabled "${unit}" || true)"
            [ "${STATE}" = "masked" ] && continue

            systemctl enable "${unit}" --now
        done

        # Restart or reload anything that's currently running.
        INCUS_SERVICE_STATE="$(systemctl is-enabled incus.service 2>/dev/null || true)"
        if [ "${INCUS_SERVICE_STATE}" != "masked" ] && systemctl is-active incus.service -q; then
            systemctl restart incus.service
        fi

        if systemctl is-active incus-user.service -q; then
            systemctl restart incus-user.service
        fi

        # NOTE: Disabled due to LXCFS crash risk.
        # if systemctl is-active incus-lxcfs.service -q; then
        #     systemctl reload incus-lxcfs.service
        # fi
    elif command -v systemctl >/dev/null 2>&1; then
        # On non-running systems, just enable the units.
        systemctl enable incus-lxcfs.service
        systemctl enable incus.socket
        systemctl enable incus-user.socket
        systemctl enable incus-startup.service
    else
        echo ""
        echo "WARNING: systemd wasn't found on this system"
        echo "As a result, the incus units have not been enabled."
        echo "The Zabbly Incus packages are only tested and supported on systemd systems."
        echo ""
    fi
fi

exit 0
