#!/usr/bin/env bash
# Semwright 1.0.0, source 8fa191250ae68274182570c65f067f7a60f85625.
# Downloads a pinned public bundle; delegates installation to its own helper.
set -euo pipefail
umask 077
fail() { printf 'Semwright: %s\n' "$*" >&2; exit 1; }
case "${1:-}" in
  --help) printf '%s\n' 'Usage: bash semwright-install.sh [--download-only]' 'Downloads and checks the matching bundle, installs it for your user and runs setup.'; exit 0 ;;
  --download-only|'') ;;
  *) fail 'Unknown option. Use --help for instructions.' ;;
esac
[[ $# -le 1 ]] || fail 'Use --help for instructions.'
[[ $(id -u) != 0 ]] || fail 'Open a terminal as your regular user and run this command without sudo.'
for tool in curl tar mktemp; do
  command -v "$tool" >/dev/null 2>&1 || fail "Install $tool, then run this command again."
done
if command -v sha256sum >/dev/null 2>&1; then
  digest() { sha256sum "$1" | cut -d ' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
  digest() { shasum -a 256 "$1" | cut -d ' ' -f1; }
else
  fail 'Install sha256sum or shasum, then run this command again.'
fi
os=$(uname -s)
arch=$(uname -m)
case "$os/$arch" in
  Darwin/aarch64) arch=arm64 ;;
  Linux/arm64) arch=aarch64 ;;
esac
# Detect Apple Silicon even when Terminal runs under Rosetta.
if [[ "$os" == Darwin && "$arch" == x86_64 ]] && [[ $(sysctl -in hw.optional.arm64 2>/dev/null || true) == 1 ]]; then
  arch=arm64
fi
case "$os/$arch" in
  Linux/aarch64) url='https://github.com/seradotcom/semwright/releases/download/v1.0.0/semwright-1.0.0-aarch64.tar.gz'; expected='1c0cfa81ee53fafa029208b6b60442cc8923838428a12ccdd9f745089fff73d8' ;;
  Darwin/arm64) url='https://github.com/seradotcom/semwright/releases/download/v1.0.0/semwright-1.0.0-macos-arm64.tar.gz'; expected='20b6831670151a3bdf55a22324810a15213bb4288af5d9e7221961fd7318cb07' ;;
  Darwin/x86_64) url='https://github.com/seradotcom/semwright/releases/download/v1.0.0/semwright-1.0.0-macos-x86_64.tar.gz'; expected='57511ac1dee3140438ffa580db91bbc0de545735ccd07f0b65c8493ef7fbb697' ;;
  Linux/x86_64) url='https://github.com/seradotcom/semwright/releases/download/v1.0.0/semwright-1.0.0-x86_64.tar.gz'; expected='2b927289b949b2b0b927d10869f5a845b7581a538eee829ef3495e2ba268c385' ;;
  *) fail "No bundle is available for $os/$arch. See https://semwright.com/docs/installation/" ;;
esac
[[ -n "${HOME:-}" ]] || fail 'Your user home directory could not be found.'
case "$os" in
  Linux) prefix="$HOME/.local/share/semwright" ;;
  Darwin) prefix="$HOME/Library/Application Support/Semwright" ;;
esac
if [[ "${1:-}" != --download-only && ( -e "$prefix" || -L "$prefix" ) ]]; then
  fail "Semwright is already installed at $prefix. To connect it, run \"$prefix/bin/semwright\" setup. For an upgrade, follow https://semwright.com/docs/installation/#upgrade-or-remove-semwright"
fi
work=$(mktemp -d "${TMPDIR:-/tmp}/semwright-install.XXXXXXXX")
keep=false
cleanup() { if [[ "$keep" == false ]]; then rm -rf -- "$work"; fi; }
trap cleanup EXIT
printf '1/3 Downloading Semwright 1.0.0 for %s %s…\n' "$os" "$arch"
curl --fail --location --silent --show-error --proto '=https' --proto-redir '=https' --connect-timeout 20 --max-time 600 --output "$work/bundle.tar.gz" "$url" || fail 'Download failed. Check your internet connection and run this command again.'
[[ $(digest "$work/bundle.tar.gz") == "$expected" ]] || fail 'The download did not pass its integrity check. Nothing was installed. Try downloading again.'
printf '2/3 Download verified.\n'
mkdir "$work/bundle"
tar -xzf "$work/bundle.tar.gz" -C "$work/bundle"
# Release archives may contain one top-level directory.
helper=''
if [[ -f "$work/bundle/install.sh" ]]; then
  helper="$work/bundle/install.sh"
else
  for directory in "$work/bundle"/*; do
    if [[ -d "$directory" && -f "$directory/install.sh" ]]; then
      [[ -z "$helper" ]] || fail 'The bundle contains more than one installer.'
      helper="$directory/install.sh"
    fi
  done
fi
[[ -n "$helper" ]] || fail 'The verified bundle has no install helper. Please report this at https://github.com/seradotcom/semwright/issues'
if [[ "${1:-}" == --download-only ]]; then
  keep=true
  printf 'Verified bundle extracted to %s\n' "$(dirname "$helper")"
  exit 0
fi
bash "$helper" > "$work/install-output.txt" 2>&1 || { cat "$work/install-output.txt" >&2; fail 'Installation stopped. Follow the message above, then try again.'; }
"$prefix/bin/semwright" setup > "$work/setup-output.txt" 2>&1 || { cat "$work/setup-output.txt" >&2; fail 'Semwright was installed, but setup needs attention. Run the setup command again.'; }
printf '3/3 Semwright is installed and setup is complete.\n\n'
printf 'Next: start Semwright using the Broker command below and leave that terminal open.\n'
printf 'Then follow https://semwright.com/docs/installation/#2-connect-your-agent\n\n'
# The runtime supplies the actual configuration paths and next commands.
cat "$work/setup-output.txt"
