#!/usr/bin/env sh
set -eu

download_base_url="${PROGRAMMOR_CLI_DOWNLOAD_BASE_URL:-https://public.programmor.com/cli}"
requested_version="${PROGRAMMOR_CLI_VERSION:-stable}"

if [ -n "${PROGRAMMOR_CLI_INSTALL_DIR:-}" ]; then
  install_directory="$PROGRAMMOR_CLI_INSTALL_DIR"
elif [ -n "${HOME:-}" ]; then
  install_directory="$HOME/.local/bin"
else
  echo "HOME or PROGRAMMOR_CLI_INSTALL_DIR must be set." >&2
  exit 1
fi

case "$download_base_url" in
  https://*) curl_protocol="=https" ;;
  http://localhost/* | http://localhost:* | http://127.0.0.1/* | http://127.0.0.1:*)
    curl_protocol="=http,https"
    ;;
  *)
    echo "PROGRAMMOR_CLI_DOWNLOAD_BASE_URL must use HTTPS or loopback HTTP." >&2
    exit 1
    ;;
esac
download_base_url="${download_base_url%/}"

if [ "$(uname -s)" != "Linux" ]; then
  echo "Programmor CLI currently supports Linux only." >&2
  exit 1
fi

case "$(uname -m)" in
  x86_64 | amd64) architecture="x64" ;;
  aarch64 | arm64) architecture="arm64" ;;
  *)
    echo "Unsupported Linux architecture: $(uname -m)" >&2
    exit 1
    ;;
esac

temporary_directory="$(mktemp -d)"
target_temporary=""
cleanup() {
  rm -rf "$temporary_directory"
  if [ -n "$target_temporary" ]; then
    rm -f "$target_temporary"
  fi
}
trap cleanup EXIT HUP INT TERM

download() {
  curl -fsSL \
    --proto "$curl_protocol" \
    --tlsv1.2 \
    --retry 3 \
    --retry-delay 1 \
    "$1" \
    -o "$2"
}

if [ "$requested_version" = "stable" ] || [ "$requested_version" = "latest" ]; then
  download "$download_base_url/channels/stable" "$temporary_directory/stable"
  version="$(tr -d '\r\n' < "$temporary_directory/stable")"
else
  version="$requested_version"
fi

if ! printf '%s\n' "$version" | grep -Eq '^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$'; then
  echo "Invalid Programmor CLI release version: $version" >&2
  exit 1
fi

expected_version="${version#v}"
target="$install_directory/programmor-cli"
current_version=""
if [ -e "$target" ]; then
  if [ -d "$target" ] || [ -L "$target" ]; then
    echo "Installation target must be a regular file: $target" >&2
    exit 1
  fi
  if [ -x "$target" ]; then
    current_version="$("$target" version --output human 2>/dev/null || true)"
  fi
fi

if [ "$current_version" = "$expected_version" ]; then
  echo "Programmor CLI $expected_version is already installed at $target"
  exit 0
fi

archive="programmor-cli-linux-${architecture}.tar.gz"
release_url="$download_base_url/releases/$version"
download "$release_url/$archive" "$temporary_directory/$archive"
download "$release_url/$archive.sha256" "$temporary_directory/$archive.sha256"

expected_checksum="$(sed -n '1{s/[[:space:]].*$//;p;}' "$temporary_directory/$archive.sha256")"
if ! printf '%s\n' "$expected_checksum" | grep -Eq '^[0-9a-fA-F]{64}$'; then
  echo "The release checksum is invalid." >&2
  exit 1
fi
actual_checksum="$(sha256sum "$temporary_directory/$archive" | sed 's/[[:space:]].*$//')"
if [ "$actual_checksum" != "$expected_checksum" ]; then
  echo "The release checksum does not match." >&2
  exit 1
fi

archive_entries="$(tar -tzf "$temporary_directory/$archive")"
if [ "$archive_entries" != "programmor-cli" ]; then
  echo "The release archive has an unexpected layout." >&2
  exit 1
fi
tar -xzf "$temporary_directory/$archive" -C "$temporary_directory"
if [ ! -f "$temporary_directory/programmor-cli" ] || [ -L "$temporary_directory/programmor-cli" ]; then
  echo "The release archive does not contain a regular programmor-cli executable." >&2
  exit 1
fi
chmod 0755 "$temporary_directory/programmor-cli"

downloaded_version="$("$temporary_directory/programmor-cli" version --output human 2>/dev/null || true)"
if [ "$downloaded_version" != "$expected_version" ]; then
  echo "The downloaded executable reports version $downloaded_version, expected $expected_version." >&2
  exit 1
fi

mkdir -p "$install_directory"
target_temporary="$(mktemp "$install_directory/.programmor-cli.XXXXXX")"
install -m 0755 "$temporary_directory/programmor-cli" "$target_temporary"
mv -f "$target_temporary" "$target"
target_temporary=""

if [ -n "$current_version" ]; then
  echo "Updated Programmor CLI from $current_version to $expected_version at $target"
else
  echo "Installed Programmor CLI $expected_version to $target"
fi
