#!/usr/bin/env bash
# Restore (or re-sync) the MPD + rmpc + cava music setup from music/.
# See docs/MUSIC.md for what the setup is and how the pieces fit together.
#
# install-music.sh install configs into place, enable services
# install-music.sh sync reverse direction: copy live configs back
# into music/ (run after tweaking the live setup)
# install-music.sh diff show what differs between music/ and live
#
# Idempotent. Existing live files that differ are backed up as
# <name>.bak-YYYYMMDD-HHMMSS before being overwritten. Never touches the
# music library, the MPD database, or downloaded lyrics.
set -euo pipefail
DOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SRC="$DOT/music"
MODE="${1:-install}"
# repo-relative-path<space>live-path, one per line.
MAP="\
mpd/mpd.conf $HOME/.config/mpd/mpd.conf
mpd/mpd.service-override.conf $HOME/.config/systemd/user/mpd.service.d/override.conf
pipewire/10-sample-rates.conf $HOME/.config/pipewire/pipewire.conf.d/10-sample-rates.conf
rmpc/config.ron $HOME/.config/rmpc/config.ron
rmpc/themes/rose-pine.ron $HOME/.config/rmpc/themes/rose-pine.ron
rmpc/themes/daemon.ron $HOME/.config/rmpc/themes/daemon.ron
rmpc/scripts/fetch-lyrics $HOME/.config/rmpc/scripts/fetch-lyrics"
case "$MODE" in
install)
if ! pacman -Q mpd rmpc cava mpc mpd-mpris >/dev/null 2>&1; then
echo ":: installing packages (needs auth)"
sudo pacman -S --needed mpd rmpc cava mpc mpd-mpris
fi
while read -r repo live; do
[ -z "$repo" ] && continue
if [ -e "$live" ] && ! cmp -s "$SRC/$repo" "$live"; then
cp -p "$live" "$live.bak-$(date +%Y%m%d-%H%M%S)"
echo "backed up: $live"
fi
mkdir -p "$(dirname "$live")"
cp "$SRC/$repo" "$live"
echo "installed: $live"
done <<<"$MAP"
chmod +x "$HOME/.config/rmpc/scripts/fetch-lyrics"
mkdir -p "$HOME/.local/share/mpd/playlists" "$HOME/.local/share/rmpc/lyrics"
systemctl --user daemon-reload
# mpd.socket would bind *:6600 on the LAN -- always the .service instead.
systemctl --user disable --now mpd.socket 2>/dev/null || true
systemctl --user enable --now mpd.service mpd-mpris.service
systemctl --user try-restart pipewire.service pipewire-pulse.service wireplumber.service
echo ":: done. launch with: rmpc"
;;
sync)
while read -r repo live; do
[ -z "$repo" ] && continue
if [ -e "$live" ]; then
cp "$live" "$SRC/$repo" && echo "synced: $repo"
else
echo "missing live file, skipped: $live" >&2
fi
done <<<"$MAP"
;;
diff)
while read -r repo live; do
[ -z "$repo" ] && continue
if [ ! -e "$live" ]; then
echo "missing live: $live"
elif ! cmp -s "$SRC/$repo" "$live"; then
echo "differs: $repo"
diff -u "$SRC/$repo" "$live" || true
fi
done <<<"$MAP"
;;
*)
echo "usage: install-music.sh [install|sync|diff]" >&2
exit 1
;;
esac