sad-add (1729 bytes)
1 #!/bin/bash 2 # BUG: need to use absolute paths 3 # 4 SAD_SOCK=${SAD_SOCK:-"$HOME"/.socket} 5 SAD_DEFAULT_MODE=${SAD_DEFAULT_MODE:-"random"} 6 7 # Portable echo. No arguments. 8 echo () { printf %s\\n "$*" ; } 9 10 usage () { 11 echo "usage: $0 [-p playlist] [subpaths...]" 12 echo "$0 mus/song.mp3" 13 echo "$0 mus/eminem" 14 echo "$0 -p mus/programming.list # can include folders or files." 15 } 16 17 [ -z "$*" ] && usage && exit 1 18 19 nosock () { 20 echo "Socket not found at $SAD_SOCK" 21 echo "Did you start the SAD?\nsad -f $SAD_SOCK &" 22 } 23 nopath () { 24 echo "Playlist/folder not found." 25 echo "You can set the working dir with." 26 } 27 28 file () { 29 printf 'add %s\n' "$1" | nc -NU "$SAD_SOCK" 30 } 31 32 dir () { 33 find "$1" -type f \ 34 \( -iname "*.mp3" -or \ 35 -iname "*.ogg" -or \ 36 -iname "*.flac" -or \ 37 -iname "*.wav" \) \ 38 -exec printf 'add %s\n' {} \; | \ 39 nc -NU "$SAD_SOCK" 40 } 41 42 dispatch () { 43 if [ ! -e "$1" ]; then 44 echo "Path $1 not found" 45 else 46 if [ -f "$1" ]; then 47 file "$1" 48 else 49 dir "$1" 50 fi 51 fi 52 } 53 54 55 playlist () { 56 # dirname clobbers multiple trailing newlines 57 d=$(dirname "$1" ; echo x) ; d=${d%??} 58 while read -r p; do 59 dispatch "$d/$p" 60 done <<EOF 61 $(cat "$1") 62 EOF 63 } 64 65 [ -e "$SAD_SOCK" ] || nosock 66 67 if [ ! -S "$SAD_SOCK" ]; then 68 echo "$0: no unix domain socket at: ${SAD_SOCK}" >&2 69 exit 1 70 fi 71 72 echo "$SAD_DEFAULT_MODE" | nc -NU "$SAD_SOCK" 73 74 75 while true; do 76 case $1 in 77 -h|--help) usage; exit 0;; 78 -p|--playlist) playlist "$2"; shift 2; break;; 79 --) shift; break;; 80 *) dispatch "$1"; shift; break;; 81 esac 82 done