#!/usr/bin/env bash
# Sterling Magna Access on-site bridge: installer for Raspberry Pi OS / Debian / Ubuntu.
#
#   curl -fsSL https://sterlingmagnaaccess.com/bridge/install.sh | sudo bash
#
# Installs Node.js if needed, downloads the bridge, runs the setup wizard (asks for the setup code from the
# console and each terminal's address and login), and starts it as a service that runs at every boot.
# Run the same command again to update; add "-s -- --reconfigure" to change gates or passwords.
set -euo pipefail

SITE="${SMA_SITE:-https://sterlingmagnaaccess.com}"
DIR=/opt/sterling-magna-bridge
SERVICE=sterling-magna-bridge
RECONFIGURE=0
[ "${1:-}" = "--reconfigure" ] && RECONFIGURE=1

say() { printf '\n\033[1;31m▸\033[0m %s\n' "$*"; }
die() { printf '\n\033[1;31m✗ %s\033[0m\n' "$*" >&2; exit 1; }

[ "$(id -u)" -eq 0 ] || die "Run it with sudo:  curl -fsSL $SITE/bridge/install.sh | sudo bash"
command -v systemctl >/dev/null || die "This installer needs a Linux with systemd (Raspberry Pi OS, Debian, Ubuntu). On Windows, follow the Windows steps on $SITE/bridge."

say "Sterling Magna Access on-site bridge"

node_ok() { command -v node >/dev/null && [ "$(node -p 'process.versions.node.split(".")[0]')" -ge 18 ]; }
if ! node_ok; then
  say "Installing Node.js"
  apt-get update -qq
  apt-get install -y -qq nodejs >/dev/null
  node_ok || die "Node.js 18 or newer is needed. Install it (https://nodejs.org) and run this again."
fi
echo "  Node.js $(node -v)"

id smbridge >/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin smbridge
mkdir -p "$DIR"

say "Downloading the bridge"
TMP="$(mktemp)"
curl -fsSL "$SITE/bridge/sterling-magna-bridge.mjs" -o "$TMP" || die "Could not download from $SITE. Is this computer online?"
head -c 64 "$TMP" | grep -q "node" || die "The download doesn't look right. Try again later."
install -m 0644 "$TMP" "$DIR/sterling-magna-bridge.mjs"
rm -f "$TMP"
echo "  Version $(node "$DIR/sterling-magna-bridge.mjs" version)"

if [ ! -s "$DIR/config.json" ] || [ "$RECONFIGURE" -eq 1 ]; then
  say "Setting up your gates"
  # The wizard talks to you through the terminal even though this script arrived through a pipe.
  node "$DIR/sterling-magna-bridge.mjs" setup "$DIR/config.json" </dev/tty || die "Setup did not finish. Run the command again when ready."
fi
chown -R smbridge "$DIR"
chmod 600 "$DIR/config.json"

say "Starting the bridge service"
cat >/etc/systemd/system/$SERVICE.service <<UNIT
[Unit]
Description=Sterling Magna Access on-site bridge
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=smbridge
WorkingDirectory=$DIR
ExecStart=$(command -v node) $DIR/sterling-magna-bridge.mjs run $DIR/config.json
Restart=always
RestartSec=5
ProtectSystem=strict
ReadWritePaths=$DIR
ProtectHome=true
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable "$SERVICE" >/dev/null 2>&1
systemctl restart "$SERVICE"
sleep 6
systemctl is-active --quiet "$SERVICE" || { journalctl -u "$SERVICE" -n 20 --no-pager; die "The bridge did not start. See the messages above."; }

journalctl -u "$SERVICE" -n 8 --no-pager -o cat || true
say "Done. The bridge runs now and after every restart."
cat <<TXT
  • Check the console (Gates): each gate should show the bridge as Online within a minute.
  • See what it's doing:   journalctl -u $SERVICE -f
  • Change gates/passwords: curl -fsSL $SITE/bridge/install.sh | sudo bash -s -- --reconfigure
  • Update to the latest:   curl -fsSL $SITE/bridge/install.sh | sudo bash
TXT
