#!/bin/bash
# Run outside the app: macOS can block the launcher before its code starts.
set -euo pipefail

app='/Applications/WWW-POSIM.app'
repair=0
restart=0
while [ "$#" -gt 0 ]; do
    case "$1" in
        --app) [ "$#" -ge 2 ] || { echo 'Missing --app path.' >&2; exit 2; }; app=$2; shift ;;
        --repair) repair=1 ;;
        --restart-validation) restart=1 ;;
        --help)
            echo 'Usage: bash recover_macos_launch.command [--app /path/WWW-POSIM.app] [--repair [--restart-validation]]'
            echo 'Default: inspect the app. Repair requires an explicit trust confirmation.'
            exit 0 ;;
        *) echo "Unknown option: $1" >&2; exit 2 ;;
    esac
    shift
done
[ "$(uname -s)" = Darwin ] || { echo 'This helper requires macOS.' >&2; exit 1; }
[ "$restart" -eq 0 ] || [ "$repair" -eq 1 ] || { echo '--restart-validation requires --repair.' >&2; exit 2; }
[[ "$app" = /* && "$app" = *.app && -d "$app" && ! -L "$app" ]] || {
    echo 'Choose an installed .app directory, not a symlink.' >&2; exit 1;
}
app=$(cd "$app" && pwd -P)
[[ "$app" != /Volumes/* ]] || { echo 'Copy the app to Applications before recovery.' >&2; exit 1; }
identifier=$(plutil -extract CFBundleIdentifier raw -o - "$app/Contents/Info.plist")
case "$identifier" in
    com.ioes.www-posim|com.ioes.www-posim.development) ;;
    *) echo 'This is not a WWW-POSIM app.' >&2; exit 1 ;;
esac
echo "Checking $app"
codesign --verify --deep --strict "$app" || {
    echo 'Integrity check failed. Download a fresh official installer.' >&2; exit 1;
}
echo 'Signature integrity: passed. This check alone does not establish publisher identity.'
if quarantine=$(xattr -p com.apple.quarantine "$app" 2>/dev/null); then
    echo "Download quarantine: $quarantine"
else
    echo 'No download quarantine flag on the app directory.'
fi
[ "$repair" -eq 1 ] || {
    echo 'No changes made. See the Mac launch recovery section in the installation guide.'
    exit 0;
}

processes=$(ps -axo comm=)
while IFS= read -r process; do
    if [ "$process" = "$app/Contents/MacOS/WWW-POSIM" ]; then
        echo 'Quit WWW-POSIM before recovery. Force Quit only a stuck launcher with no active simulation.' >&2
        exit 1
    fi
done <<< "$processes"
echo 'Use only an official download whose DMG SHA-256 matches the published checksum.'
echo 'Repair allows this app to run even though Apple may not have checked it for malware.'
echo 'It removes download quarantine only from the selected app.'
if [ "$restart" -eq 1 ]; then
    echo 'It also restarts the system-wide macOS app-validation service with sudo.'
    echo 'Other pending app launches may need to be retried. Gatekeeper stays enabled.'
fi
echo 'Type TRUST WWW-POSIM to continue, or press Enter to cancel:'
IFS= read -r consent || consent=''
[ "$consent" = 'TRUST WWW-POSIM' ] || { echo 'Cancelled. No changes made.'; exit 1; }
xattr -dr com.apple.quarantine "$app" || {
    echo 'Could not clear quarantine. Check the app ownership or reinstall the official DMG.' >&2; exit 1;
}
if [ "$restart" -eq 1 ]; then
    sudo /usr/bin/killall -TERM syspolicyd || {
        echo 'Validation-service restart failed. Restart your Mac, then open WWW-POSIM.' >&2; exit 1;
    }
fi
open -a "$app"
echo 'Launch requested. Confirm that the WWW-POSIM window appears.'
