#!/bin/sh
# Canvashand CLI installer.
#
#   curl -fsSL https://www.canvashand.com/install.sh | sh
#
# Pin a version:            CANVASHAND_VERSION=1.2.0 sh
# Install somewhere else:   CANVASHAND_INSTALL_DIR="$HOME/bin" sh
# The FIGMAGO_VERSION and FIGMAGO_INSTALL_DIR names still work.
#
# POSIX sh on purpose: this runs on stock macOS and on minimal Linux images
# where bash is not guaranteed.

set -eu

BUCKET_URL="${CANVASHAND_BUCKET_URL:-${FIGMAGO_BUCKET_URL:-https://downloads.canvashand.com}}"
VERSION="${CANVASHAND_VERSION:-${FIGMAGO_VERSION:-}}"
INSTALL_DIR="${CANVASHAND_INSTALL_DIR:-${FIGMAGO_INSTALL_DIR:-}}"

die() {
  echo "canvashand install: $1" >&2
  exit 1
}

need() {
  command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
}

need uname
need tar
need mkdir

if command -v curl >/dev/null 2>&1; then
  fetch() { curl -fsSL "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
  fetch() { wget -qO "$2" "$1"; }
else
  die "need curl or wget"
fi

# macOS ships shasum; most Linux images ship sha256sum. Refusing to continue
# without one is deliberate — an unverified binary is the thing this avoids.
if command -v sha256sum >/dev/null 2>&1; then
  sha256() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
  sha256() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
  die "need sha256sum or shasum to verify the download"
fi

case "$(uname -s)" in
  Darwin) OS=darwin ;;
  Linux)  OS=linux ;;
  *) die "unsupported OS: $(uname -s) (macOS and Linux only; on Windows download the .tar.gz from https://www.canvashand.com/download)" ;;
esac

case "$(uname -m)" in
  x86_64 | amd64) ARCH=amd64 ;;
  arm64 | aarch64) ARCH=arm64 ;;
  *) die "unsupported architecture: $(uname -m)" ;;
esac

if [ -n "$VERSION" ]; then
  PREFIX="v${VERSION#v}"
else
  PREFIX="latest"
fi

ARCHIVE="canvashand_${OS}_${ARCH}.tar.gz"
BASE="$BUCKET_URL/$PREFIX"

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT INT TERM

echo "Downloading $ARCHIVE ($PREFIX)..."
fetch "$BASE/$ARCHIVE" "$TMP/$ARCHIVE" || die "could not download $BASE/$ARCHIVE"
fetch "$BASE/checksums.txt" "$TMP/checksums.txt" || die "could not download $BASE/checksums.txt"

EXPECTED="$(grep " \*\{0,1\}${ARCHIVE}\$" "$TMP/checksums.txt" | cut -d' ' -f1 || true)"
[ -n "$EXPECTED" ] || die "no checksum for $ARCHIVE in checksums.txt"

ACTUAL="$(sha256 "$TMP/$ARCHIVE")"
if [ "$EXPECTED" != "$ACTUAL" ]; then
  die "checksum mismatch for $ARCHIVE
  expected $EXPECTED
  actual   $ACTUAL"
fi
echo "Checksum verified."

tar -xzf "$TMP/$ARCHIVE" -C "$TMP" || die "could not extract $ARCHIVE"
[ -f "$TMP/canvashand" ] || die "archive did not contain a canvashand binary"
chmod +x "$TMP/canvashand"

# Prefer a system-wide install, but never silently fail: fall back to a
# per-user directory when /usr/local/bin is not writable and sudo is absent.
if [ -n "$INSTALL_DIR" ]; then
  DEST="$INSTALL_DIR"
  SUDO=""
elif [ -w /usr/local/bin ] 2>/dev/null; then
  DEST=/usr/local/bin
  SUDO=""
elif command -v sudo >/dev/null 2>&1 && [ -d /usr/local/bin ]; then
  DEST=/usr/local/bin
  SUDO=sudo
  echo "Installing to $DEST requires sudo."
else
  DEST="$HOME/.local/bin"
  SUDO=""
fi

$SUDO mkdir -p "$DEST"
$SUDO mv "$TMP/canvashand" "$DEST/canvashand"
# Configs written before the rename start figma-go; keep that name working.
$SUDO ln -sf canvashand "$DEST/figma-go"

echo "Installed $DEST/canvashand (figma-go still works)"
"$DEST/canvashand" --version || die "installed binary did not run"

case ":$PATH:" in
  *":$DEST:"*) ;;
  *)
    echo
    echo "WARNING: $DEST is not on your PATH. Add this to your shell profile:"
    echo "  export PATH=\"$DEST:\$PATH\""
    ;;
esac

# Put the matching Figma plugin in ~/.canvashand/canvashand-plugin, where
# `canvashand update` keeps it current. A pinned version skips this: update
# would move the binary to the latest release. The call says `upgrade`, the
# old name every release understands (newer ones treat it as `update`).
if [ -z "$VERSION" ]; then
  echo
  "$DEST/canvashand" upgrade \
    || echo "canvashand install: could not set up the plugin folder; download the plugin from https://www.canvashand.com/setup instead." >&2
fi

echo
echo "Next: set up your AI clients. Run this from your project folder:"
echo "  canvashand setup --all"
echo "To set up every project at once, add --scope user."
