#!/usr/bin/env bash
# Public installer for the kompressius binary.
# Prefers a GitHub release asset and its SHA256SUMS.txt entry.
# Builds from the repository when that asset is not published yet.
set -euo pipefail

RELEASE_BASE="https://github.com/administrakt0r/kompress-tokens/releases/latest/download"
REPO="https://github.com/administrakt0r/kompress-tokens.git"
INSTALL_DIR="${HOME}/.local/bin"

OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$ARCH" in
  x86_64|amd64) ARCH="x86_64" ;;
  aarch64|arm64) ARCH="aarch64" ;;
  *)
    echo "unsupported architecture: $ARCH" >&2
    exit 1
    ;;
esac
case "$OS" in
  linux) TARGET="${ARCH}-unknown-linux-gnu" ;;
  darwin) TARGET="${ARCH}-apple-darwin" ;;
  *)
    echo "unsupported os: $OS" >&2
    exit 1
    ;;
esac

ASSET="kompressius-${TARGET}.tar.gz"
mkdir -p "$INSTALL_DIR"

install_binary() {
  local src="$1"
  if [ -e "${INSTALL_DIR}/kompressius" ]; then
    mv "${INSTALL_DIR}/kompressius" "${INSTALL_DIR}/kompressius.bak"
  fi
  install -m 755 "$src" "${INSTALL_DIR}/kompressius"
}

ensure_path() {
  case ":${PATH}:" in
    *":${INSTALL_DIR}:"*) ;;
    *)
      touch "${HOME}/.profile"
      if ! grep -F '.local/bin' "${HOME}/.profile" >/dev/null 2>&1; then
        printf '\nexport PATH="$HOME/.local/bin:$PATH"\n' >> "${HOME}/.profile"
      fi
      echo "Add ${INSTALL_DIR} to PATH, or open a new shell so ~/.profile is read."
      ;;
  esac
}

try_release() {
  local workdir asset_path sums expected actual
  workdir="$(mktemp -d)"
  asset_path="${workdir}/${ASSET}"
  if ! curl -fsSL "${RELEASE_BASE}/${ASSET}" -o "$asset_path"; then
    rm -rf "$workdir"
    return 1
  fi
  if ! curl -fsSL "${RELEASE_BASE}/SHA256SUMS.txt" -o "${workdir}/SHA256SUMS.txt"; then
    echo "release asset downloaded but SHA256SUMS.txt is missing" >&2
    rm -rf "$workdir"
    exit 1
  fi
  expected="$(awk -v file="$ASSET" '$2 == file { print $1 }' "${workdir}/SHA256SUMS.txt")"
  if [ -z "$expected" ]; then
    echo "checksum entry missing for ${ASSET}" >&2
    rm -rf "$workdir"
    exit 1
  fi
  if command -v sha256sum >/dev/null 2>&1; then
    actual="$(sha256sum "$asset_path" | awk '{ print $1 }')"
  else
    actual="$(shasum -a 256 "$asset_path" | awk '{ print $1 }')"
  fi
  if [ "$expected" != "$actual" ]; then
    echo "checksum mismatch" >&2
    rm -rf "$workdir"
    exit 1
  fi
  tar -xzf "$asset_path" -C "$workdir"
  if [ ! -f "${workdir}/kompressius" ]; then
    echo "archive did not contain kompressius" >&2
    rm -rf "$workdir"
    exit 1
  fi
  install_binary "${workdir}/kompressius"
  rm -rf "$workdir"
}

build_from_source() {
  echo "No published release for ${ASSET}. Building from source."
  if ! command -v git >/dev/null 2>&1; then
    echo "git is required to build from source." >&2
    exit 1
  fi
  if ! command -v cargo >/dev/null 2>&1; then
    echo "Rust is required. Install it from https://rustup.rs and run this script again." >&2
    exit 1
  fi
  local src
  src="$(mktemp -d)"
  git clone --depth 1 "$REPO" "$src"
  (cd "${src}/kompressius" && cargo build --release -p kompressius-cli)
  install_binary "${src}/kompressius/target/release/kompressius"
  rm -rf "$src"
}

if ! try_release; then
  build_from_source
fi
ensure_path
"${INSTALL_DIR}/kompressius" --version
echo "Installed ${INSTALL_DIR}/kompressius"
echo "Next: kompressius start"
