#!/usr/bin/env sh
# Marigold installer. One line, then sign in:
#
#   curl -fsSL https://get.marigold.rasyn.ai | sh
#
# It downloads the package for this machine, unpacks it to ~/.marigold/app,
# links `marigold` into ~/.local/bin, and starts it. Your data lives in
# ~/.marigold and never leaves the machine; models run on your Marigold plan
# after you sign in.
#
# Env overrides: MARIGOLD_VERSION (default: latest), MARIGOLD_HOME,
# MARIGOLD_RELEASES (base URL of the release host).
#
# POSIX sh. ASCII only. No em or en dashes.

set -eu

RELEASES="${MARIGOLD_RELEASES:-https://get.marigold.rasyn.ai}"
HOME_DIR="${MARIGOLD_HOME:-$HOME/.marigold}"
APP="$HOME_DIR/app"
BINDIR="$HOME/.local/bin"

say() { printf '%s\n' "$*"; }
die() { printf '%s\n' "$*" >&2; exit 1; }

case "$(uname -s)" in
  Linux)  OS=linux;;
  Darwin) OS=darwin;;
  *) die "Marigold installs on macOS and Linux. On Windows, install it inside WSL.";;
esac
case "$(uname -m)" in
  x86_64|amd64) ARCH=x64;;
  arm64|aarch64) ARCH=arm64;;
  *) die "Unsupported CPU architecture: $(uname -m)";;
esac
PLATFORM="$OS-$ARCH"

command -v curl >/dev/null 2>&1 || die "curl is required."
command -v tar  >/dev/null 2>&1 || die "tar is required."

VERSION="${MARIGOLD_VERSION:-}"
if [ -z "$VERSION" ]; then
  VERSION="$(curl -fsSL "$RELEASES/latest" 2>/dev/null | tr -d ' \n\r')" ||
    die "Could not reach $RELEASES to find the latest version."
  [ -n "$VERSION" ] || die "The release host did not name a version."
fi

TARBALL="marigold-$VERSION-$PLATFORM.tar.gz"
URL="$RELEASES/$VERSION/$TARBALL"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

say "Downloading Marigold $VERSION for $PLATFORM ..."
curl -fSL --progress-bar -o "$TMP/pkg.tar.gz" "$URL" ||
  die "Download failed: $URL"

# Checksum, when the release publishes one. A silent mismatch is worse than a
# slow install, so a published-but-wrong sum is fatal; a missing sum is not.
if curl -fsSL -o "$TMP/sha256" "$URL.sha256" 2>/dev/null; then
  EXPECT="$(cut -d' ' -f1 < "$TMP/sha256" | tr -d ' \n\r')"
  if command -v sha256sum >/dev/null 2>&1; then
    ACTUAL="$(sha256sum "$TMP/pkg.tar.gz" | cut -d' ' -f1)"
  else
    ACTUAL="$(shasum -a 256 "$TMP/pkg.tar.gz" | cut -d' ' -f1)"
  fi
  [ "$EXPECT" = "$ACTUAL" ] || die "Checksum mismatch. Refusing to install."
  say "Checksum verified."
fi

say "Unpacking ..."
mkdir -p "$HOME_DIR" "$BINDIR"
rm -rf "$APP.new" && mkdir -p "$APP.new"
tar xzf "$TMP/pkg.tar.gz" -C "$APP.new" --strip-components 1
# Keep the previous install until the new one is in place, so a failed unpack
# never leaves a machine with no app at all.
if [ -d "$APP" ]; then rm -rf "$APP.prev" && mv "$APP" "$APP.prev"; fi
mv "$APP.new" "$APP"
rm -rf "$APP.prev"

ln -sf "$APP/marigold" "$BINDIR/marigold"
say "Installed to $APP"

case ":$PATH:" in
  *":$BINDIR:"*) ;;
  *) say ""; say "Add this to your shell profile so 'marigold' is on your PATH:"; say "  export PATH=\"\$HOME/.local/bin:\$PATH\"";;
esac

say ""
say "Starting Marigold (first run sets up the local database; it takes a minute) ..."
"$APP/marigold" start || die "Marigold did not start. Run '$APP/marigold logs' to see why."

say ""
say "Marigold is running at http://localhost:3000"
say "Sign in with your Marigold account to use models on your plan."
say "Commands: marigold status | logs | stop | update | doctor"
