#!/bin/sh
# Installs or updates the Bumpwise command line (https://gilware.com/bumpwise/cli/), on Linux and macOS:
#   curl -fsSL https://gilware.com/bumpwise/cli/install.sh | sh
# - the jar goes to ~/.local/share/bumpwise/bumpwise.jar (the same path for every version, so updates need no other change)
# - a "bumpwise" command goes to ~/.local/bin
# - it asks before adding ~/.local/bin to your PATH and before setting up Claude Code
# Run it again to update. It downloads the jar from gilware.com and sends nothing else.
# Without questions (CI): BUMPWISE_NO_PROMPT=1. Other folders: BUMPWISE_DIR, BUMPWISE_BIN.
set -eu

VERSION=1.1.0
SHA256=a26e3bf8894f7640acbae604502b0341cf1e894bd084737ba3bc1eae2d65b976
URL="https://gilware.com/bumpwise/cli/bumpwise-$VERSION.jar"
DIR="${BUMPWISE_DIR:-$HOME/.local/share/bumpwise}"
BIN="${BUMPWISE_BIN:-$HOME/.local/bin}"
JAR="$DIR/bumpwise.jar"

say() { printf '%s\n' "$*"; }
# a path as people write it: ~/.local/bin instead of /home/you/.local/bin
tilde() { case "$1" in "$HOME"/*) printf '~/%s' "${1#"$HOME"/}" ;; *) printf '%s' "$1" ;; esac; }
fail() { printf '\nBumpwise was not installed: %s\n' "$*" >&2; exit 1; }

# yes/no question on the terminal, also when the script comes through a pipe; "no" when nobody can answer
ask() {
  [ "${BUMPWISE_NO_PROMPT:-}" = 1 ] && return 1
  ( : </dev/tty ) 2>/dev/null || return 1
  printf '%s [Y/n] ' "$1" >/dev/tty
  read -r answer </dev/tty || return 1
  case "$answer" in [nN]*) return 1 ;; *) return 0 ;; esac
}

say "Bumpwise command line $VERSION"
say ""

# 1. Java 17 or later
java_output=$(java -version 2>&1) || java_output=""
java_version=$(printf '%s\n' "$java_output" | sed -n 's/.*version "\([^"]*\)".*/\1/p' | head -n 1)
java_major=$(printf '%s' "$java_version" | cut -d. -f1)
[ "$java_major" = 1 ] && java_major=$(printf '%s' "$java_version" | cut -d. -f2)
case "$java_major" in
  ''|*[!0-9]*) fail "Java was not found. Bumpwise needs Java 17 or later: install one (for example from https://adoptium.net), open a new terminal and run this command again." ;;
esac
[ "$java_major" -ge 17 ] || fail "Bumpwise needs Java 17 or later, and 'java' is version $java_version. Install a newer one (for example from https://adoptium.net) and run this command again."
say "  Java $java_version: OK"

# 2. download and check
if command -v sha256sum >/dev/null 2>&1; then sha() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then sha() { shasum -a 256 "$1" | cut -d' ' -f1; }
else fail "sha256sum or shasum is needed to check the download."
fi
previous=""
[ -f "$JAR" ] && previous=$(java -jar "$JAR" --version 2>/dev/null | sed -n 's/^bumpwise //p' | head -n 1)
mkdir -p "$DIR" "$BIN"
tmp="$DIR/.download.part"
trap 'rm -f "$tmp"' EXIT
curl -fsSL -o "$tmp" "$URL" || fail "the download from $URL failed. Check your internet connection and try again."
[ "$(sha "$tmp")" = "$SHA256" ] || fail "the downloaded file is not the published one (SHA-256 mismatch). Nothing was changed; try again later."
mv -f "$tmp" "$JAR"
printf '#!/bin/sh\nexec java -jar "%s" "$@"\n' "$JAR" > "$BIN/bumpwise"
chmod +x "$BIN/bumpwise"
if [ -n "$previous" ] && [ "$previous" != "$VERSION" ]; then
  say "  Updated from $previous to $VERSION"
elif [ -n "$previous" ]; then
  say "  $VERSION was already installed: reinstalled"
else
  say "  Installed $(tilde "$JAR")"
fi

# 3. the bumpwise command on the PATH
command_ready=1
case ":$PATH:" in
  *":$BIN:"*) ;;
  *)
    command_ready=0
    case "${SHELL:-}" in
      */zsh) profile="$HOME/.zshrc" ;;
      */bash) [ "$(uname)" = Darwin ] && profile="$HOME/.bash_profile" || profile="$HOME/.bashrc" ;;
      *) profile="$HOME/.profile" ;;
    esac
    if grep -qs "$BIN" "$profile"; then
      say "  $(tilde "$BIN") is already added to your PATH in $(tilde "$profile")"
    elif ask "  Add $(tilde "$BIN") to your PATH in $(tilde "$profile"), so that the 'bumpwise' command works?"; then
      printf '\n# Bumpwise command line\nexport PATH="%s:$PATH"\n' "$BIN" >> "$profile"
      say "  Added to $(tilde "$profile")"
    else
      profile=""
    fi
    ;;
esac

# 4. Claude Code
if command -v claude >/dev/null 2>&1; then
  existing=$(claude mcp get bumpwise </dev/null 2>/dev/null || true)
  case "$existing" in
    *"$JAR"*) say "  Claude Code: already set up" ;;
    *)
      if ask "  Set up Bumpwise for Claude Code, in all your projects?"; then
        [ -n "$existing" ] && claude mcp remove -s user bumpwise </dev/null >/dev/null 2>&1 || true
        if claude mcp add -s user bumpwise -- java -jar "$JAR" mcp </dev/null >/dev/null 2>&1; then
          say "  Claude Code: set up (check with: claude mcp list)"
        else
          say "  Claude Code: could not set it up. Run: claude mcp add -s user bumpwise -- java -jar \"$JAR\" mcp"
        fi
      fi
      ;;
  esac
fi

say ""
say "Done. Next: open a terminal in your library's folder, build it, and run:"
say ""
if [ "$command_ready" = 1 ]; then
  say "    bumpwise check"
elif [ -n "${profile:-}" ]; then
  say "    bumpwise check        (in a new terminal window)"
else
  say "    $(tilde "$BIN")/bumpwise check"
fi
say ""
say "To update later, run the same install command again. Help: https://gilware.com/bumpwise/cli/"
