#!/bin/sh
# Installs hype, the Hype Apps command line, on macOS or Linux.
#
#   curl -fsSL https://hypeapps.dev/install.sh | sh
#   wget -qO- https://hypeapps.dev/install.sh | sh     (Linux without curl)
#
# It downloads the binary for this machine, checks its SHA-256, puts it in
# ~/.hype/bin, and adds that to your PATH (in your shell's startup file).
# Set HYPE_NO_MODIFY_PATH=1 to leave your shell files alone, HYPE_INSTALL to
# install somewhere else, HYPE_DOWNLOAD to download from somewhere else,
# HYPE_ARCH=x64 for the Intel build on Apple silicon (it runs through Rosetta).
set -eu

BASE="${HYPE_DOWNLOAD:-https://hypeapps.dev/cli/latest}"
DEST="${HYPE_INSTALL:-${HOME}/.hype/bin}"

say() { printf '%s\n' "$*"; }
fail() { printf 'hype install: %s\n' "$*" >&2; exit 1; }

case "$(uname -s)" in
  Darwin) os=macos ;;
  Linux) os=linux ;;
  *) fail "this script is for macOS and Linux. On Windows: irm https://hypeapps.dev/install.ps1 | iex" ;;
esac
case "${HYPE_ARCH:-$(uname -m)}" in
  arm64|aarch64) arch=arm64 ;;
  x86_64|amd64|x64) arch=x64 ;;
  *) fail "no hype build for $(uname -m) yet." ;;
esac

fetch() { # url → file
  if command -v curl >/dev/null 2>&1; then curl -fsSL "$1" -o "$2"
  elif command -v wget >/dev/null 2>&1; then wget -qO "$2" "$1"
  else fail "needs curl or wget."; fi
}

name="hype-${os}-${arch}"
tmp="$(mktemp -d)"
trap 'rm -rf "${tmp}"' EXIT
say "Downloading ${name}…"
if ! fetch "${BASE}/${name}" "${tmp}/hype" 2>/dev/null; then
  # A Mac with Apple silicon runs the Intel build too, through Rosetta.
  if [ "${os}-${arch}" = "macos-arm64" ] && fetch "${BASE}/hype-macos-x64" "${tmp}/hype" 2>/dev/null; then
    name="hype-macos-x64"
    say "No Apple silicon build yet: using the Intel one, which runs through Rosetta."
  else
    fail "could not download ${BASE}/${name}"
  fi
fi

if fetch "${BASE}/${name}.sha256" "${tmp}/sum" 2>/dev/null; then
  want="$(cut -d' ' -f1 < "${tmp}/sum")"
  if command -v shasum >/dev/null 2>&1; then got="$(shasum -a 256 "${tmp}/hype" | cut -d' ' -f1)"
  else got="$(sha256sum "${tmp}/hype" | cut -d' ' -f1)"; fi
  [ "${want}" = "${got}" ] || fail "the download does not match its checksum; not installing it."
fi

chmod +x "${tmp}/hype"
# It must run here before it is installed. Alpine and other musl Linuxes
# need the glibc compatibility layer the build links against.
if ! "${tmp}/hype" --version >/dev/null 2>"${tmp}/err"; then
  if [ "${os}" = linux ] && { [ -f /etc/alpine-release ] || ldd --version 2>&1 | grep -qi musl; }; then
    fail "this Linux uses musl (Alpine). hype needs glibc compatibility: apk add gcompat, then run this again."
  fi
  fail "hype downloaded but does not run here: $(head -c 300 "${tmp}/err")"
fi
mkdir -p "${DEST}"
mv "${tmp}/hype" "${DEST}/hype"
say "Installed ${DEST}/hype"

case ":${PATH}:" in
  *":${DEST}:"*) on_path=yes ;;
  *) on_path=no ;;
esac
if [ "${on_path}" = no ] && [ "${HYPE_NO_MODIFY_PATH:-0}" != 1 ]; then
  line="export PATH=\"${DEST}:\${PATH}\"  # hype"
  case "$(basename "${SHELL:-sh}")" in
    zsh) rc="${HOME}/.zshrc" ;;
    bash) if [ "${os}" = macos ]; then rc="${HOME}/.bash_profile"; else rc="${HOME}/.bashrc"; fi ;;
    fish) rc="${HOME}/.config/fish/config.fish"; line="fish_add_path ${DEST}  # hype" ;;
    *) rc="${HOME}/.profile" ;;
  esac
  if ! grep -qs "# hype" "${rc}"; then
    mkdir -p "$(dirname "${rc}")"
    printf '\n%s\n' "${line}" >> "${rc}"
    say "Added ${DEST} to your PATH in ${rc}"
  fi
  say "Open a new terminal, or run this now:  export PATH=\"${DEST}:\${PATH}\""
fi

"${DEST}/hype" --version
say ""
say "Installed. Next, in the folder for your app (an empty one is fine):"
if [ "${on_path}" = yes ]; then
  say "  hype start"
elif [ "${HYPE_NO_MODIFY_PATH:-0}" = 1 ]; then
  say "  ${DEST}/hype start      (${DEST} is not on your PATH: you asked to leave it)"
else
  say "  ${DEST}/hype start      (plain \"hype start\" in a new terminal)"
fi
if [ "$(uname -s)" = Darwin ]; then
  say "It sets the app up, checks it and opens it on a simulator. No questions."
else
  say "It sets the app up, checks it and opens it on an Android emulator if you have one. No questions."
  say "No emulator? See it on your phone: hype pair, then hype push."
fi
