From a078503a890d22e8daf048a239184239a02ce52a Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Sat, 24 May 2025 09:58:22 +0200 Subject: [PATCH] feat: add picoreplayer script --- raspberry/picoreplayer.sh | 115 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100755 raspberry/picoreplayer.sh diff --git a/raspberry/picoreplayer.sh b/raspberry/picoreplayer.sh new file mode 100755 index 0000000..7d8baac --- /dev/null +++ b/raspberry/picoreplayer.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Default env file +ENV_FILE="./.env" + +# PiCorePlayer version to use +PCP_VERSION="10.0.0" +BASE_URL="https://repo.pcplayer.org/insitu/piCorePlayer${PCP_VERSION}" +IMG_ZIP="piCorePlayer${PCP_VERSION}.img.xz" +DOWNLOAD_URL="${BASE_URL}/${IMG_ZIP}" + +# Print usage +usage() { + cat < \ + --name \ + --sdcard \ + [--env-file /path/to/.env] + + --device Target board (rpi-4, rpi-zero, rpi-zero-2) + --name Hostname for the Pi + --sdcard Device node of the SD card (e.g. /dev/sdb) + --env-file File containing WIFI_SSID and WIFI_PSK (defaults to ./.env) +EOF + exit 1 +} + +# Parse args +DEVICE="" NAME="" SDCARD="" +while [[ $# -gt 0 ]]; do + case "$1" in + --device) DEVICE="$2"; shift 2;; + --name) NAME="$2"; shift 2;; + --sdcard) SDCARD="$2"; shift 2;; + --env-file) ENV_FILE="$2"; shift 2;; + -h|--help) usage;; + *) echo "Unknown option: $1"; usage;; + esac +done + +# Validate +if [[ -z "$DEVICE" || -z "$NAME" || -z "$SDCARD" ]]; then + usage +fi +if [[ ! -b "$SDCARD" ]]; then + echo "Error: $SDCARD is not a block device." >&2 + exit 1 +fi +if [[ ! -f "$ENV_FILE" ]]; then + echo "Error: env file '$ENV_FILE' not found." >&2 + exit 1 +fi + +# Load Wi-Fi credentials +# Expect lines: WIFI_SSID=... and WIFI_PSK=... +source "$ENV_FILE" +if [[ -z "${WIFI_SSID:-}" || -z "${WIFI_PSK:-}" ]]; then + echo "Error: WIFI_SSID and WIFI_PSK must be set in $ENV_FILE" >&2 + exit 1 +fi + +# Download if needed +if [[ ! -f "$IMG_ZIP" ]]; then + echo "Downloading $IMG_ZIP ..." + wget -q --show-progress "$DOWNLOAD_URL" +fi + +# Flash image +echo "Flashing $IMG_ZIP to $SDCARD (this will overwrite everything!)" +sync +xzcat $IMG_ZIP | dd of="$SDCARD" bs=4M status=progress conv=fsync +sync + +# Determine partition suffixes (/dev/sdX1 vs /dev/mmcblk0p1) +if [[ "$SDCARD" =~ [0-9]$ ]]; then + PART1="${SDCARD}p1" + PART2="${SDCARD}p2" +else + PART1="${SDCARD}1" + PART2="${SDCARD}2" +fi + +# Mount partitions +BOOT_MNT=$(mktemp -d) +ROOT_MNT=$(mktemp -d) +mount "$PART1" "$BOOT_MNT" +mount "$PART2" "$ROOT_MNT" + +# Create hidden Wi-Fi config on boot partition (picoreplayer picks this up) +cat > "$BOOT_MNT/wpa_supplicant.conf" < "$ROOT_MNT/etc/hostname" +# Update /etc/hosts +sed -i "s/127\.0\.1\.1.*/127.0.1.1\t$NAME/" "$ROOT_MNT/etc/hosts" + +# Clean up +umount "$BOOT_MNT" "$ROOT_MNT" +rmdir "$BOOT_MNT" "$ROOT_MNT" + +echo "Done! SD card is ready with PiCorePlayer on $DEVICE, hostname '$NAME', hidden Wi-Fi '$WIFI_SSID'."