#!/bin/bash

# Installs TinyPilot and all dependencies.

# Exit on first error.
set -e

# Exit on unset variable.
set -u

# Echo commands before executing them, by default to stderr.
set -x

# Abort installation if the read-only filesystem is enabled.
if grep -q "boot=overlay" /proc/cmdline ; then
  echo 'The read-only filesystem is enabled.' >&2
  echo 'Disable the read-only filesystem before proceeding.' >&2
  echo 'See https://tinypilotkvm.com/faq/read-only-filesystem for details.' >&2
  exit 1
fi

# Check if the user is forcing an install.
if [[ -n "${FORCE_INSTALL+x}" ]]; then
  echo 'Forcing installation on unsupported setup.'
else
  # Restrict installation to only specific configurations.

  # Install only on supported hardware:
  # - Raspberry Pi 4 Model B (e.g., Voyager 2)
  # - Compute Module 4 (e.g., Voyager 3)
  if ! grep --quiet \
    --regexp '^Model\s*: Raspberry Pi 4 Model B' \
    --regexp '^Model\s*: Raspberry Pi Compute Module 4' \
    /proc/cpuinfo; then
    echo 'You are trying to install on unsupported hardware.' >&2
    echo 'Visit https://github.com/tiny-pilot/tinypilot/ for more details.' >&2
    exit 1
  fi

  # Prevent installation on 32-bit / non-64-bit operating systems.
  # Note: the distro ID for 64-bit Raspberry Pi OS is called "Debian"; for the
  # 32-bit version, it would say "Raspbian".
  if [[ "$(uname --machine)" != "aarch64" || "$(lsb_release --id --short)" != 'Debian' ]]; then
    echo "TinyPilot currently only supports the 64-bit version of Raspberry Pi OS 13 \"Trixie\"." >&2
    echo "To install TinyPilot, you'll need Raspberry Pi OS 13 \"Trixie\" (64-bit)." >&2
    exit 1
  fi

  # Prevent installation on OS versions lower than Raspberry Pi OS 13 "Trixie".
  if (( "$(lsb_release --release --short)" < 13 )); then
    echo "TinyPilot no longer supports Raspberry Pi OS 11 \"Bullseye\" or lower." >&2
    echo "To install TinyPilot, you'll need to upgrade your operating system to Raspberry Pi OS 13 \"Trixie\" (64-bit)." >&2
    exit 1
  fi

  # Prevent installation on Raspberry Pi OS 13+.
  if (( "$(lsb_release --release --short)" > 13 )); then
    echo "TinyPilot is not compatible with Raspberry Pi OS greater than version 13 \"Trixie\"." >&2
    echo "To install TinyPilot, you'll need to downgrade your operating system to Raspberry Pi OS 13 \"Trixie\" (64-bit)." >&2
    exit 1
  fi
fi

# Get the filename of the Janus Debian package.
JANUS_DEBIAN_PACKAGE="$(ls janus*.deb)"
readonly JANUS_DEBIAN_PACKAGE

# Get the filename of the uStreamer Debian package.
USTREAMER_DEBIAN_PACKAGE="$(ls ustreamer*.deb)"
readonly USTREAMER_DEBIAN_PACKAGE

# Get the filename of the TinyPilot Debian package.
TINYPILOT_DEBIAN_PACKAGE="$(ls tinypilot*.deb)"
readonly TINYPILOT_DEBIAN_PACKAGE

# Update apt package index files, allowing the apt repos to change suite name.
# See https://github.com/tiny-pilot/tinypilot/issues/764
apt-get update --allow-releaseinfo-change-suite

apt-get install -y \
  "./${JANUS_DEBIAN_PACKAGE}" \
  "./${USTREAMER_DEBIAN_PACKAGE}" \
  "./${TINYPILOT_DEBIAN_PACKAGE}"

# Install yq.
mv yq /usr/bin/yq
chmod 0755 /usr/bin/yq
