#!/bin/sh
set -eu
# Supersede older detached workers, including reinstall of the same version.
# Failure to allocate a marker leaves installation/cleanup successful but skips
# automatic respring. The directory and marker are private to package scripts.
state=/var/jb/var/lib/netshield2-lifecycle
new_generation() {
    umask 077
    [ ! -L "$state" ] && mkdir -p "$state" || return 1
    marker=$(mktemp "$state/generation.XXXXXXXX") || return 1
    if ! printf '%s\n' "$marker" > "$marker" || ! mv -f "$marker" "$state/current"; then
        rm -f "$marker"
        return 1
    fi
    printf '%s\n' "$marker"
}
case "${1:-}" in
    upgrade)
        new_generation >/dev/null 2>&1 || true
        ;;
    remove|deconfigure)
        generation=$(new_generation 2>/dev/null) || generation=
        # Capture startup output too: injected tweaks can log before main().
        # Successful cleanup is silent; retain our own diagnostics on failure.
        if cleanup_output=$(/var/jb/Applications/NetShield2.app/NetShield2 --remove-filter 2>&1); then
            :
        else
            cleanup_status=$?
            printf '%s\n' "$cleanup_output" | while IFS= read -r line; do
                case "$line" in
                    NetShield2:*) printf '%s\n' "$line" >&2 ;;
                esac
            done
            printf 'NetShield2: filter cleanup failed (exit %s); uninstall cancelled.\n' "$cleanup_status" >&2
            case "$cleanup_output" in
                *'NetShield2: saved-state cleanup failed:'*)
                    printf '%s\n' 'Close any open export and address the reported storage error, then retry package removal.' >&2
                    ;;
                *)
                    printf '%s\n' 'Follow the helper diagnostic above. In NetShield2, tap the Firewall status row to refresh.' >&2
                    printf '%s\n' 'If filter removal is still needed, use Configuration > Remove Filter & Settings, then retry package removal.' >&2
                    ;;
            esac
            exit 1
        fi
        if /var/jb/usr/bin/uicache -u /var/jb/Applications/NetShield2.app >/dev/null 2>&1; then
            # Deconfiguration can precede replacement, rather than removal.
            if [ "${1:-}" != remove ]; then
                exit 0
            fi
            reload=/var/jb/usr/bin/sbreload
            query=/var/jb/usr/bin/dpkg-query
            if [ -x "$reload" ] && [ -x "$query" ] && [ -n "$generation" ]; then
                package_manager_pid=$PPID
                # Wait outside dpkg so a successful removal is never retried just because
                # SpringBoard could not be reloaded. All descriptors are detached.
                (
                    trap '' HUP
                    report_reload_failure() {
                        if command -v logger >/dev/null 2>&1; then
                            logger -t NetShield2 "$1 Respring manually using your jailbreak." || true
                        fi
                    }
                    attempts=0
                    while kill -0 "$package_manager_pid" 2>/dev/null; do
                        if [ "$attempts" -ge 600 ]; then
                            report_reload_failure 'Uninstall respring timed out waiting for the package manager.'
                            exit 0
                        fi
                        sleep 1
                        attempts=$((attempts + 1))
                    done
                    # Absence and residual config-files state are distinct from
                    # a failed query or a replacement package already installed.
                    if observed=$(LC_ALL=C "$query" -W -f='${Status} ${Version}' com.eolnmsuk.netshield 2>&1); then
                        case "$observed" in
                            'deinstall ok config-files @NETSHIELD_PACKAGE_VERSION@'|*' ok not-installed'*) ;;
                            *) report_reload_failure 'Uninstall respring skipped: package removal is unverified.'; exit 0 ;;
                        esac
                    else
                        case "$observed" in
                            *'no packages found matching com.eolnmsuk.netshield') ;;
                            *) report_reload_failure 'Uninstall respring skipped: package state could not be read.'; exit 0 ;;
                        esac
                    fi
                    if [ "$(cat "$state/current" 2>/dev/null)" != "$generation" ]; then
                        report_reload_failure 'Uninstall respring skipped: a newer package operation superseded it.'
                        exit 0
                    fi
                    if ! "$reload"; then
                        report_reload_failure 'Uninstall respring failed to start.'
                    fi
                ) </dev/null >/dev/null 2>&1 &
            elif command -v logger >/dev/null 2>&1; then
                logger -t NetShield2 'Uninstall completed, but sbreload is unavailable or package state cannot be verified; respring manually.' || true
            fi
        elif command -v logger >/dev/null 2>&1; then
            logger -t NetShield2 'Uninstall completed, but icon-cache invalidation failed; respring was not scheduled.' || true
        fi
        ;;
esac
