#!/bin/sh


before_remove() {
    :
#!/bin/sh

for_each_regular_user() {
    func="$1"

    UID_MIN=$(grep -Po '^UID_MIN[[:space:]]*\K([[:digit:]]*)' /etc/login.defs)
    UID_MAX=$(grep -Po '^UID_MAX[[:space:]]*\K([[:digit:]]*)' /etc/login.defs)

    getent passwd | while read -r entry; do
        #1        2      3   4   5           6    7
        #username:passwd:uid:gid:description:home:shell
        username=$(echo "$entry" | cut -d ":" -f1)
        uid=$(echo "$entry" | cut -d ":" -f3)
        home=$(echo "$entry" | cut -d ":" -f6)

        if [ "$uid" -ge "$UID_MIN" ] && [ "$uid" -le "$UID_MAX" ]; then
            "$func" "$username" "$home"
        fi
    done
}

disable_warp_taskbar_service() {
    username=$1
    home=$2

    if [ -e $XDG_RUNTIME_DIR ]; then
        # The following command is the correct way to control user systemd, but
        # unfortunately it is only available in systemd v248 and higher. Once
        # all supported platforms support system v248 or higher we can switch
        # and remove the if check
        #
        # systemctl --machine=$username@.host --user stop warp-taskbar >/dev/null || true
        # systemctl --machine=$username@.host --user daemon-reload >/dev/null || true

        su - $username --shell=/bin/sh -c 'export XDG_RUNTIME_DIR=/run/user/$(id -u); systemctl --user stop warp-taskbar || true'
        su - $username --shell=/bin/sh -c 'export XDG_RUNTIME_DIR=/run/user/$(id -u); systemctl --user daemon-reload'
    fi
}

for_each_regular_user disable_warp_taskbar_service

debsystemctl=$(command -v deb-systemd-invoke || echo systemctl)
$debsystemctl stop warp-svc.service >/dev/null || true
systemctl disable warp-svc.service >/dev/null || true
systemctl --system daemon-reload >/dev/null || true
}

dummy() {
    :
}

if [ "${1}" = "remove" -a -z "${2}" ]
then
    # "before remove" goes here
    before_remove
elif [ "${1}" = "upgrade" ]
then
    # Executed before the old version is removed
    # upon upgrade.
    # We should generally not do anything here. The newly installed package
    # should do the upgrade, not the uninstalled one, since it can't anticipate
    # what new things it will have to do to upgrade for the new version.
    dummy
elif echo "${1}" | grep -E -q "(fail|abort)"
then
    echo "Failed to install before the pre-removal script was run." >&2
    exit 1
fi
