SmartestHome/tools/build-audio-endpoint-image-...

99 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Builds the arm64 headless audio-endpoint image (Raspberry Pi + HiFiBerry
# Amp2) with rpi-image-gen. Read hosts/audio-endpoint/README.md before running
# this — in particular the callout that rpi-image-gen's exact config/layer
# schema (config/audio-endpoint.yaml) was not hands-on validated while writing
# this script; a real build may need that file adjusted first.
#
# Output: a real bootable .img. Each physical unit then gets its own
# hostname/Wi-Fi via Raspberry Pi Imager's OS Customisation dialog when
# flashing — see the README's "Per-room identity" section. This script builds
# ONE generic image, not one per room.
set -euo pipefail
# ---------------------------------------------------------------------------
# CONFIGURATION — edit if needed
# ---------------------------------------------------------------------------
RPI_IMAGE_GEN_SRC="${RPI_IMAGE_GEN_SRC:-/opt/smart-home/src/rpi-image-gen}"
RPI_IMAGE_GEN_REPO="https://github.com/raspberrypi/rpi-image-gen.git"
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/coreconfig.sh
source "${SCRIPT_DIR}/lib/coreconfig.sh"
core_select_audio_endpoint arm64 "${1:-}"
# This script now lives in tools/, so the host directory it drives is addressed from
# the repo root rather than relative to the script.
AUDIO_ENDPOINT_DIR="${CORE_REPO_ROOT}/hosts/audio-endpoint"
RPI_IMAGE_GEN_DIR="${AUDIO_ENDPOINT_DIR}/rpi-image-gen"
SHARED_CONFIGS_DIR="$AUDIO_ENDPOINT_DIR/configs"
BUILD_CONFIG="$RPI_IMAGE_GEN_DIR/config/audio-endpoint.yaml"
# ---------------------------------------------------------------------------
# Sanity checks
# ---------------------------------------------------------------------------
for f in spotify-connect.service spotify-connect-start; do
if [ ! -f "$SHARED_CONFIGS_DIR/$f" ]; then
echo "Missing $SHARED_CONFIGS_DIR/$f — is this script running from a full checkout?" >&2
exit 1
fi
done
if [ ! -f "$BUILD_CONFIG" ]; then
echo "Missing $BUILD_CONFIG" >&2
exit 1
fi
if ! command -v git >/dev/null 2>&1; then
echo "git is required to fetch rpi-image-gen. Install it first: sudo apt-get install git" >&2
exit 1
fi
# ---------------------------------------------------------------------------
# 1. Fetch rpi-image-gen if not already present
# ---------------------------------------------------------------------------
if [ ! -d "$RPI_IMAGE_GEN_SRC" ]; then
echo "--- Cloning rpi-image-gen into $RPI_IMAGE_GEN_SRC ---"
git clone --depth 1 "$RPI_IMAGE_GEN_REPO" "$RPI_IMAGE_GEN_SRC"
else
echo "--- rpi-image-gen already present at $RPI_IMAGE_GEN_SRC, skipping clone ---"
fi
# ---------------------------------------------------------------------------
# 2. Build
# ---------------------------------------------------------------------------
echo "--- Running rpi-image-gen build ---"
echo " Config: $BUILD_CONFIG"
cd "$RPI_IMAGE_GEN_SRC"
# UNVERIFIED: assumes a ./rpi-image-gen entry point at the repo root, matching
# common conventions for this class of tool (e.g. pi-gen's own build.sh). If
# the real tool exposes a different entry point (a pip-installed console
# script, a Python module, etc.), fix this one line — everything else in this
# script and config/audio-endpoint.yaml is unaffected either way.
./rpi-image-gen build -c "$BUILD_CONFIG"
echo
echo "=== Done (if rpi-image-gen's invocation above matched the real tool) ==="
echo "Image should be under $RPI_IMAGE_GEN_SRC/work/ — check rpi-image-gen's own"
echo "build output above for the exact path; this wrapper does not parse it."
echo
echo "Next steps:"
echo " 1. Flash with Raspberry Pi Imager (GUI) or:"
echo " rpi-imager --cli <path-to-image>.img /dev/<sd-card-device>"
echo " 2. BEFORE writing, use Imager's OS Customisation (gear icon / Ctrl+Shift+X)"
echo " to set THIS unit's hostname (= its Spotify Connect device name — see"
echo " the README's 'Per-room identity' section), Wi-Fi SSID/password, and"
echo " an SSH key if you want remote access. This is per physical unit, not"
echo " per build — the same .img is reused for every room."
echo " 3. Attach the HiFiBerry Amp2 HAT and passive speakers before first boot."
echo " 4. First boot checklist (see README.md for the full unverified list):"
echo " systemctl status spotify-connect"
echo " # confirm the room's hostname shows up as a Spotify Connect device"
echo " aplay -l # confirm the HiFiBerry is card 0 (onboard audio disabled)"