#!/bin/bash
# Set up the sign detector on a Raspberry Pi 5 with the AI Camera (Raspberry Pi OS with the desktop), as a release
# install that keeps itself up to date (see release/updater.py). make_release.py publishes this into each channel's
# folder with the server and channel filled in, so it's run as:
#
#     curl -fsSL <server>/stable/install.sh | bash
#
# It installs what the detector needs with apt (asking for your password), puts the latest release in
# ~/sign-detector (or $SIGN_DETECTOR_DIR), and adds "Sign Detector" to the desktop, the app menu and the login
# autostart. Run it again to repair an install; what the detector saved (data/) is kept.
set -euo pipefail
server="${SIGN_DETECTOR_SERVER:-https://gitea.myclouding.com/api/packages/elias/generic/sign-detector}"
channel="${SIGN_DETECTOR_CHANNEL:-stable}"
dir="${SIGN_DETECTOR_DIR:-$HOME/sign-detector}"

if [ "$(id -u)" -eq 0 ]; then
    echo "Run this as your own user, not with sudo: it asks for your password when it needs it." >&2
    exit 1
fi
if ! grep -qs "Raspberry Pi" /proc/device-tree/model; then
    echo "This isn't a Raspberry Pi: the sign detector needs a Pi 5 with the AI Camera." >&2
    exit 1
fi

echo "Installing what the sign detector needs (apt)..."
sudo apt-get update -q
sudo apt-get install -y -q python3-picamera2 python3-opencv python3-numpy python3-pil python3-pyqt5 \
    tesseract-ocr imx500-all rsync openssh-client curl
# The camera's model packager (start.sh packages each release's model for the camera with it)
command -v imx500-package >/dev/null || sudo apt-get install -y -q imx500-tools || true
if ! command -v imx500-package >/dev/null; then
    echo "Warning: imx500-package wasn't found after installing imx500-all and imx500-tools, and the detector" \
         "needs it to load its model: see the Raspberry Pi AI Camera documentation." >&2
fi
# Tesseract's Python binding: from apt where Raspberry Pi OS has it, else from pip
if ! python3 -c "import pytesseract" 2>/dev/null; then
    sudo apt-get install -y -q python3-pytesseract 2>/dev/null ||
        pip3 install --user --break-system-packages -q pytesseract
fi

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
echo "Getting the latest $channel release from $server..."
curl -fsSL "$server/$channel/updater.py" -o "$tmp/updater.py"
curl -fsSL "$server/$channel/allowed_signers" -o "$tmp/allowed_signers"
python3 "$tmp/updater.py" install --server "$server" --channel "$channel" --signers "$tmp/allowed_signers" \
    --dir "$dir"

entry="[Desktop Entry]
Type=Application
Name=Sign Detector
Comment=Reads street signs with the AI Camera
Exec=$dir/run.sh
Icon=camera-video
Terminal=false
Categories=Utility;"
mkdir -p "$HOME/.config/autostart" "$HOME/.local/share/applications"
printf '%s\n' "$entry" >"$HOME/.config/autostart/sign-detector.desktop"
printf '%s\n' "$entry" >"$HOME/.local/share/applications/sign-detector.desktop"
if [ -d "$HOME/Desktop" ]; then
    printf '%s\n' "$entry" >"$HOME/Desktop/sign-detector.desktop"
    chmod +x "$HOME/Desktop/sign-detector.desktop"
fi

echo
echo "Done. The sign detector starts when you log in, or from \"Sign Detector\" on the desktop or in the menu."
echo "If the AI Camera's firmware was just installed, restart the Pi first: sudo reboot"
echo "It updates itself: a new release is downloaded when the Pi is online, and used from the next start."
