#!/bin/sh
# Installs the Kenso CLI on macOS or Linux. It brings its own Node.js, so the machine needs none.
#
#   curl -fsSL https://ken.so/install.sh | sh
#
# It downloads this machine's standalone build from https://downloads.ken.so/desktop/cli/ (published by
# ken-backend .github/workflows/publish-cli.yml), checks it against that version's SHA256SUMS, unpacks it to
# ~/.local/share/kenso and links ~/.local/bin/kenso to it. Run it again to update.
#
# Settings, as environment variables (pass them to sh: curl ... | KENSO_VERSION=0.1.120 sh):
#   KENSO_VERSION      the version to install (default: the latest)
#   KENSO_INSTALL_DIR  where the CLI goes (default: ~/.local/share/kenso)
#   KENSO_BIN_DIR      where the kenso link goes (default: ~/.local/bin)
#   KENSO_DOWNLOAD_URL where the builds are (default: https://downloads.ken.so/desktop/cli)
# On Windows, or with Node 22 or newer already installed, see https://ken.so/cli for the other ways to install.
# Source: ken-website public/install.sh (DEV-11140). src/lib/cli-install-script.test.ts runs it.
set -eu

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

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

# Everything runs from main, called on the last line: when `curl | sh` is cut off mid-download, sh has only
# defined functions and runs nothing.
main() {
  base=${KENSO_DOWNLOAD_URL:-https://downloads.ken.so/desktop/cli}
  # Without a trailing slash, so "$install_dir.new" sits beside the install, never inside it.
  install_dir=${KENSO_INSTALL_DIR:-$HOME/.local/share/kenso}
  install_dir=${install_dir%/}
  bin_dir=${KENSO_BIN_DIR:-$HOME/.local/bin}
  bin_dir=${bin_dir%/}
  if [ -z "$install_dir" ] || [ -z "$bin_dir" ]; then die "KENSO_INSTALL_DIR and KENSO_BIN_DIR cannot be /"; fi

  case $(uname -s) in
    Darwin) os=darwin ;;
    Linux) os=linux ;;
    *) die "this script installs on macOS and Linux. For Windows, see https://ken.so/cli" ;;
  esac
  case $(uname -m) in
    x86_64 | amd64) arch=x64 ;;
    arm64 | aarch64) arch=arm64 ;;
    *) die "there is no build for $(uname -m). With Node 22 or newer, install with: npm i -g kenso" ;;
  esac
  # A shell under Rosetta reports x86_64 on an Apple silicon Mac: install the native build.
  if [ "$os" = darwin ] && [ "$arch" = x64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then
    arch=arm64
  fi
  # The Node.js inside is built for glibc.
  if [ "$os" = linux ] && ldd --version 2>&1 | grep -qi musl; then
    die "this build needs glibc, and this Linux uses musl (Alpine). With Node 22 or newer, install with: npm i -g kenso"
  fi

  # Only ever replace an earlier kenso install, never another folder someone pointed KENSO_INSTALL_DIR at,
  # and only the link this installer made, never npm's or another kenso.
  if [ -e "$install_dir" ] && ! { [ -f "$install_dir/bin/kenso" ] && [ -d "$install_dir/lib/kenso" ]; }; then
    die "$install_dir exists and is not a kenso install. Move it, or set KENSO_INSTALL_DIR to another folder."
  fi
  if { [ -e "$bin_dir/kenso" ] || [ -L "$bin_dir/kenso" ]; } &&
    [ "$(readlink "$bin_dir/kenso" 2>/dev/null || true)" != "$install_dir/bin/kenso" ]; then
    die "$bin_dir/kenso exists and is not a link from this installer. Move it, or set KENSO_BIN_DIR to another folder."
  fi

  tmp=$(mktemp -d)
  trap 'rm -rf "$tmp"' EXIT
  trap 'exit 130' INT TERM

  version=${KENSO_VERSION:-}
  if [ -z "$version" ]; then
    fetch "$base/latest.yml" "$tmp/latest.yml" ||
      die "could not read $base/latest.yml to find the newest version. With Node 22 or newer, install with: npm i -g kenso"
    version=$(sed -n 's/^version: *//p' "$tmp/latest.yml" | tr -d '\r')
    [ -n "$version" ] || die "$base/latest.yml names no version"
  fi
  name="kenso-$version-$os-$arch"
  say "Installing kenso $version for $os-$arch"
  fetch "$base/$version/$name.tar.gz" "$tmp/$name.tar.gz" || die "could not download $base/$version/$name.tar.gz"
  fetch "$base/$version/SHA256SUMS" "$tmp/SHA256SUMS" || die "could not download $base/$version/SHA256SUMS"

  expected=$(awk -v file="$name.tar.gz" '$2 == file { print $1 }' "$tmp/SHA256SUMS")
  [ -n "$expected" ] || die "SHA256SUMS lists no $name.tar.gz"
  if command -v sha256sum >/dev/null 2>&1; then
    actual=$(sha256sum "$tmp/$name.tar.gz" | awk '{ print $1 }')
  elif command -v shasum >/dev/null 2>&1; then
    actual=$(shasum -a 256 "$tmp/$name.tar.gz" | awk '{ print $1 }')
  else
    die "needs sha256sum or shasum to check the download"
  fi
  [ "$actual" = "$expected" ] || die "$name.tar.gz has sha256 $actual, but SHA256SUMS says $expected. Nothing was installed."

  tar -xzf "$tmp/$name.tar.gz" -C "$tmp"
  mkdir -p "$(dirname "$install_dir")" "$bin_dir"
  rm -rf "$install_dir.new"
  mv "$tmp/$name" "$install_dir.new"
  rm -rf "$install_dir"
  mv "$install_dir.new" "$install_dir"
  ln -sfn "$install_dir/bin/kenso" "$bin_dir/kenso"

  installed=$("$bin_dir/kenso" --version) || die "the installed kenso did not start. Run $bin_dir/kenso --version to see why."
  say "Installed kenso $installed at $bin_dir/kenso"
  case ":$PATH:" in
    *":$bin_dir:"*)
      found=$(command -v kenso || true)
      if [ -n "$found" ] && [ "$found" != "$bin_dir/kenso" ]; then
        say "Another kenso comes first on your PATH: $found. Remove it (npm rm -g kenso) or put $bin_dir first."
      fi
      ;;
    *) say "Add $bin_dir to your PATH: echo 'export PATH=\"$bin_dir:\$PATH\"' >> ~/.profile (~/.zshrc for zsh), then open a new terminal." ;;
  esac
  say "Next: kenso auth login"
}

main "$@"
