diff --git a/home/bin/sync-music b/home/bin/sync-music index 3b65dd1..e0d881a 100755 --- a/home/bin/sync-music +++ b/home/bin/sync-music @@ -6,10 +6,10 @@ # How it works: # 1. Ensures the NAS SMB share is mounted at /Volumes/data (uses your keychain # credentials; falls back to a clear error if it can't mount). -# 2. Imports every audio file in ~/Music into beets via -# `beet import --noautotag --move --quiet`, which moves each file into the -# library and removes it from ~/Music. Files already in the library are -# skipped by beets (left in place). Safe to re-run any time. +# 2. Imports every audio file anywhere under ~/Music (recursively) into beets +# via `beet import --noautotag --move --singletons`, which moves each file +# into the library and removes it from ~/Music. Files already in the +# library are skipped (left in place). Safe to re-run any time. # # Why a manual script (not the old daemon)? The daemon kept dying because # launchd/WezTerm spawn a minimal PATH that lacks /opt/homebrew/bin (where @@ -61,13 +61,11 @@ fi # $LIBRARY_DIR/beets/beets_library.db (per ~/.config/beets/config.yaml). log "sync-music: scanning $WATCH_DIR -> $LIBRARY_DIR" -shopt -s nullglob nocaseglob +# Recursively collect every audio file under $WATCH_DIR. files=() -for f in "$WATCH_DIR"/*; do - [ -f "$f" ] || continue +while IFS= read -r -d '' f; do [[ "${f##*.}" =~ ^($AUDIO_EXT)$ ]] && files+=("$f") -done -shopt -u nocaseglob nullglob +done < <(find "$WATCH_DIR" -type f -print0 2>/dev/null) total=${#files[@]} log "Found $total audio file(s)." @@ -86,9 +84,10 @@ for f in "${files[@]}"; do bn=$(basename "$f") - # Pre-check: has beets already imported this file? Match by path or title. + # Pre-check: has beets already imported this file? Match by exact path, or + # by a title guess derived from the filename ("03 - Artist - Title"). existing="$(beet ls --path "$f" 2>/dev/null || true)" - stripped="$(echo "$bn" | sed 's/\.flac$//; s/\.mp3$//')" + stripped="$(echo "$bn" | sed 's/\.[A-Za-z0-9]*$//' | sed -E 's/^[0-9]+ - [^ -]+ - //' )" if [ -n "$existing" ] || [ -n "$(beet ls "$stripped" 2>/dev/null || true)" ]; then log " already in library (skipped): $bn" ok=$((ok + 1))