feat: add picoreplayer script

This commit is contained in:
2025-05-24 09:58:22 +02:00
parent bc57914131
commit a078503a89

115
raspberry/picoreplayer.sh Executable file
View File

@@ -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 <<EOF
Usage: sudo $0 --device <rpi-4|rpi-zero|rpi-zero-2> \
--name <hostname> \
--sdcard </dev/sdX> \
[--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" <<EOF
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=staff
country=AT
update_config=1
network={
ssid="$WIFI_SSID"
psk="$WIFI_PSK"
scan_ssid=1
key_mgmt=WPA-PSK
auth_alg=OPEN
}
EOF
# Set hostname
echo "$NAME" > "$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'."