moode audio watchdog

Maybe you want to make sure that your moode audio player is nearly always outputting a signal. Moode does not handle this by itself, at least not predictable. So I hacked together the following shell script which I execute by cron every minute:

#!/bin/sh
#
# simple-moode-watchdog.sh
# checks if other media renderer stuck in paused state, if so, unblock that
# checks if radio stream still playing, would otherwise restart playback

if [ -n "$(cat /proc/asound/card0/pcm0p/sub0/hw_params | grep closed)" ]
then
        if [ -n "$(cat /var/local/www/currentsong.txt | grep Spotify)" ]
        then
                /usr/local/bin/moodeutl -R --spotify
        elif [ -n "$(cat /var/local/www/currentsong.txt | grep Airplay)" ]
        then
                /usr/local/bin/moodeutl -R --airplay
        elif [ -n "$(cat /var/local/www/currentsong.txt | grep Bluetooth)" ]
        then
                /usr/local/bin/moodeutl -R --bluetooth
        fi
fi

if [ -n "$(cat /var/local/www/currentsong.txt | grep state\=play)" ] && [ -n "$(mpc | grep play)" ]
then
        timestamp1=$(mpc status | awk 'NR==2 { split($3, a, "/"); print a[1]}')
        sleep 10
        timestamp2=$(mpc status | awk 'NR==2 { split($3, a, "/"); print a[1]}')
        if [ "$timestamp1" = "$timestamp2" ]; then
                mpc -q stop
                mpc -q play
        fi
fi

if [ -n "$(cat /var/local/www/currentsong.txt | grep state\=stop)" ] || [ -n "$(cat /var/local/www/currentsong.txt | grep state\=pause)" ]
then
        mpc -q clear
        mpc -q load "Favorites"
        mpc -q play
fi

if ! pgrep -x "librespot" > /dev/null
then
        /usr/local/bin/moodeutl -R --spotify
fi