#!/bin/sh
# VibeCode.surf installer for macOS and Linux.
#
#   curl -fsSL https://vibecode.surf/install.sh | sh
#
# Asks the VibeCode.surf service which file fits this computer, downloads it and installs it:
#   macOS  -> /Applications/VibeCode.surf.app (or ~/Applications without admin rights)
#   Linux  -> ~/.local/bin/vibecode-surf (AppImage) plus a launcher entry
#
# Running it again later installs the newest version over the old one. Your projects,
# chats and settings are kept: they live outside the app.
#
# Options (environment variables):
#   VIBECODE_VERSION   install this version (for example 0.1.0) instead of the newest
#   VIBECODE_DIR       where to put the app (macOS) or the program (Linux)
set -eu

BASE="${VIBECODE_URL:-https://vibecode.surf}"
VERSION="${VIBECODE_VERSION:-latest}"
APP_NAME="VibeCode.surf"
BIN_NAME="vibecode-surf"

say() { printf '%s\n' "$*"; }
fail() { printf 'install: %s\n' "$*" >&2; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || fail "this installer needs '$1', which was not found"; }

need curl
need uname

case "$(uname -s)" in
  Darwin) os=darwin ;;
  Linux) os=linux ;;
  *) fail "this script is for macOS and Linux. On Windows, run in PowerShell:  irm $BASE/install.ps1 | iex" ;;
esac
case "$(uname -m)" in
  x86_64 | amd64) arch=x86_64 ;;
  arm64 | aarch64) arch=aarch64 ;;
  *) fail "unsupported processor: $(uname -m)" ;;
esac

say "Looking up $APP_NAME for this computer..."
info=$(curl -fsSL "$BASE/api/install/$os/$arch?version=$VERSION") || fail "could not reach $BASE"
field() { printf '%s\n' "$info" | sed -n "s/^$1=//p" | sed -n 1p; }
[ "$(field ok)" = 1 ] || fail "$(field error)"
url=$(field url)
version=$(field version)
[ -n "$url" ] || fail "the service did not name a download"

tmp=$(mktemp -d 2>/dev/null || mktemp -d -t vibecode)
trap 'rm -rf "$tmp"' EXIT INT TERM

case "$os" in
  darwin)
    need hdiutil
    say "Downloading $APP_NAME $version..."
    curl -fL --progress-bar -o "$tmp/app.dmg" "$url" || fail "download failed"

    mount="$tmp/mount"
    mkdir "$mount"
    hdiutil attach "$tmp/app.dmg" -nobrowse -readonly -mountpoint "$mount" >/dev/null || fail "could not open the disk image"
    app=$(find "$mount" -maxdepth 1 -name '*.app' | sed -n 1p)
    if [ -z "$app" ]; then hdiutil detach "$mount" >/dev/null 2>&1 || true; fail "the disk image holds no app"; fi

    dest="${VIBECODE_DIR:-/Applications}"
    if [ ! -w "$dest" ]; then dest="$HOME/Applications"; mkdir -p "$dest"; fi
    target="$dest/$(basename "$app")"
    say "Installing to $target..."
    rm -rf "$target"
    cp -R "$app" "$dest/" || { hdiutil detach "$mount" >/dev/null 2>&1 || true; fail "could not copy the app to $dest"; }
    hdiutil detach "$mount" >/dev/null 2>&1 || true

    say ""
    say "$APP_NAME $version is installed. Open it from Launchpad, or run:"
    say "  open \"$target\""
    ;;

  linux)
    dest="${VIBECODE_DIR:-$HOME/.local/bin}"
    mkdir -p "$dest"
    say "Downloading $APP_NAME $version..."
    curl -fL --progress-bar -o "$tmp/app.AppImage" "$url" || fail "download failed"
    chmod +x "$tmp/app.AppImage"
    mv "$tmp/app.AppImage" "$dest/$BIN_NAME"

    # A launcher entry, so the app shows up in the applications menu.
    apps="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
    mkdir -p "$apps"
    cat >"$apps/$BIN_NAME.desktop" <<DESKTOP
[Desktop Entry]
Type=Application
Name=$APP_NAME
Comment=A crew of AI agents in one desktop code editor
Exec=$dest/$BIN_NAME %U
Terminal=false
Categories=Development;IDE;
DESKTOP

    say ""
    say "$APP_NAME $version is installed at $dest/$BIN_NAME"
    case ":$PATH:" in
      *":$dest:"*) say "Start it with: $BIN_NAME" ;;
      *) say "Start it with: $dest/$BIN_NAME   (add $dest to your PATH to type just '$BIN_NAME')" ;;
    esac
    say "If it does not start, your system may need FUSE for AppImages (package 'libfuse2' on Debian and Ubuntu)."
    ;;
esac

message=$(field message)
if [ -n "$message" ]; then say ""; say "$message"; fi
say "Create your free account when the app opens, or at $BASE/app"
