#!/bin/bash
# postinstall - Runs after PKG installation to install the privileged helper daemon
# This script runs as root during installation.
# The actual daemon install lives in install-helper.sh (shared with the in-app
# Repair flow); this wrapper handles PKG quirks: lock file, app-path discovery,
# xray binary permissions, and app launch.

HELPER_NAME="com.browsec.helper"
SOCKET_PATH="/var/run/browsec-vpn.sock"
LOG_FILE="/var/log/browsec-helper-install.log"
LOCK_FILE="/tmp/browsec-postinstall.lock"

log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE"
}

log "postinstall called with args: $*"

# Guard against concurrent/repeated postinstall invocations.
# PKG installer may call this script multiple times with different paths.
if [ -f "$LOCK_FILE" ]; then
    LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null)
    if kill -0 "$LOCK_PID" 2>/dev/null; then
        log "Another postinstall is already running (PID $LOCK_PID), skipping"
        exit 0
    fi
    # Stale lock file, remove it
    rm -f "$LOCK_FILE"
fi
echo $$ > "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT

# Try to find the app bundle - PKG may pass different paths
# $2 could be /Applications, /Applications/Browsec.app, or the full path
for CANDIDATE in "$2" "$2/Browsec.app" "/Applications/Browsec.app"; do
    HELPER_SRC="$CANDIDATE/Contents/Resources/helper/$HELPER_NAME"
    if [ -f "$HELPER_SRC" ]; then
        APP_PATH="$CANDIDATE"
        break
    fi
done

if [ -z "$APP_PATH" ] || [ ! -f "$HELPER_SRC" ]; then
    log "Helper binary not found, skipping (this may be a secondary postinstall call)"
    exit 0
fi

log "Starting Browsec helper installation..."
log "App path: $APP_PATH"

# Fix executable permissions on browbox and browray binaries
XRAY_DIR="$APP_PATH/Contents/Resources/xray"
if [ -d "$XRAY_DIR" ]; then
    log "Fixing browbox/browray permissions..."
    chmod +x "$XRAY_DIR/browbox" 2>/dev/null || true
    chmod +x "$XRAY_DIR/browray" 2>/dev/null || true
    # Remove quarantine attributes that may prevent execution
    xattr -cr "$XRAY_DIR/browbox" 2>/dev/null || true
    xattr -cr "$XRAY_DIR/browray" 2>/dev/null || true
fi

# Install/refresh the daemon via the shared script. Prefer the copy shipped
# inside the bundle (same one the in-app Repair uses); fall back to the copy
# bundled next to this postinstall in the PKG scripts directory.
SCRIPTS_DIR="$(dirname "$0")"
INSTALL_SCRIPT="$APP_PATH/Contents/Resources/helper/install-helper.sh"
if [ ! -f "$INSTALL_SCRIPT" ]; then
    INSTALL_SCRIPT="$SCRIPTS_DIR/install-helper.sh"
fi

if [ -f "$INSTALL_SCRIPT" ]; then
    /bin/bash "$INSTALL_SCRIPT" "$APP_PATH" || log "install-helper.sh reported failure"
else
    log "ERROR: install-helper.sh not found"
fi

log "Browsec helper installation complete"

# Launch the app only if daemon is ready and app is not already running
if [ -S "$SOCKET_PATH" ]; then
    if ! pgrep -x "Browsec" > /dev/null 2>&1; then
        log "Launching Browsec..."
        open "$APP_PATH" || true
    else
        log "Browsec is already running, skipping launch"
    fi
else
    log "Skipping app launch - daemon is not ready"
fi

exit 0
