#!/bin/sh
# Installs the kanban CLI from https://kanban.infopanel.org
# Usage: curl -fsSL https://kanban.infopanel.org/install | sh
set -eu

DIR="${KANBAN_INSTALL_DIR:-$HOME/.local/bin}"

if command -v node >/dev/null 2>&1 && node -e 'process.exit(Number(process.versions.node.split(".")[0]) >= 18 ? 0 : 1)'; then
  RUNTIME=node
elif command -v bun >/dev/null 2>&1; then
  RUNTIME=bun
else
  echo "kanban: needs Node.js 18+ or Bun. Install one of them and re-run this script." >&2
  exit 1
fi

mkdir -p "$DIR"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
curl -fsSL "https://kanban.infopanel.org/kanban.js" -o "$WORK/download"
if [ "$(head -c 2 "$WORK/download")" != "#!" ]; then
  echo "kanban: the download from https://kanban.infopanel.org/kanban.js doesn't look like the CLI. Try again in a minute." >&2
  exit 1
fi
# Point the shebang at whichever runtime is available, and check it runs before replacing anything.
{ echo "#!/usr/bin/env $RUNTIME"; tail -n +2 "$WORK/download"; } > "$WORK/kanban"
chmod +x "$WORK/kanban"
if ! VERSION="$("$WORK/kanban" --version)"; then
  echo "kanban: the downloaded CLI failed to run with $RUNTIME." >&2
  exit 1
fi
mv "$WORK/kanban" "$DIR/kanban"

echo "Installed $VERSION to $DIR/kanban"
case ":$PATH:" in
  *":$DIR:"*) ;;
  *) echo "Add it to your PATH:  export PATH=\"$DIR:\$PATH\"" ;;
esac
echo "Next: kanban init <project-url>   (or: kanban new \"Project title\")"
