#! /bin/bash
MYNAME=$(basename $0)
MYDIR=$(dirname $0)
MYDIR=$(readlink -m "$MYDIR")
VERSION="0.0.1"
VERBOSE=""
LOGFILE=""
SERVER="192.168.1.104"

#
# Prints the software version and exits.
#
function printVersionAndExit()
{
    echo "$MYNAME Version $VERSION on $(hostname)" >&2
}

#
# $*: the error message
#
#
# Prints an error message to the standard error. The text will not mixed up with
# the data that is printed to the standard output.
#
function printError()
{
    local datestring=$(date "+%Y-%m-%d %H:%M:%S")

    echo -e "$MYNAME($$) $*" >&2

    if [ "$LOGFILE" ]; then
        echo -e "$datestring ERROR $MYNAME($$) $*" >>"$LOGFILE"
    fi
}

#
# $*: the message
#
# Prints all the arguments but only if the program is in the verbose mode.
#
function printVerbose()
{
    local datestring=$(date "+%Y-%m-%d %H:%M:%S")

    if [ "$VERBOSE" == "true" ]; then
        echo -e "$MYNAME($$) $*" >&2
    fi

    if [ "$LOGFILE" ]; then
        echo -e "$datestring DEBUG $MYNAME($$) $*" >>"$LOGFILE"
    fi
}

function printHelpAndExit()
{
cat <<EOF
Usage:
  $MYNAME [OPTION]... [CONTAINER_NAME]...

  $MYNAME - Creates and starts containers.

 -h, --help           Print this help and exit.
 -v, --version        Print version information and exit.
 --verbose            Print more messages.
 --log-file=FILE      Store all the messages in the given file too.
 --server=SERVER      Receive messages from this MQTT server.

EOF
    exit 0
}

ARGS=$(\
    getopt \
        -o hvs:c:l \
        -l "help,verbose,version,log-file:,server:" \
        -- "$@")

if [ $? -ne 0 ]; then
    exit 6
fi

eval set -- "$ARGS"
while true; do
    case "$1" in
        -h|--help)
            shift
            printHelpAndExit
            ;;

        --verbose)
            shift
            VERBOSE="true"
            VERBOSE_OPTION="--verbose"
            ;;

        -v|--version)
            shift
            VERSION_OPTION="--version"
            ;;

        --log-file)
            shift
            LOGFILE=$(readlink -m "$1")
            shift
            ;;

        --server)
            shift
            SERVER="$1"
            shift
            ;;

        --)
            shift
            break
            ;;

        *)
            ;;
    esac
done

input=tts/say
pipe="/var/tmp/say_server_backpipe";
pidfile="/var/tmp/say_server.pid"

function ctrl_c() 
{
    local pid=$(cat "$pidfile")

    echo "Cleaning up..."
    echo "Removing '$pipe'."
    rm -f $pipe; 

    echo "Killing PID ${pid}..."
    kill $pid 2>/dev/null

    if [[ "$?" -eq "0" ]]; then
        echo "Exit success";exit 0
    else
        exit 1
    fi
}

function listen()
{
    echo "listen"

    ([ ! -p "$pipe" ]) && mkfifo $pipe
    (mosquitto_sub -h $SERVER -t $input >$pipe 2>/dev/null) &
    echo "$!" > "$pidfile"
    echo "Pid is $(cat $pidfile)"

    while read line <$pipe; do
        echo "$line"
        if [ "$line" == "play keyok" ]; then
            play \
                /home/pipas/Desktop/star-trek-sounds/keyok1.mp3 \
                >/dev/null 2>/dev/null &
        else
            echo "$line" | festival --tts &
        fi
    done
}

trap ctrl_c INT
listen
