#!/bin/sh
# gitHrum (깃흐름) installer for macOS and Linux — version 0.1.16.
#   curl -fsSL https://console.hrum.ai/git/install.sh | sh
# Downloads the single executable for this OS/CPU (no Node.js needed), checks its
# SHA-256 BEFORE it is run, puts it under ~/.local/share/hrum/git/<version> and a
# `hrum` link in ~/.local/bin. Where no executable is listed for this system (or
# its download is not available), it installs hrum-git-0.1.16.tgz instead,
# which needs Node.js 20 or newer. The macOS executables are Developer ID-signed
# and notarized; the SHA-256 check is the integrity guard everywhere.
set -eu

VERSION="0.1.16"
SHA256="7626e2d0422f39446283a4def126620383cea08dcb89ca823d67f90c92713834"
BASE="${HRUM_GIT_BASE_URL:-https://console.hrum.ai/git}"
BIN_BASE="${HRUM_GIT_BIN_BASE_URL:-${HRUM_GIT_BASE_URL:-https://dl.hrum.ai/git}}"
TGZ="hrum-git-${VERSION}.tgz"
# SHA-256 per target; empty = no executable for that target in this release.
SHA_DARWIN_ARM64="10e141df6051e02c5b3e8410909e8ea489ed347ba1406344f402cdc58c6ed2a2"
SHA_DARWIN_X64="d03548b6f59aa7fccaaf10f5990766aacfaa14f474e3adf783f5d5fd72c50e4f"
SHA_LINUX_X64="f32e67fea2f1069f53442e379f94930f4921a48e6685e03ed9ba0cf71e7722d2"
SHA_LINUX_ARM64="3e4268dff32ae41b0a7add1e6bb50b6a718dc03b2a0bc4921ec3d0a23583778f"

case "${HRUM_LANG:-${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}}" in ko*) KO=1 ;; *) KO=0 ;; esac
say() { if [ "$KO" = 1 ]; then printf '%s\n' "$1"; else printf '%s\n' "$2"; fi; }
die() { say "$1" "$2" >&2; exit 1; }

for u in "$BASE" "$BIN_BASE"; do
  case "$u" in
    https://*) ;;
    http://127.0.0.1*|http://localhost*) ;;
    *) die "설치 주소는 https 여야 해요: $u" "The download address must be https: $u" ;;
  esac
done

TMP=$(mktemp -d 2>/dev/null || mktemp -d -t hrum)
trap 'rm -rf "${TMP:?}"' EXIT INT TERM

fetch() { # fetch <url> <file>; non-zero when the download fails
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL --proto '=https,http' -o "$2" "$1"
  elif command -v wget >/dev/null 2>&1; then
    wget -q -O "$2" "$1"
  else
    die "curl 이나 wget 이 필요해요." "curl or wget is needed."
  fi
}
sum_of() {
  if command -v sha256sum >/dev/null 2>&1; then
    sha256sum "$1" | awk '{print $1}'
  elif command -v shasum >/dev/null 2>&1; then
    shasum -a 256 "$1" | awk '{print $1}'
  else
    die "SHA-256 을 확인할 도구(sha256sum/shasum)가 없어요." "No SHA-256 tool (sha256sum/shasum) found."
  fi
}
mismatch() {
  die "내려받은 파일이 맞지 않아요 (SHA-256 불일치). 설치하지 않았어요." \
      "The download does not match (SHA-256 mismatch). Nothing was installed."
}

# Which executable fits this computer.
OS=$(uname -s 2>/dev/null || echo unknown)
ARCH=$(uname -m 2>/dev/null || echo unknown)
TARGET=""
case "$OS" in
  Darwin)
    # An x86_64 shell under Rosetta on Apple silicon still gets the arm64 build.
    if [ "$ARCH" = x86_64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then ARCH=arm64; fi
    case "$ARCH" in arm64) TARGET=darwin-arm64 ;; x86_64) TARGET=darwin-x64 ;; esac
    ;;
  Linux)
    # The official Node builds need glibc: musl systems (Alpine) take the tgz path.
    if ! ls /lib/ld-musl-* >/dev/null 2>&1; then
      case "$ARCH" in x86_64|amd64) TARGET=linux-x64 ;; aarch64|arm64) TARGET=linux-arm64 ;; esac
    fi
    ;;
esac
EXE_SHA=""
case "$TARGET" in
  darwin-arm64) EXE_SHA="$SHA_DARWIN_ARM64" ;;
  darwin-x64) EXE_SHA="$SHA_DARWIN_X64" ;;
  linux-x64) EXE_SHA="$SHA_LINUX_X64" ;;
  linux-arm64) EXE_SHA="$SHA_LINUX_ARM64" ;;
esac

DATA="${XDG_DATA_HOME:-$HOME/.local/share}/hrum/git"
DEST="$DATA/$VERSION"
BIN="${HRUM_BIN_DIR:-$HOME/.local/bin}"
mkdir -p "$DATA" "$BIN"

MODE=""
if [ -n "$EXE_SHA" ]; then
  EXE="hrum-git-${VERSION}-${TARGET}"
  if fetch "$BIN_BASE/$VERSION/$EXE" "$TMP/hrum" 2>/dev/null; then
    [ "$(sum_of "$TMP/hrum")" = "$EXE_SHA" ] || mismatch
    chmod 755 "$TMP/hrum"
    rm -rf "${DEST:?}.tmp"
    mkdir -p "$DEST.tmp"
    mv "$TMP/hrum" "$DEST.tmp/hrum"
    rm -rf "${DEST:?}"
    mv "$DEST.tmp" "$DEST"
    ln -sf "$DEST/hrum" "$BIN/hrum.tmp"
    mv -f "$BIN/hrum.tmp" "$BIN/hrum"
    MODE=exe
  else
    say "실행 파일을 받지 못했어요. Node.js 로 도는 설치로 바꿀게요." \
        "Could not download the executable. Falling back to the Node.js install."
  fi
fi

if [ -z "$MODE" ]; then
  command -v node >/dev/null 2>&1 || die \
    "Node.js 20 이상이 필요해요. https://nodejs.org 에서 설치한 뒤 다시 실행해 주세요." \
    "Node.js 20 or newer is needed. Install it from https://nodejs.org and run this again."
  NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)
  [ "$NODE_MAJOR" -ge 20 ] 2>/dev/null || die \
    "Node.js 20 이상이 필요해요 (지금 $(node -v 2>/dev/null)). https://nodejs.org" \
    "Node.js 20 or newer is needed (found $(node -v 2>/dev/null)). https://nodejs.org"

  fetch "$BASE/$TGZ" "$TMP/$TGZ" || die "내려받지 못했어요: $BASE/$TGZ" "Download failed: $BASE/$TGZ"
  [ "$(sum_of "$TMP/$TGZ")" = "$SHA256" ] || mismatch

  rm -rf "${DEST:?}.tmp"
  mkdir -p "$DEST.tmp"
  tar -xzf "$TMP/$TGZ" -C "$DEST.tmp"
  [ -f "$DEST.tmp/package/dist/hrum.cjs" ] || die "설치 파일이 이상해요." "The package looks broken."
  rm -rf "${DEST:?}"
  mv "$DEST.tmp" "$DEST"

  rm -f "$BIN/hrum.tmp"
  cat > "$BIN/hrum.tmp" <<EOF
#!/bin/sh
exec node "$DEST/package/dist/hrum.cjs" "\$@"
EOF
  chmod 755 "$BIN/hrum.tmp"
  mv -f "$BIN/hrum.tmp" "$BIN/hrum"
fi

# Older versions take 100MB+ each as executables: keep only this one.
for old in "$DATA"/*; do
  [ -d "$old" ] || continue
  case "${old##*/}" in
    "$VERSION") ;;
    [0-9]*.[0-9]*.[0-9]*) rm -rf "${old:?}" ;;
  esac
done

case ":$PATH:" in
  *":$BIN:"*) ;;
  *)
    if [ "${HRUM_NO_MODIFY_PATH:-}" != 1 ]; then
      case "${SHELL:-}" in
        */zsh) RC="$HOME/.zshrc" ;;
        */bash) if [ "$(uname)" = Darwin ]; then RC="$HOME/.bash_profile"; else RC="$HOME/.bashrc"; fi ;;
        *) RC="$HOME/.profile" ;;
      esac
      LINE="export PATH=\"$BIN:\$PATH\" # hrum"
      grep -qs '# hrum$' "$RC" || printf '\n%s\n' "$LINE" >> "$RC"
      say "$RC 에 PATH 를 추가했어요. 새 터미널을 열면 hrum 을 쓸 수 있어요." \
          "Added $BIN to PATH in $RC. Open a new terminal to use hrum."
    else
      say "PATH 에 $BIN 을 넣어 주세요." "Add $BIN to your PATH."
    fi
    ;;
esac

say "깃흐름 프로그램 $VERSION 을 설치했어요. 다음: hrum login" \
    "Installed gitHrum $VERSION. Next: hrum login"
