#!/bin/sh -e # Play given files on mpd, enabling playing of external files through symlinking and recursively resolving playlists # depends: xargs mpc realpath # env: MUSIC links="$MUSIC/links" test "$1" = "-r" || next=true { for arg; do path="$(test -e "$MUSIC/$arg" && echo "$MUSIC/$arg" || realpath "$arg")" test -n "$path" || continue if file "$path" | grep -i ' playlist' || expr "$path" : ".*\.m3u8\?$" >/dev/null then pushd "$(dirname "$path")" >/dev/null && cat "$path" | xargs --delim='\n' play -r && popd >/dev/null else find "$path" -name "*.flac" -o -name "*.mp3" | while read file do case "$path" in ($MUSIC/*) echo "${file/$MUSIC\/}";; (*) mkdir -p "$links" && ln -fs "$file" "$links/$(basename "$file")" && echo "links/$(basename "$file")";; esac done fi done && mpc update --wait -q; } | xargs --delim='\n' mpc insert test "$next" && test "$(mpc status | wc -l)" -gt 1 || mpc play -q && mpc next