#!/usr/bin/env bash set -Eeuo pipefail # Dedicated Ridley party Pi only. Do not use this on a lesson-board Pi. if [[ $EUID != 0 ]]; then echo 'Run with sudo bash.' >&2; exit 1; fi if [[ $(hostname) != scf-tv-ridley-party ]]; then echo 'Set the Pi hostname to scf-tv-ridley-party first. Nothing was changed.' >&2; exit 1 fi if [[ -e /etc/scf-tv/device.env ]]; then echo 'An existing lesson-TV installation was found. Refusing to replace it.' >&2; exit 1 fi if [[ ${1:-} != --worker ]]; then setup_code=${2:-} [[ ${1:-} == --setup-code && ${#setup_code} -ge 32 ]] || { echo 'Supply --setup-code followed by your private setup code.' >&2; exit 1; } command -v systemctl >/dev/null command -v python3 >/dev/null command -v apt-get >/dev/null if systemctl is-active --quiet scf-party-install.service; then echo 'Installation is already running. Use: sudo journalctl -u scf-party-install -f'; exit 0 fi install -d -m 0700 /var/lib/scf-party-install install -m 0700 "$0" /var/lib/scf-party-install/install.sh umask 077 printf '%s' "$2" > /var/lib/scf-party-install/setup-code # A persistent service survives SSH disconnection and resumes after power loss. printf '%s\n' '[Unit]' 'Description=SCF party TV installation' 'Wants=network-online.target' 'After=network-online.target' 'StartLimitIntervalSec=0' \ '[Service]' 'Type=oneshot' 'ExecStart=/bin/bash /var/lib/scf-party-install/install.sh --worker' 'TimeoutStartSec=0' 'Restart=on-failure' 'RestartSec=60' \ '[Install]' 'WantedBy=multi-user.target' > /etc/systemd/system/scf-party-install.service systemctl daemon-reload systemctl enable scf-party-install.service systemctl start --no-block scf-party-install.service echo 'Installation started independently of SSH. You may disconnect; keep the Pi powered and online.' echo 'Progress: sudo journalctl -u scf-party-install -f' exit 0 fi export DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=300 update apt-get -o DPkg::Lock::Timeout=300 install -y --no-install-recommends python3 python3-pil python3-pil.imagetk python3-tk xserver-xorg xinit x11-xserver-utils x11-utils fonts-dejavu-core curl ca-certificates util-linux id scf-party >/dev/null 2>&1 || useradd --system --create-home --home-dir /var/lib/scf-party-user --shell /usr/sbin/nologin scf-party install -d -m 0755 /opt/scf-party /opt/scf-party/releases /var/lib/scf-party /var/lib/scf-party/media install -d -m 0755 -o scf-party -g scf-party /var/lib/scf-party/runtime install -d -m 0700 /etc/scf-party curl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' --max-time 90 --retry 3 \ -A SCF-TV-Remote/1 https://tv.shadowcreekfarm.com/party/bootstrap.json -o /var/lib/scf-party-install/bootstrap.json python3 - <<'PY' import hashlib, json, os, py_compile from pathlib import Path bundle = json.loads(Path('/var/lib/scf-party-install/bootstrap.json').read_text()) assert set(bundle) == {'controller.py', 'guard.py'} for name, record in bundle.items(): content = record['content'].encode() assert hashlib.sha256(content).hexdigest() == record['sha256'] path = Path('/opt/scf-party') / name temp = path.with_suffix('.part') temp.write_bytes(content) py_compile.compile(str(temp), doraise=True) temp.chmod(0o755) os.replace(temp, path) PY python3 /opt/scf-party/controller.py --initial --enroll-file /var/lib/scf-party-install/setup-code printf '%s\n' '#!/bin/sh' 'set -eu' 'xset s off || true' 'xset -dpms || true' 'xset s noblank || true' \ 'exec runuser -u scf-party -- /usr/bin/python3 /opt/scf-party/current/player.py' > /opt/scf-party/session.sh chmod 0755 /opt/scf-party/session.sh printf '%s\n' '[Unit]' 'Description=SCF Ridley party slideshow' 'After=systemd-user-sessions.service' 'StartLimitIntervalSec=0' \ '[Service]' 'Type=simple' 'ExecStart=/usr/bin/xinit /opt/scf-party/session.sh -- /usr/lib/xorg/Xorg :1 vt8 -nolisten tcp -ac' \ 'Restart=always' 'RestartSec=5' 'KillMode=control-group' 'TimeoutStopSec=15' \ '[Install]' 'WantedBy=multi-user.target' > /etc/systemd/system/scf-party-player.service printf '%s\n' '[Unit]' 'Description=SCF party five-minute update' 'After=network-online.target' \ '[Service]' 'Type=oneshot' 'ExecStart=/usr/bin/python3 /opt/scf-party/current/controller.py' 'TimeoutStartSec=240' > /etc/systemd/system/scf-party-update.service printf '%s\n' '[Unit]' 'Description=SCF party update schedule' '[Timer]' 'OnBootSec=60' 'OnUnitActiveSec=5min' 'AccuracySec=1s' \ '[Install]' 'WantedBy=timers.target' > /etc/systemd/system/scf-party-update.timer printf '%s\n' '[Unit]' 'Description=SCF party actual-rotation watchdog' '[Service]' 'Type=oneshot' \ 'ExecStart=/usr/bin/python3 /opt/scf-party/guard.py' 'TimeoutStartSec=45' > /etc/systemd/system/scf-party-watchdog.service printf '%s\n' '[Unit]' 'Description=SCF party watchdog schedule' '[Timer]' 'OnBootSec=120' 'OnUnitActiveSec=30s' 'AccuracySec=1s' \ '[Install]' 'WantedBy=timers.target' > /etc/systemd/system/scf-party-watchdog.timer for script in /opt/scf-party/guard.py /opt/scf-party/current/controller.py /opt/scf-party/current/player.py /opt/scf-party/session.sh; do test -x "$script" done # Dedicated kiosk owns the display. Download/validate content before stopping the desktop. systemctl disable --now display-manager.service 2>/dev/null || true systemctl set-default multi-user.target systemctl daemon-reload systemctl enable --now scf-party-player.service scf-party-update.timer scf-party-watchdog.timer systemctl start scf-party-watchdog.service systemctl is-active --quiet scf-party-player.service systemctl is-active --quiet scf-party-update.timer systemctl is-active --quiet scf-party-watchdog.timer test "$(systemctl show -p ExecMainStatus --value scf-party-watchdog.service)" = 0 # Verify real visible-frame progress from this boot, not just a running process. python3 - <<'PY' import json, time from pathlib import Path deadline = time.monotonic() + 120 while time.monotonic() < deadline: try: p = json.loads(Path('/var/lib/scf-party/runtime/progress.json').read_text()) if p.get('boot_id') == Path('/proc/sys/kernel/random/boot_id').read_text().strip() and p['sequence'] >= 3 and 0 <= time.monotonic() - p['monotonic'] < 30 and len({x['id'] for x in p['history']}) == 2: print('Verified both images and three actual display transitions.', flush=True) break except (OSError, ValueError, KeyError): pass time.sleep(2) else: raise SystemExit('Display verification failed; installer will retry. Inspect journalctl -u scf-party-player.') PY systemctl disable scf-party-install.service # Retain only the permanent, root-readable device credential after successful enrollment. rm -f /var/lib/scf-party-install/setup-code echo 'READY: both images rotating; five-minute updates and watchdog verified. No nightly shutoff is configured.'