#!/bin/sh
# Ulvium installer for macOS and Linux.
#
#   curl -fsSL https://ulvium.com/install.sh | sh
#
# Installs the `ulvium` terminal command into ~/.local/bin (override with
# ULVIUM_BIN_DIR). On macOS, add --desktop to also install the desktop app
# into /Applications:
#
#   curl -fsSL https://ulvium.com/install.sh | sh -s -- --desktop
#
# Every download is checked against the published SHA-256 list before it is
# installed. Nothing is sent anywhere: no telemetry, no machine id.
set -eu

BASE="${ULVIUM_DOWNLOAD_BASE:-https://dl.ulvium.com}"
BIN_DIR="${ULVIUM_BIN_DIR:-$HOME/.local/bin}"
WANT_DESKTOP=0
VERSION="${ULVIUM_VERSION:-}"

for arg in "$@"; do
  case "$arg" in
    --desktop) WANT_DESKTOP=1 ;;
    --version=*) VERSION="${arg#--version=}" ;;
    -h|--help)
      echo "Usage: install.sh [--desktop] [--version=X.Y.Z]"
      exit 0 ;;
    *) echo "Unknown option: $arg" >&2; exit 2 ;;
  esac
done

say() { printf '%s\n' "$*"; }
fail() { printf 'ulvium install: %s\n' "$*" >&2; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || fail "this installer needs '$1'. Install it and run the command again."; }

need curl
need tar
need uname

fetch() { curl -fsSL --proto '=https' --tlsv1.2 "$1" -o "$2" || fail "could not download $1"; }

sha256_of() {
  if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1
  elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1
  else fail "this installer needs 'sha256sum' or 'shasum' to verify the download."
  fi
}

# ---- Which build fits this machine ----
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
  Darwin) os=darwin ;;
  Linux) os=linux ;;
  *) fail "this installer supports macOS and Linux. On Windows, see https://ulvium.com/download" ;;
esac
case "$arch" in
  x86_64|amd64) arch=x64 ;;
  arm64|aarch64) arch=arm64 ;;
  *) fail "unsupported processor: $arch" ;;
esac

# An Intel shell under Rosetta on an Apple Silicon Mac: use the native build.
if [ "$os" = darwin ] && [ "$arch" = x64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then
  arch=arm64
fi

target="$os-$arch"
if [ "$os" = linux ]; then
  if [ -f /etc/alpine-release ] || (ldd --version 2>&1 | grep -qi musl); then target="$target-musl"; fi
fi
# Older x64 processors without AVX2 need the baseline build.
if [ "$arch" = x64 ]; then
  if [ "$os" = linux ] && ! grep -qw avx2 /proc/cpuinfo 2>/dev/null; then target="$target-baseline"; fi
  if [ "$os" = darwin ] && ! sysctl -n machdep.cpu.leaf7_features 2>/dev/null | grep -qw AVX2; then target="$target-baseline"; fi
fi

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

if [ -z "$VERSION" ]; then
  fetch "$BASE/latest" "$tmp/latest"
  VERSION="$(tr -d ' \r\n' < "$tmp/latest")"
fi
case "$VERSION" in
  ''|*[!0-9A-Za-z.-]*) fail "unexpected version string from the download server." ;;
esac

fetch "$BASE/$VERSION/SHA256SUMS" "$tmp/SHA256SUMS"

# Downloads one release file and stops unless its SHA-256 matches the published list.
download() {
  file="$1"
  want="$(awk -v f="$file" '$2 == f { print $1 }' "$tmp/SHA256SUMS")"
  [ -n "$want" ] || fail "no build named $file in version $VERSION. See https://ulvium.com/download"
  say "Downloading $file"
  fetch "$BASE/$VERSION/$file" "$tmp/$file"
  got="$(sha256_of "$tmp/$file")"
  [ "$got" = "$want" ] || fail "checksum mismatch for $file. Nothing was installed."
}

# ---- Terminal command ----
cli="ulvium-$target.tar.gz"
download "$cli"
mkdir -p "$tmp/cli" "$BIN_DIR"
tar -xzf "$tmp/$cli" -C "$tmp/cli"
[ -f "$tmp/cli/ulvium" ] || fail "the download did not contain the ulvium command."
chmod 755 "$tmp/cli/ulvium"
# Replace in one step so a running copy is never half written.
mv -f "$tmp/cli/ulvium" "$BIN_DIR/ulvium.new"
mv -f "$BIN_DIR/ulvium.new" "$BIN_DIR/ulvium"
say "Installed the ulvium command to $BIN_DIR/ulvium"

# ---- Desktop app (macOS) ----
if [ "$WANT_DESKTOP" = 1 ]; then
  [ "$os" = darwin ] || fail "--desktop is for macOS. Linux and Windows desktop builds are on https://ulvium.com/download"
  app="Ulvium-mac-$arch.dmg"
  download "$app"
  need hdiutil
  rm -rf "$tmp/app" "$tmp/mnt"
  mkdir -p "$tmp/app" "$tmp/mnt"
  hdiutil attach "$tmp/$app" -nobrowse -readonly -mountpoint "$tmp/mnt" >/dev/null || fail "could not open the downloaded disk image."
  if [ -d "$tmp/mnt/Ulvium.app" ]; then cp -R "$tmp/mnt/Ulvium.app" "$tmp/app/"; fi
  hdiutil detach "$tmp/mnt" >/dev/null 2>&1 || true
  [ -d "$tmp/app/Ulvium.app" ] || fail "the download did not contain Ulvium.app."
  dest="/Applications"
  [ -w "$dest" ] || { dest="$HOME/Applications"; mkdir -p "$dest"; }
  if pgrep -x Ulvium >/dev/null 2>&1; then fail "Ulvium is running. Quit it and run the command again to update the app."; fi
  rm -rf "$dest/Ulvium.app"
  mv "$tmp/app/Ulvium.app" "$dest/Ulvium.app"
  say "Installed the desktop app to $dest/Ulvium.app"
fi

case ":$PATH:" in
  *":$BIN_DIR:"*) ;;
  *)
    say ""
    say "$BIN_DIR is not on your PATH yet. Add this line to your shell profile, then open a new terminal:"
    say "  export PATH=\"$BIN_DIR:\$PATH\""
    ;;
esac

say ""
say "Next: run 'ulvium' in a project folder and sign in with your account number."
say "No account yet? Create one at https://ulvium.com/login"
