#!/bin/sh
set -u

MIRROR=/.sysroot/usr/libexec/networkserviceproxy
LEGACY_PATCHER=${MIRROR}.roothidepatch
TWEAK=/Library/MobileSubstrate/DynamicLibraries/RelayRace.dylib
FILTER=/Library/MobileSubstrate/DynamicLibraries/RelayRace.plist
SESSION=/var/lib/relayrace/prepared-jbroot
RESET_SESSION=/var/lib/relayrace/reset-jbroot
ORIGINAL_STATE=/var/lib/relayrace/original-state

JBROOT_TOOL=$(command -v jbroot 2>/dev/null || true)

jb_real() {
    [ -n "$JBROOT_TOOL" ] || return 1
    "$JBROOT_TOOL" "$1"
}

resolve_roothide_tool() {
    logical="$1"
    if [ -x "$logical" ]; then
        printf '%s\n' "$logical"
        return 0
    fi
    resolved=$(jb_real "$logical" 2>/dev/null || true)
    if [ -n "$resolved" ] && [ -x "$resolved" ]; then
        printf '%s\n' "$resolved"
        return 0
    fi
    name=${logical##*/}
    found=$(command -v "$name" 2>/dev/null || true)
    if [ -n "$found" ] && [ -x "$found" ]; then
        printf '%s\n' "$found"
        return 0
    fi
    return 1
}

LDID=$(resolve_roothide_tool /basebin/ldid || true)

echo '=== RelayRace RootHide status ==='
printf 'jbroot: '
CURRENT=$(jb_real / 2>/dev/null || true)
[ -n "$CURRENT" ] && echo "$CURRENT" || echo 'unavailable'

if [ -e /var/mobile/.tweakenabled ]; then
    echo 'tweak injection: enabled'
else
    echo 'tweak injection: DISABLED'
fi

if [ -f "$MIRROR" ] && [ ! -L "$MIRROR" ]; then
    echo 'networkserviceproxy mirror: prepared regular file'
else
    echo 'networkserviceproxy mirror: MISSING/INVALID'
fi

if [ -f "$MIRROR" ] && [ ! -L "$MIRROR" ]; then
    if [ -n "$LDID" ]; then
        REAL_MIRROR=$(jb_real "$MIRROR" 2>/dev/null || true)
        if [ -n "$REAL_MIRROR" ] && "$LDID" -e "$REAL_MIRROR" 2>/dev/null | grep -q '<key>com.apple.private.security.no-sandbox</key>'; then
            echo 'mirror RootHide daemon profile: no-sandbox present'
        else
            echo 'mirror RootHide daemon profile: no-sandbox MISSING'
        fi
    else
        echo 'mirror RootHide daemon profile: unable to inspect (ldid unavailable)'
    fi
fi

[ -f "$TWEAK" ] && echo 'RelayRace tweak dylib: present' || echo 'RelayRace tweak dylib: MISSING'
[ -f "$FILTER" ] && echo 'RelayRace tweak filter: present' || echo 'RelayRace tweak filter: MISSING'
[ -e "$LEGACY_PATCHER" ] && echo 'legacy PatchLoader module: PRESENT (unexpected)' || echo 'legacy PatchLoader module: absent'
if [ -f "$ORIGINAL_STATE" ]; then
    printf 'original mirror state: '
    cat "$ORIGINAL_STATE" 2>/dev/null || true
fi

if [ -f "$SESSION" ]; then
    printf 'prepared session: '
    cat "$SESSION" 2>/dev/null || true
else
    echo 'prepared session: missing'
fi

if [ -f "$RESET_SESSION" ]; then
    RESET=$(cat "$RESET_SESSION" 2>/dev/null || true)
    if [ -n "$CURRENT" ] && [ "$RESET" = "$CURRENT" ]; then
        echo 'Private Relay reset: posted for current session'
    else
        printf 'Private Relay reset: stale session marker: %s\n' "$RESET"
    fi
else
    echo 'Private Relay reset: NOT posted for current session'
fi

echo '--- launchd state ---'
STATE=$(launchctl print system/com.apple.networkserviceproxy 2>&1 || true)
printf '%s\n' "$STATE" | grep -E 'state =|runs =|pid =|last exit code|last terminating signal|job state =' || true

PID=$(printf '%s\n' "$STATE" | awk '/^[[:space:]]*pid = / { print $3; exit }')
if [ -n "$PID" ]; then
    echo "networkserviceproxy process: launchd pid=$PID"
else
    echo 'networkserviceproxy process: not currently assigned a pid'
fi
