#!/usr/bin/env bash
# Adam – AI Copilot installer — drops the add-in into Fusion's standard
# AddIns folder so it shows up in Shift+S → Add-Ins.
#
# Note: Fusion ships new third-party add-ins disabled until you click Run
# once in the Add-Ins panel. After that one click it auto-loads on every
# subsequent Fusion launch (the manifest's runOnStartup: true takes effect).
# We tried installing into ApplicationPlugins/ to skip this gate the way
# the App Store install does, but Fusion didn't pick the bundle up at all
# without going through Autodesk's installer dialog — so this path is the
# only reliable manual install.
#
# Usage:
#   curl -sSL https://fusion.adam.new/install.sh | bash
#
# Idempotent: rerun any time to upgrade. Removes the existing install
# (or any bundle install from the brief ApplicationPlugins-path attempt)
# before extracting the new copy.

set -euo pipefail

ADDIN_NAME="AdamFusion"
DOWNLOAD_URL="https://fusion.adam.new/AdamFusion.bundle.zip"

print_step()    { printf "\033[1;36m→\033[0m %s\n" "$1"; }
print_ok()      { printf "\033[1;32m✓\033[0m %s\n" "$1"; }
print_warn()    { printf "\033[1;33m!\033[0m %s\n" "$1"; }
print_error()   { printf "\033[1;31m✗\033[0m %s\n" "$1"; }

# Detect platform — for now macOS only. Windows users get a clear pointer.
case "$(uname -s)" in
  Darwin)
    INSTALL_DIR="$HOME/Library/Application Support/Autodesk/Autodesk Fusion 360/API/AddIns"
    LEGACY_BUNDLE="$HOME/Library/Application Support/Autodesk/ApplicationPlugins/AdamFusion.bundle"
    ;;
  Linux)
    print_error "Adam runs inside Autodesk Fusion, which doesn't support Linux."
    exit 1
    ;;
  MINGW*|MSYS*|CYGWIN*)
    print_error "On Windows, run the PowerShell one-liner instead:"
    echo "  irm https://fusion.adam.new/install.ps1 | iex"
    exit 1
    ;;
  *)
    print_error "Unsupported OS: $(uname -s)"
    exit 1
    ;;
esac

# Pretty header
printf "\n"
printf "  Adam – AI Copilot installer\n"
printf "  drives Fusion natively with AI · https://fusion.adam.new\n"
printf "\n"

# If a previous installer dropped the bundle into ApplicationPlugins (the
# brief experiment that didn't work outside the App Store flow), clear it
# so users upgrading from that release don't end up with a ghost install.
if [ -L "$LEGACY_BUNDLE" ] || [ -d "$LEGACY_BUNDLE" ]; then
  print_step "Removing legacy bundle install at $LEGACY_BUNDLE"
  rm -rf "$LEGACY_BUNDLE"
fi

# Remove existing install so an upgrade replaces it cleanly. Done up front
# so a partial download below doesn't leave the user with two installs.
if [ -L "$INSTALL_DIR/$ADDIN_NAME" ] || [ -d "$INSTALL_DIR/$ADDIN_NAME" ]; then
  print_step "Removing previous install at $INSTALL_DIR/$ADDIN_NAME"
  rm -rf "$INSTALL_DIR/$ADDIN_NAME"
fi

# Make sure the parent dir exists. Autodesk normally creates it on first
# Fusion launch, but on a fresh machine it might not be there yet.
mkdir -p "$INSTALL_DIR"

# Download to a tmp, extract on success — avoids a half-written addin if
# the user's Wi-Fi cuts mid-download.
TMP_DIR="$(mktemp -d -t adam-fusion-install)"
trap 'rm -rf "$TMP_DIR"' EXIT

print_step "Downloading add-in (~140KB)…"
if ! curl -fsSL --retry 3 --retry-delay 1 -o "$TMP_DIR/AdamFusion.bundle.zip" "$DOWNLOAD_URL"; then
  print_error "Download failed. Check your network and try again."
  print_warn  "If the problem persists, manually grab $DOWNLOAD_URL and unzip it."
  exit 1
fi

# The artifact is an Autodesk Application Plug-in bundle: the actual
# Fusion add-in lives under AdamFusion.bundle/Contents/. We extract that
# subfolder directly into AddIns/AdamFusion/ so Fusion's standard add-in
# scanner picks it up at next launch.
print_step "Installing to $INSTALL_DIR/$ADDIN_NAME"
unzip -q -d "$TMP_DIR" "$TMP_DIR/AdamFusion.bundle.zip"
mv "$TMP_DIR/AdamFusion.bundle/Contents" "$INSTALL_DIR/$ADDIN_NAME"

if [ ! -f "$INSTALL_DIR/$ADDIN_NAME/AdamFusion.manifest" ]; then
  print_error "Install completed but the manifest isn't where Fusion expects it."
  echo "  Expected: $INSTALL_DIR/$ADDIN_NAME/AdamFusion.manifest"
  exit 1
fi

print_ok "Installed to $INSTALL_DIR/$ADDIN_NAME"
printf "\n"
printf "Next steps:\n"
printf "  1. \033[1mQuit Fusion fully\033[0m (⌘Q so add-ins reload).\n"
printf "  2. Reopen Fusion.\n"
printf "  3. Press \033[1mShift+S\033[0m and switch to the \033[1mAdd-Ins\033[0m tab.\n"
printf "  4. Find \033[1mAdamFusion\033[0m, click \033[1mRun\033[0m, and tick \033[1mRun on Startup\033[0m.\n"
printf "  5. The Adam palette docks on the right. Sign in with your Autodesk account.\n"
printf "\n"
printf "Heads up: Fusion ships new add-ins disabled until you click Run once.\n"
printf "After that they auto-launch every time.\n"
printf "\n"
printf "Need help? https://fusion.adam.new\n"
printf "\n"
