#!/bin/bash # Waits for the ingest chain (download -> encode -> card) to finish, then # installs and starts the launchd service. Safe to re-run; it replaces any # existing service instance. set -u cd "$(dirname "$0")/.." echo "$(date '+%F %T') waiting for ingest to complete..." until grep -q "card written" logs/ingest.log 2>/dev/null; do if grep -q "failures:" logs/ingest.log 2>/dev/null; then echo "$(date '+%F %T') ingest reported failures; not going live." && exit 1 fi sleep 120 done # Give the second pass (re-sourced films) up to 40 minutes to finish, then # launch regardless; stragglers join the playlist on the next shuffle cycle. waited=0 until grep -q "SECOND PASS DONE" logs/ingest2.log 2>/dev/null || (( waited >= 2400 )); do sleep 120; waited=$(( waited + 120 )) done echo "$(date '+%F %T') second pass: $(grep -c 'SECOND PASS DONE' logs/ingest2.log 2>/dev/null || echo 0) done flag, waited ${waited}s" n=$(ls encoded/*.mp4 2>/dev/null | wc -l | tr -d ' ') echo "$(date '+%F %T') ingest complete: $n encoded films" if (( n < 60 )); then echo "only $n encodes present; expected ~68. Not going live." && exit 1 fi mkdir -p ~/Library/LaunchAgents cp launchd/town.laugh.stream.plist ~/Library/LaunchAgents/ launchctl bootout "gui/$(id -u)/town.laugh.stream" 2>/dev/null || true launchctl bootstrap "gui/$(id -u)" ~/Library/LaunchAgents/town.laugh.stream.plist echo "$(date '+%F %T') service started; checking stability..." sleep 90 if pgrep -f "f flv rtmps://stream.place" > /dev/null; then echo "$(date '+%F %T') LIVE: ffmpeg is streaming to stream.place" else echo "$(date '+%F %T') ffmpeg not running after 90s; see logs/stream.log" tail -5 logs/stream.log exit 1 fi