#!/bin/bash # Sourced helper: regenerate one playlist's .m3u8 from the beets library. # # regen_m3u # # Writes atomically (temp file in the same dir, then rename over the target): # rename(2) is atomic for a reader like Navidrome, and needs write access only # on the directory, so it also replaces a stray wrong-owner leftover .m3u8 (e.g. # from a pre-container run) without a host-side chown. # # Relies on the caller's log() function and $LOG. Always returns 0 (an empty # result is a warning, not a failure), so it is safe to call as a standalone # statement under set -euo pipefail. regen_m3u() { local playlist_name="$1" local out="$2" mkdir -p "$(dirname "$out")" local beet_output # || true: beet ls exits non-zero on no match, which would trip set -e. beet_output=$(beet ls -f '$path' "grouping:${playlist_name}" 2>>"$LOG" || true) local track_count=0 [[ -n "$beet_output" ]] && track_count=$(printf '%s\n' "$beet_output" | wc -l) if [[ "$track_count" -gt 0 ]]; then local tmp="${out}.tmp.$$" { echo "#EXTM3U" echo "$beet_output" } > "$tmp" mv "$tmp" "$out" log "M3U updated with $track_count tracks" else log "WARNING: beet list returned no tracks for grouping:${playlist_name} — not updating M3U" fi return 0 }