Files
OrbitTv/tools/install-tv.sh
T

44 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Orbit's install routine for one TV (9 November 2026), as run by hand since 6 November, in one command:
# tools/install-tv.sh <adb-device> <new-version> [room] e.g. 192.168.0.252:5555 0.122.2-provider downstairs
# 1. Refuses if Orbit is playing something (pass FORCE=1 to override) or the APK's checksum is wrong.
# 2. Keeps a rollback copy of the installed APK, and checks it against apks/ when that version is there.
# 3. Takes the before snapshot, sets the schedules aside on the TV, installs, compiles for speed and reopens Orbit.
# 4. Checks one copy of Orbit is running, takes the after snapshot and compares (tools/compare_snapshots.py).
# 5. Keeps the last three backups for this room. Exit 0 only when everything checked out.
# Restore, if ever needed: tools/restore-tv.sh <device> <backup>/before
# DRY_RUN=1 stops after step 2 and the before snapshot: it installs nothing and changes nothing on the TV.
set -uo pipefail
A=${ADB:-/opt/android-sdk/platform-tools/adb}; D=$1; NEW=$2; ROOM=${3:-downstairs}
OT=${ORBIT_HOME:-/home/pauljd/Documents/OrbitTV}; APKS=$OT/apks; TOOLS="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
PFX=$([ "$ROOM" = downstairs ] && echo "" || echo "$ROOM-")
R="$A -s $D shell am broadcast -f 0x20 -n tv.orbit.classic/.InstallSnapshotReceiver -a tv.orbit.classic.INSTALL_SNAPSHOT"
( cd "$APKS" && sha256sum -c "orbit-tv-$NEW.apk.sha256" >/dev/null ) || { echo "Checksum of orbit-tv-$NEW.apk failed or missing; nothing installed."; exit 1; }
APPID=$($A -s $D shell dumpsys package tv.orbit.classic | grep -m1 -oE '(userId|appId)=[0-9]+' | cut -d= -f2 | tr -d '\r')
[ -n "$APPID" ] || { echo "Couldn't find Orbit on $ROOM; nothing installed."; exit 1; }
PLAYING=$($A -s $D shell dumpsys audio | grep -c "u/pid:$APPID/.*state:started")
[ "${PLAYING:-0}" != "0" ] && [ "${FORCE:-0}" != "1" ] && { echo "Orbit is playing on $ROOM; not interrupting. FORCE=1 to install anyway."; exit 2; }
PREV=$($A -s $D shell dumpsys package tv.orbit.classic | grep -m1 versionName | sed 's/.*versionName=//' | tr -d '\r ')
B=$OT/backups/${PFX}install-$NEW-$(date +%Y%m%d-%H%M); mkdir -p "$B"; echo "room $ROOM, installed $PREV, backup $B"
P=$($A -s $D shell pm path tv.orbit.classic | sed -n 's/package://p' | head -1 | tr -d '\r'); $A -s $D pull "$P" "$B/rollback-$PREV.apk" >/dev/null || { echo "Couldn't keep a rollback copy; nothing installed."; exit 1; }
if [ -f "$APKS/orbit-tv-$PREV.apk.sha256" ]; then
[ "$(sha256sum "$B/rollback-$PREV.apk" | cut -d' ' -f1)" = "$(cut -d' ' -f1 "$APKS/orbit-tv-$PREV.apk.sha256")" ] && echo "rollback copy verified" || { echo "Rollback copy doesn't match apks/; nothing installed."; exit 1; }
fi
snap() { for i in 1 2 3; do r=$($R --es do snapshot | grep -o 'data="[^"]*"'); echo "$1 snapshot $r"; echo "$r" | grep -q failed || break; sleep 3; done
$A -s $D pull /sdcard/Android/data/tv.orbit.classic/files/install-snapshot "$B/$1" >/dev/null && $R --es do clear >/dev/null; }
snap before
if [ "${DRY_RUN:-0}" = "1" ]; then python3 "$TOOLS/compare_snapshots.py" "$B/before" "$B/before" | tail -4; echo "Dry run: nothing installed, backup kept at $B."; exit 0; fi
$R --es do rollback --es version "$(echo "$PREV" | grep -oE '^[0-9]+\.[0-9]+\.[0-9]+')" | grep -o 'data="[^"]*"'
$A -s $D install -r "$APKS/orbit-tv-$NEW.apk" | tail -1 || exit 1
$A -s $D shell cmd package compile -m speed -f tv.orbit.classic >/dev/null
$A -s $D shell am start -n tv.orbit.classic/.MainActivity >/dev/null
sleep 25
NOW=$($A -s $D shell dumpsys package tv.orbit.classic | grep -m1 versionName | sed 's/.*versionName=//' | tr -d '\r ')
COPIES=$($A -s $D shell "dumpsys activity activities | grep -c 'Hist.*tv.orbit.classic/.MainActivity'" | tr -d '\r')
echo "now $NOW, copies of Orbit running: $COPIES"
snap after
python3 "$TOOLS/compare_snapshots.py" "$B/before" "$B/after"; RESULT=$?
ls -d "$OT/backups/${PFX}install-"* 2>/dev/null | sort | head -n -3 | xargs -r rm -rf
[ "$NOW" = "$NEW" ] && [ "$COPIES" = "1" ] && [ $RESULT -eq 0 ] && { echo "Install checked out."; exit 0; }
echo "Install needs a look (version $NOW, copies $COPIES, comparison exit $RESULT)."; exit 3