#!/bin/bash PIDFILE=/tmp/`basename $0`.$LOGNAME.pid sleepinterval=1 excludefile="/home/chkno/R/musicdb/files" #playcommand="amp -q" playcommand="mplayer -really-quiet" watch=false kill=false verbose=true rand="sort -R" clip=false ejectplaylist=cat pattern='\.\([Mm][Pp][cC23]\|[Oo][Gg][Gg]\|[Ff][Ll][Aa][Cc]\|[Ss][Pp][Ee]*[Xx]|[Aa][Pp][Ee]\)$' pretty=true max=999999999 usefind=false follow= dir=$(pwd) while getopts LPRc:d:Efi:Ikm:o:p:qw opt;do case $opt in R) rand=sort;; q) verbose=false;; c) playcommand="$OPTARG";; d) dir="$OPTARG";; E) clip=true; clipcmd="tail -c 120000";; f) usefind=true;; L) usefind=true; follow="-follow";; I) clip=true; clipcmd="head -c 120000";; i) sleepinterval=$OPTARG;; k) kill=true;; m) max=$OPTARG;; o) ejectplaylist="tee $OPTARG";; p) pattern="$OPTARG";; P) pretty=false;; w) watch=true;; esac done a="" if $watch;then while true;do olda=$a a=$(ps axww |egrep "$playcommand.*${pattern//\\/}"|head -n 1|sed "s@.*$playcommand @@") if [ "$a" -a "$a" != "$olda" ];then echo "$a" fi sleep 10 done fi if $kill;then if [ -r $PIDFILE ];then if ps -p`cat $PIDFILE`|grep -q `basename $0`;then kill `cat $PIDFILE` exit 0 else echo Removing stale lockfile.. >&2 rm $PIDFILE echo `basename $0` is not running. >&2 exit 78 fi else echo `basename $0` is not running. >&2 exit 78 fi fi if [ -r $PIDFILE ];then if ps -p`cat $PIDFILE`|grep -q $0;then echo `basename $0` is already running at pid `cat $PIDFILE`. >&2 exit 78 else echo Removing stale lockfile.. >&2 fi fi exec 5<&0 exec 6>&1 if $usefind;then find "$dir" $follow -not -type d | egrep "${pattern//\\/}" else locate -r "^${dir%/}/.*$pattern" fi | egrep -v -f "$excludefile" | $rand | head -n $max | $ejectplaylist | while read filename;do # get the correct PID. $$ is the parent shell at this point. perl -e 'print getppid();' > $PIDFILE if $verbose;then echo "$filename" | cat -v >&6 # remove cat-v when localization works fi if $clip;then $clipcmd "$filename" | $playcommand - 2>&1 else $playcommand "$filename" <&5 2>&1 fi read -t $sleepinterval <&5 2>&1 done | if $pretty;then cat > /dev/null else cat fi rm $PIDFILE # The original version: # lam -s"a=\"" - -s"\";echo \"\$a\";$playcommand \"\$a\";sleep 1"|sh