#!/bin/sh
# Generated from validated, version-pinned release metadata.
# SHA256 verifies integrity against that metadata, not independent authenticity.
set -eu
umask 077
LC_ALL=C; export LC_ALL
unset TAR_OPTIONS GZIP
fail() { printf '%s\n' "erlkonig installer: $*" >&2; exit 1; }
for tool in uname curl tar wc awk sort tr mktemp mkdir chmod mv ln readlink cat rm rmdir; do
  command -v "$tool" >/dev/null 2>&1 || fail "required tool missing: $tool"
done
if command -v sha256sum >/dev/null 2>&1; then
  digest() { sha256sum "$1" | awk '{print $1}'; }
elif command -v shasum >/dev/null 2>&1; then
  digest() { shasum -a 256 "$1" | awk '{print $1}'; }
else
  fail 'sha256sum or shasum is required'
fi
case "$(uname -s):$(uname -m)" in
  Darwin:arm64)
    target=aarch64-apple-darwin
    url=https://erlkonig.ai/downloads/erlkonig-cli-0.1.2-aarch64-apple-darwin.tar.gz
    archive_size=31950266
    archive_sha=93ac715f1710177128f913808d1215aa716adf60ecb20cb79310bf858d484ee2
    binary_size=80753584
    binary_sha=725f31418a0e64cf0fa53eb9dcaf85ab556ad2804f0f2aba4aea874c6d978a36
    notices_size=13031809
    notices_sha=fb103f1d459b2f4c442bf52045a26d9a1435ceef351a4721fb459bf87c77b602
    ;;
  Linux:aarch64|Linux:arm64)
    target=aarch64-unknown-linux-gnu
    url=https://erlkonig.ai/downloads/erlkonig-cli-0.1.2-aarch64-unknown-linux-gnu.tar.gz
    archive_size=33951196
    archive_sha=72d135dbb51ed488c9b97828e45f9e9e582c3e7424f6df5bd9c8ea36ebc448d3
    binary_size=86269752
    binary_sha=b16ae993406925e5e079192bded153e6b97201f8f744a6e0df3abaad51043e2a
    notices_size=13031809
    notices_sha=fb103f1d459b2f4c442bf52045a26d9a1435ceef351a4721fb459bf87c77b602
    ;;
  Linux:x86_64)
    target=x86_64-unknown-linux-gnu
    url=https://erlkonig.ai/downloads/erlkonig-cli-0.1.2-x86_64-unknown-linux-gnu.tar.gz
    archive_size=36536093
    archive_sha=df10157d032db23c41b623e771c2e744f1e6323ec1e27a6a6dd9e2e5eb8b7d18
    binary_size=96731488
    binary_sha=c331632535a35560f2d0ec3ab0da096e1762c56aa8a1c0c8ec07d90a2856eb03
    notices_size=13031809
    notices_sha=fb103f1d459b2f4c442bf52045a26d9a1435ceef351a4721fb459bf87c77b602
    ;;
  *) fail 'this operating system/architecture has no supported CLI artifact' ;;
esac
version=0.1.2
: "${HOME:?HOME must identify your user directory}"
case "$HOME" in /*) ;; *) fail 'HOME must be absolute' ;; esac
store="$HOME/.local/share/erlkonig-cli"
bindir="$HOME/.local/bin"
binary="$bindir/erlkonig"
owned='erlkonig-cli-installer-v1'
mkdir -p "$HOME/.local/share" "$bindir"
[ ! -L "$store" ] || fail 'installer storage must not be a symlink'
if mkdir "$store" 2>/dev/null; then
  printf '%s\n' "$owned" > "$store/.owner"
else
  [ -f "$store/.owner" ] && [ "$(cat "$store/.owner")" = "$owned" ] || fail 'existing installer storage is not owned by this installer'
fi
lock="$store/.install-lock"
mkdir "$lock" 2>/dev/null || fail 'another install is active, or its lock needs inspection'
stage= link_stage=
cleanup() {
  [ -z "$link_stage" ] || rm -f "$link_stage"
  [ -z "$stage" ] || rm -rf "$stage"
  rmdir "$lock" 2>/dev/null || :
}
trap cleanup EXIT
trap 'exit 130' HUP INT TERM
owned_link() {
  if [ -L "$binary" ]; then
    old=$(readlink "$binary") || fail 'cannot read existing erlkonig link'
    case "$old" in "$store/"*/erlkonig) ;; *) fail 'existing erlkonig link belongs to another installation' ;; esac
    old_dir=${old%/erlkonig}
    [ ! -L "$old_dir" ] && [ -f "$old_dir/.owner" ] && [ "$(cat "$old_dir/.owner")" = "$owned" ] && [ -f "$old" ] && [ ! -L "$old" ] || fail 'existing erlkonig link has no owned installation'
  elif [ -e "$binary" ]; then
    fail 'existing erlkonig is not owned by this installer'
  fi
}
owned_link
stage=$(mktemp -d "$store/.stage.XXXXXX") || fail 'cannot create private staging directory'
archive="$stage/package.tar.gz"
curl --fail --location --proto '=https' --proto-redir '=https' --max-filesize "$archive_size" --output "$archive" "$url" || fail 'download failed'
check_file() {
  actual_size=$(wc -c < "$1" | tr -d '[:space:]')
  [ "$actual_size" = "$2" ] && [ "$(digest "$1")" = "$3" ] || fail 'download/member integrity mismatch'
}
check_file "$archive" "$archive_size" "$archive_sha"
tar -tzf "$archive" > "$stage/names" || fail 'cannot list archive'
sort "$stage/names" > "$stage/sorted-names"
expected=$(printf '%s\n' THIRD_PARTY_NOTICES.txt erlkonig)
[ "$(cat "$stage/sorted-names")" = "$expected" ] || fail 'archive must contain exactly the two expected flat members'
tar -tvzf "$archive" > "$stage/types" || fail 'cannot inspect archive types'
awk 'substr($0,1,1)!="-" {exit 1} END {if(NR!=2) exit 1}' "$stage/types" || fail 'archive members must be regular files'
# Only fixed members are streamed to fixed private files; never expand paths.
tar -xOzf "$archive" -- erlkonig > "$stage/erlkonig" || fail 'cannot read executable member'
tar -xOzf "$archive" -- THIRD_PARTY_NOTICES.txt > "$stage/THIRD_PARTY_NOTICES.txt" || fail 'cannot read notices member'
check_file "$stage/erlkonig" "$binary_size" "$binary_sha"
check_file "$stage/THIRD_PARTY_NOTICES.txt" "$notices_size" "$notices_sha"
chmod 755 "$stage/erlkonig"
chmod 644 "$stage/THIRD_PARTY_NOTICES.txt"
printf '%s\n' "$owned" > "$stage/.owner"
rm -f "$archive" "$stage/names" "$stage/sorted-names" "$stage/types"
version_dir="$store/$version-$target"
if [ -e "$version_dir" ] || [ -L "$version_dir" ]; then
  [ ! -L "$version_dir" ] && [ -f "$version_dir/.owner" ] && [ "$(cat "$version_dir/.owner")" = "$owned" ] || fail 'existing version directory is not owned'
  [ ! -L "$version_dir/erlkonig" ] && [ ! -L "$version_dir/THIRD_PARTY_NOTICES.txt" ] || fail 'owned version members must not be symlinks'
  check_file "$version_dir/erlkonig" "$binary_size" "$binary_sha"
  check_file "$version_dir/THIRD_PARTY_NOTICES.txt" "$notices_size" "$notices_sha"
else
  mv "$stage" "$version_dir" || fail 'cannot commit version directory'
  stage=
fi
owned_link
link_stage="$bindir/.erlkonig-link-$$"
# ln refuses a collision; do not overwrite unrelated temporary paths.
ln -s "$version_dir/erlkonig" "$link_stage" || { link_stage=; fail 'cannot stage executable link'; }
mv -f "$link_stage" "$binary" || fail 'cannot commit executable link'
link_stage=
printf 'Installed Erlkonig CLI %s at %s\nNotices: %s/THIRD_PARTY_NOTICES.txt\n' "$version" "$binary" "$version_dir"
printf 'If needed, add %s to PATH in your shell settings. No settings were changed.\n' "$bindir"
