mpcq (1675B)
1 #!/bin/sh 2 # Queue one or more files in mpd via mpc 3 printfq() { printf "'%s'\\n" "$(printf '%s' "$1" | sed -e "s/'/'\\\\''/g")"; } 4 die() { printf '%s\n' "$1" >&2 && exit 1; } 5 6 checkdeps() { 7 for com in "$@"; do 8 command -v "$com" >/dev/null 2>&1 \ 9 || { printf '%s not found.\n' "$com" >&2 && exit 1; } 10 done 11 } 12 checkdeps mpc 13 14 pgrep mpd >/dev/null 2>&1 || die "mpd is not running." 15 [ -S ~/.local/share/mpd/socket ] || die "$HOME/.local/share/mpd/socket does not exist or is not a socket." 16 17 queue() { 18 case "$1" in 19 *.wav|*.flac|*.mp3|*.m4a|*.opus|*.webm) 20 { [ -n "$next" ] && queuecmd="insert"; } || queuecmd="add" 21 mpc -h ~/.local/share/mpd/socket "$queuecmd" "file://$1" 22 printf "Queued: %s\n" "$1" 23 ;; 24 *.m3u) 25 mpc -h ~/.local/share/mpd/socket load "file://$1" 26 printf "Loaded playlist: %s\n" "$1" 27 ;; 28 esac 29 } 30 31 PARAMS="" 32 while [ $(($#)) -ne 0 ]; do 33 case "$1" in 34 -n|--next) 35 next=1 36 shift 37 ;; 38 39 -h|--help) 40 echo "Usage:" 41 echo "mpcq [-n] file1 [file2..]" 42 exit 0 43 ;; 44 --) # end arg parsing 45 shift 46 break 47 ;; 48 -*) # unsupported flags 49 echo "Unsupported flag $1" >&2 50 exit 1 51 ;; 52 *) # preserve positional arguments 53 PARAMS="$PARAMS $(printfq "$1")" 54 shift 55 ;; 56 esac 57 done 58 eval set -- "$PARAMS" 59 60 if ! [ -t 0 ] && [ $# -eq 0 ]; then 61 while read -r line; do 62 filepath="$(realpath "$line")" 63 [ -f "$filepath" ] && queue "$filepath" 64 done 65 elif [ $# -gt 0 ]; then 66 for f in "$@"; do 67 filepath="$(realpath "$f")" 68 [ -f "$filepath" ] && queue "$filepath" 69 done 70 else 71 die "Files expected as arguments or on stdin." 72 fi