#!/bin/bash
# Installs, or updates, Pi OS+ on this Raspberry Pi: https://piosplus.com
#
#   curl -fsSL https://piosplus.com/get.sh | sudo bash
#
# It finds the latest version, downloads the build for this Pi (64-bit or 32-bit Raspberry Pi OS), checks its
# SHA-256 checksum, unpacks it and runs its install.sh, the same steps as the step-by-step guide on the website.
# Run it from your own account with sudo: Pi OS+ is set up for the user who runs it, who then signs in to it.
#
# To add this Pi to another Pi's Pi OS+ (its Pi Collection) instead, as a Pi that console looks after:
#
#   curl -fsSL https://piosplus.com/get.sh | sudo bash -s -- --join https://carrot.local:3141 0123456789ABCDEF
#
# Pi Collection's "Use a joining code" shows that line for you, with its console's address and fingerprint. This
# installs only the agent and the watcher (no pages of its own), then asks for the joining code shown beside it.
set -euo pipefail

site="https://piosplus.com/download"
say() { printf '%s\n' "$*"; }
fail() { printf 'Pi OS+: %s\n' "$*" >&2; exit 1; }

# --join <console address> <its fingerprint>: install as a Pi another console looks after, and join it.
join_url=""; join_pin=""
while [ $# -gt 0 ]; do
  case "$1" in
    --join) join_url="${2:-}"; join_pin="${3:-}"; shift 3 || fail "--join needs the console's address and fingerprint." ;;
    *) fail "unknown option $1 (the only one is --join <console address> <fingerprint>)" ;;
  esac
done
if [ -n "$join_url" ]; then
  [[ "$join_url" =~ ^https://[A-Za-z0-9.:\[\]-]+:3141/?$ ]] || fail "that console address does not look like one: it should be like https://carrot.local:3141"
  [[ "$join_pin" =~ ^[0-9A-Fa-f:]{16,95}$ ]] || fail "that fingerprint does not look like one: copy the whole line from Pi Collection."
  [ -r /dev/tty ] || fail "--join needs a terminal to ask for the joining code in."
fi

[ "$(id -u)" -eq 0 ] || fail "run it with sudo:  curl -fsSL https://piosplus.com/get.sh | sudo bash"
[ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ] || fail "run it from your own account with sudo, not as root, so Pi OS+ is set up for you."
command -v dpkg >/dev/null || fail "this does not look like Raspberry Pi OS."
case "$(uname -m)" in armv6*) fail "this Pi has an ARMv6 processor (the first Pi, or a Zero or Zero W), which Pi OS+ cannot run on. It needs a Pi 2 or later." ;; esac
case "$(dpkg --print-architecture)" in
  arm64) arch=arm64 ;;
  armhf) arch=armhf ;;
  *) fail "Pi OS+ is for Raspberry Pi OS on a Raspberry Pi; this machine is $(dpkg --print-architecture)." ;;
esac

version="$(curl -fsSL "$site/latest.txt" | tr -d '[:space:]')"
[[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || fail "could not find the latest version at $site/latest.txt."
name="piosplus-$version-$arch.tar.gz"

# On the card rather than in /tmp, which is in memory on recent Raspberry Pi OS and small on a 1 GB Pi.
work="$(mktemp -d /var/tmp/piosplus.XXXXXX)"
trap 'rm -rf "$work"' EXIT

say "Downloading Pi OS+ $version for $arch Raspberry Pi OS..."
curl -fL --progress-bar -o "$work/$name" "$site/$version/$name"
curl -fsSL -o "$work/$name.sha256" "$site/$version/$name.sha256"
say "Checking the download..."
(cd "$work" && sha256sum -c --quiet "$name.sha256") || fail "the download is damaged (its checksum does not match). Try again."

tar -xzf "$work/$name" -C "$work"
if [ -z "$join_url" ]; then
  bash "$work/piosplus/install.sh"
  exit 0
fi

# A Pi another console looks after: the agent and the watcher only, then joined with the code shown on that console.
bash "$work/piosplus/install.sh" --satellite
say ""
say "Now join it to $join_url."
code=""
while [ -z "$code" ]; do
  printf 'The joining code shown in Pi Collection (like ABCD-EFGH): ' > /dev/tty
  read -r code < /dev/tty || fail "no joining code was typed."
  code="$(printf '%s' "$code" | tr -d '[:space:]')"
done
# The code goes in on standard input, so it never shows in this Pi's list of running programs.
printf '%s\n' "$code" | /opt/piosplus/PiOSPlus.Agent join "$join_url" - "$join_pin"
