#!/bin/bash
# Boot emulator, setup Frida, and run the capture server for airline apps.
# Usage: ./run.sh [spirit|delta|united|all]

set -e

EMU=/Users/treyt/Library/Android/sdk/emulator/emulator
ADB=/Users/treyt/Library/Android/sdk/platform-tools/adb
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
FRIDA_SERVER="/data/local/tmp/frida-server"

AIRLINE=${1:-spirit}

# Map airline to package name
case "$AIRLINE" in
    spirit) PKG="com.spirit.customerapp" ;;
    delta)  PKG="com.delta.mobile.android" ;;
    united) PKG="com.united.mobile.android" ;;
    all)    PKG="all" ;;
    *)      echo "Usage: $0 [spirit|delta|united|all]"; exit 1 ;;
esac

echo "========================================="
echo "Airline API Capture via Frida"
echo "Target: $AIRLINE ($PKG)"
echo "========================================="

# 1. Check if emulator is running
if ! $ADB devices 2>/dev/null | grep -q "emulator"; then
    echo "[1/5] Booting emulator..."
    $EMU -avd Pixel_6_API_28 -writable-system -no-snapshot-load -no-audio -gpu swiftshader_indirect &
    sleep 30
    $ADB wait-for-device
    $ADB root
    sleep 3
    $ADB remount
else
    echo "[1/5] Emulator already running"
fi

# 2. Start Frida server
echo "[2/5] Starting Frida server..."
$ADB root 2>/dev/null
sleep 1
if ! $ADB shell "ps -A | grep frida-server" 2>/dev/null | grep -q frida; then
    if ! $ADB shell "test -f $FRIDA_SERVER" 2>/dev/null; then
        echo "  Pushing frida-server..."
        $ADB push /tmp/frida-server $FRIDA_SERVER
        $ADB shell chmod 755 $FRIDA_SERVER
    fi
    $ADB shell "$FRIDA_SERVER -D &"
    sleep 3
fi
echo "  Frida server running"

# 3. Verify Frida connection
echo "[3/5] Verifying Frida..."
frida-ps -U | head -3
echo "  Connected"

# 4. Install app if needed
echo "[4/5] Checking app installation..."
if [ "$PKG" = "all" ]; then
    for pkg in com.spirit.customerapp com.delta.mobile.android com.united.mobile.android; do
        if ! $ADB shell pm list packages | grep -q "$pkg"; then
            echo "  $pkg not installed - install manually first"
        else
            echo "  $pkg OK"
        fi
    done
else
    if ! $ADB shell pm list packages | grep -q "$PKG"; then
        echo "  Installing $PKG..."
        case "$AIRLINE" in
            spirit) $ADB install ~/Desktop/code/flights/apps/com.spirit.customerapp*.apk ;;
            delta)  $ADB install-multiple /tmp/delta_apk/base.apk /tmp/delta_apk/split_config.arm64_v8a.apk /tmp/delta_apk/split_config.xxhdpi.apk ;;
            united) $ADB install ~/Desktop/code/flights/apps/united-airlines.apk ;;
        esac
    else
        echo "  $PKG installed"
    fi
fi

# 5. Launch app and start capture
echo "[5/5] Launching capture..."
if [ "$PKG" = "all" ]; then
    echo "Run this script separately for each airline"
    exit 0
fi

# Launch the app
$ADB shell monkey -p $PKG -c android.intent.category.LAUNCHER 1 2>/dev/null
sleep 5

echo ""
echo "========================================="
echo "Starting Frida capture server..."
echo "Interact with the $AIRLINE app to trigger API calls."
echo "========================================="
echo ""

python3 "$SCRIPT_DIR/server.py" "$PKG" "$SCRIPT_DIR/okhttp_hook.js"
