#!/bin/bash

set -eu
set -o pipefail
#et -x

wrap_up_with=""

# If we receive SIGUSR1, exit at the end of 12 songs
trap "echo $0: 'Will exit at the end of 12 more songs due to SIGUSR1' 1>&2; counter=0; wrap_up_with=12" SIGUSR1

# If we receive SIGUSR2, exit at the end of 18 songs
trap "echo $0: 'Will exit at the end of 18 more songs due to SIGUSR2' 1>&2; counter=0; wrap_up_with=18" SIGUSR2

counter=""
wrap_up_with=""
announce=True
play_forever=""
presleep=""
pause_until_exit=""
if [ -d $HOME/Sound/avconv-Music ]
then
    directory=$HOME/Sound/avconv-Music
elif [ -d $HOME/Sound-local/avconv-Music ]
then
    directory=$HOME/Sound-local/avconv-Music
fi

function usage
{
    retval="$1"
    (
    echo "Usage: $0"
    echo "   --count n"
    echo "   --forever"
    echo "   --no-announce"
    echo "   --presleep seconds"
    echo "   --pause-until-exit processname"
    echo "   --help"
    ) 1>&2
    exit "$retval"
}

while [ "$#" -ge 1 ]
do
    if [ "$1" = --count ]
    then
        counter="0"
        wrap_up_with="$2"
        play_forever="False"
        shift
    elif [ "$1" = --forever ]
    then
        counter=""
        wrap_up_with=""
        play_forever="True"
    elif [ "$1" = --directory ]
    then
        directory="$2"
        shift
    elif [ "$1" = --no-announce ]
    then
        announce=False
    elif [ "$1" = --presleep ]
    then
        presleep="$2"
        shift
    elif [ "$1" = --pause-until-exit ]
    then
        pause_until_exit="$2"
        shift
    elif [ "$1" = --help ]
    then
        usage 0
    else
        echo "$0: Unrecognized option: $1" 1>&2
        usage 1
    fi
    shift
done

cd "$directory"

function pause_until
{
    # We modify the search string to be a regex pattern that doesn't match itself
    process_name=$(echo "$1" | sed 's/^\(.\)\(.*\)$/[\1]\2/')
    if ! notify-when-up2 --no-mail -i 2.5 --no-blocking -s "$process_name"
    then
        exit 1
    fi
}

function produce_speech
{
    if type -path festival > /dev/null 2>&1
    then
        case "$announce" in
            True)
                # Festival seems to do strange things with underscores...
                tr '_' ' ' | festival --tts
                ;;
            False)
                cat > /dev/null
                ;;
            *)
                echo "$0: Invalid \$announce: $announce" 1>&2
                exit 1
                ;;
        esac
    fi
}

function play_one
{
    music_file="$(find . -name '*.mp3' -print | randomize | head -1 || true)"
    echo "$music_file"
    echo "$music_file" | sed -e 's/^/Starting /' -e 's#/#. #g' -e 's/\.mp3/./' | produce_speech
    mplayer -really-quiet "$music_file" 2>&1 | \
        egrep -v '^mplayer: could not connect to socket$|^mplayer: No such file or directory$' || \
        true
    echo "$music_file" | sed -e 's/^/That was /' -e 's#/#. #g' -e 's/\.mp3/./' | produce_speech
}

if [ "$presleep" = "" ]
then
    :
else
    echo "Pre-sleeping for $presleep seconds" | produce_speech
    for i in $(seq "$presleep" -1 1)
    do
        if [ $(echo "$i%60" | bc) = "0" ]
        then
            minutes=$(echo "$i/60" | bc | sed 's/\..*$//')
            echo "$minutes minute mark"
            echo "$minutes minutes remaining in pre-sleep" | produce_speech
        fi
        echo "sleeping $i of $presleep"
        sleep 1
    done
fi

if [ "$pause_until_exit" != "" ]
then
    pause_until "$pause_until_exit"
fi

while [ "$counter" != "" ] || [ "$play_forever" = "True" ]
do
    if [ "$counter" != "" ]
    then
        if [ "$counter" -lt "$wrap_up_with" ]
        then
            echo "Playing $((counter + 1)) of $wrap_up_with" | produce_speech
            counter=$(($counter + 1))
        else
            echo "Done" | produce_speech
            exit 0
        fi
    elif [ "$play_forever" = True ]
    then
        echo "Playing forever" | produce_speech
    else
        echo "$0: Internal error 1" 1>&2
        exit 1
    fi

    play_one
done