#!/usr/bin/env bash
# BotMaker Studio — one-command install on Fedora/RHEL (dnf) and Debian/Ubuntu (apt).
#
#   curl -fsSL https://botmakerdev.github.io/botmaker-studio/install.sh | sudo bash
#
# All this does is what the landing page tells you to do by hand: register the signed package repository
# and install from it. It is deliberately readable end to end, because the recommended way to run it pipes
# it into a root shell — anything clever here would be a thing you cannot audit before it executes.
#
# It is NOT generated by build-repo.sh. That script emits index.html and the .repo file from heredocs,
# which is fine for a few lines of config; a real script nested in a heredoc inside another script is
# unreviewable and its $-escaping is a trap. This file is committed, reviewed and shipped byte-for-byte:
# build-repo.sh copies it to the site root and the release job attaches it to the GitHub Release.
#
# Re-running is safe. Both package managers treat an already-registered repo and an already-installed
# package as a no-op or an upgrade, so this doubles as the "update me" command.
set -euo pipefail

# The site this script installs from. Overridable for testing against a staging deploy; the default is
# asserted by build-repo.sh to match the URL it is actually publishing to, so the two cannot drift.
BASE_URL="${BOTMAKER_REPO_URL:-https://botmakerdev.github.io/botmaker-studio}"

RELEASES_URL="https://github.com/BotMakerDev/botmaker-studio/releases"

die() { echo "error: $*" >&2; exit 1; }
info() { echo "==> $*"; }

[ "$(id -u)" = "0" ] || die "run as root — pipe into 'sudo bash', or 'sudo $0'"

command -v curl >/dev/null 2>&1 || die "curl is required (install it, or download the package from $RELEASES_URL)"

# Both packages are built x86_64-only (one per-OS runtime image, see the package matrix in ci.yml). Fail
# here with something actionable rather than letting apt report an unrelated "no installation candidate".
arch="$(uname -m)"
case "$arch" in
  x86_64|amd64) ;;
  *) die "no package for $arch — BotMaker Studio ships x86_64 only. See $RELEASES_URL" ;;
esac

if command -v dnf >/dev/null 2>&1 || command -v yum >/dev/null 2>&1; then
  pm="$(command -v dnf || command -v yum)"
  info "Registering the BotMaker Studio repository for ${pm##*/}"
  # Written with curl rather than `dnf config-manager addrepo` on purpose: the dnf5 spelling
  # (--from-repofile=) and the dnf4 spelling (--add-repo) differ, and dropping the file in works on both.
  curl -fsSL -o /etc/yum.repos.d/botmaker-studio.repo "${BASE_URL}/botmaker-studio.repo"

  # The repo sets repo_gpgcheck=1, so the key is needed before the first metadata read. dnf would offer to
  # import it interactively — but stdin is the pipe this script arrived on, not a terminal, so there is
  # nobody there to answer the prompt. Importing it up front is what makes the piped form work at all.
  info "Importing the signing key"
  rpm --import "${BASE_URL}/botmaker.asc"

  info "Installing"
  "$pm" install -y botmaker-studio

elif command -v apt-get >/dev/null 2>&1; then
  info "Registering the BotMaker Studio repository for apt"
  install -d -m 755 /etc/apt/keyrings
  # signed-by accepts an ASCII-armored key as long as the file is named .asc, so this is the same single
  # export the dnf side imports — there is no second, dearmored copy to keep in step.
  curl -fsSL -o /etc/apt/keyrings/botmaker.asc "${BASE_URL}/botmaker.asc"
  # arch=amd64 is explicit so an arm64 host with amd64 foreign-arch enabled gets a clean "not found"
  # from the guard above rather than a confusing update error about a missing architecture.
  echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/botmaker.asc] ${BASE_URL}/deb stable main" \
    > /etc/apt/sources.list.d/botmaker-studio.list

  info "Installing"
  apt-get update
  DEBIAN_FRONTEND=noninteractive apt-get install -y botmaker-studio

else
  die "no supported package manager found (dnf, yum or apt-get).
The AppImage needs no package manager and no root: $RELEASES_URL"
fi

info "Done. Launch BotMaker Studio from your application menu."
info "Updates arrive with the rest of the system — re-run this script, or just upgrade normally."
