#!/bin/sh


after_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
}

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

    rm -rf "$home/.local/share/warp"
    rm -rf "$home/.local/share/cloudflare-warp-gui"
}

rm -rf /var/lib/cloudflare-warp
rm -rf /var/log/cloudflare-warp
for_each_regular_user remove_tos_acceptance

# NOTE: These needs to stay in sync with the constant in network-info
SYSTEMD_OVERRIDE_PATH="/etc/systemd/resolved.conf.d/cloudflare-warp.conf"

if [ -f "$SYSTEMD_OVERRIDE_PATH" ]; then
  # Remove the WARP-override file for systemd-resolved if it exists
  rm -f "$SYSTEMD_OVERRIDE_PATH"
  # Restart the systemd-resolved service to force it to pick up the new changes
  systemctl restart systemd-resolved
fi

}

after_purge() {
    :
}

dummy() {
    :
}


if [ "${1}" = "remove" -o "${1}" = "abort-install" ]
then
    # "after remove" goes here
    # "abort-install" happens when the pre-installation script failed.
    #   In that case, this script, which should be idemptoent, is run
    #   to ensure a clean roll-back of the installation.
    after_remove
elif [ "${1}" = "purge" -a -z "${2}" ]
then
    # like "on remove", but executes after dpkg deletes config files
    # 'apt-get purge' runs 'on remove' section, then this section.
    # There is no equivalent in RPM or ARCH.
    after_purge
elif [ "${1}" = "upgrade" ]
then
    # This represents the case where the old package's postrm is called after
    # the 'preinst' script is called.
    # We should ignore this and just use 'preinst upgrade' and
    # 'postinst configure'. 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 post-removal script was run." >&2
    exit 1
fi
