#!/bin/bash
# Expedition Launcher - Double-cliquez pour installer

set -e

APP_NAME="Expedition Launcher"
INSTALL_DIR="/Applications"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

clear
echo ""
echo "  ╔══════════════════════════════════════╗"
echo "  ║   Expedition Launcher - Installer    ║"
echo "  ╚══════════════════════════════════════╝"
echo ""

# Find the .app bundle next to this script
APP_BUNDLE=$(find "$SCRIPT_DIR" -maxdepth 1 -name "*.app" -type d | head -1)

if [ -z "$APP_BUNDLE" ]; then
    echo "  ✗ Erreur: Impossible de trouver l'application."
    echo "    Assurez-vous que le .app est dans le meme dossier que ce script."
    echo ""
    echo "  Appuyez sur une touche pour fermer..."
    read -n 1
    exit 1
fi

APP_BASENAME=$(basename "$APP_BUNDLE")
echo "  → Application trouvee: $APP_BASENAME"

# Remove quarantine
echo "  → Suppression des restrictions macOS..."
xattr -cr "$APP_BUNDLE"

# Ad-hoc code sign
echo "  → Signature de l'application..."
codesign --force --deep -s - "$APP_BUNDLE" 2>/dev/null || true

# Move to Applications
DEST="$INSTALL_DIR/$APP_BASENAME"
if [ -d "$DEST" ]; then
    echo "  → Mise a jour (suppression ancienne version)..."
    rm -rf "$DEST"
fi

echo "  → Installation dans Applications..."
cp -R "$APP_BUNDLE" "$INSTALL_DIR/"

echo ""
echo "  ✓ $APP_NAME installe avec succes !"
echo ""
echo "  Lancement de l'application..."
sleep 1
open "$DEST"

echo ""
echo "  Vous pouvez fermer cette fenetre."
echo ""
