#!/bin/bash
#
# tts-audio.sh - allstar Text to Speech
#
#  by w0anm
#
#  This scripts converts the file and calls the google-tts.py script
# to convert the text to an audio file (mp3).
#
#######################
# $Id: tts_audio.sh 24 2015-03-27 00:53:24Z w0anm $

# variables
TMPDIR=/tmp/tts
PID=$$
TMPFILE1=/tmp/tts/tmpfile1${PID}
TMPFILE2=/tmp/tts/tmpfile2${PID}
TTS_LOCK=$TMPDIR/lock_tts
VOL=1.5

old_IFS=$IFS
IFS=$'\n'

# setup lock file
readonly PROGNAME=$(basename "$0")
readonly LOCKFILE_DIR=$TMPDIR
readonly LOCK_FD=200

## Functions Start

lock() {
    local prefix=$1
    local fd=${2:-$LOCK_FD}
    local lock_file=$LOCKFILE_DIR/$prefix.lock

    # create lock file
    eval "exec $fd>$lock_file"

    # aquire the lock
    flock -n $fd \
        && return 0 \
       || return 1
}

help() {
    echo
    echo "$PROGNAME tts_audio.sh [-v <volume>] filename"
    echo "  Where the -v allows you to set the output volume generated by the script."
    echo "  The accepted values are 0-10, where 0 is the lowest and 10 is the "
    echo "  highest volume setting. If you do not use this option, the default"
    echo "  volume is 3."
    echo
    echo "For example:"
    echo "    tts_audio.sh -v 4 /tmp/hello.txt"
    echo
    echo "This will create a file called /tmp/hello.ul"
    echo
}

main() {

    #if lock is present, wait until lock is removed. Should only have
    # one instance of this program running.

    until lock $PROGNAME ; do
        sleep 20
    done

    ## swift -n Callie -m text -f $FILE -p audio/encoding=ulaw,audio/sampling-rate=8000,audio/volume=$VOL -o ${TMPDIR}/audio${PID}.ul
    /usr/bin/text2wave -F 8000 -scale $VOL -otype ulaw  $FILE -o ${TMPDIR}/audio${PID}.ul

    if [ ! -f ${TMPDIR}/audio${PID}.ul ] ; then
        echo "Failed to convert wav file to u-law audio file (sox failure)"
        echo "Aborting..."
        exit
    fi

    IFS=$old_IFS
    mv ${TMPDIR}/audio${PID}.ul ${FILE%.txt}.ul
}

# end of functions...

#####################################################################
# main

# read the options
TEMP=`getopt -o a::sv: --long arga::,args,argv: -n 'tts_audio.sh' -- "$@"`
eval set -- "$TEMP"

# extract options and their arguments into variables.
while true ; do
    case "$1" in
        -a|--arga)
            case "$2" in
                "") ARG_A='some default value' ; shift 2 ;;
                *) ARG_A=$2 ; shift 2 ;;
            esac ;;
        -s|--split) SPLIT=1 ; shift ;;
        -v|--vol)
            case "$2" in
                "") shift 2 ;;
                *) VOL=$2 ; shift 2 ;;
            esac ;;
        --) shift ; break ;;
        *) echo "Internal error!" ; exit 1 ;;
    esac
done

FILE=$1

VOL_ARRAY=(-0.0 .2 .4 .6 .8 1.0 1.2 1.4 1.6 1.8 2.0 )
# VOL_ARRAY=(0 10 20 30 40 50 60 70 80 100 120 )
# get converted volume
VOLCONV=${VOL_ARRAY[$VOL]}

if [ "$VOLCONV" = "" ] ; then
    echo "Error in setting the volume. The argument must be an integer value from"
    echo " 1 through 10,  aborting"
    exit 1
fi

VOL=$VOLCONV

# make sure that the temp directory is there
if [ ! -d $TMPDIR ] ; then
    mkdir -p $TMPDIR
fi

if [ $# -eq 0 ] ; then
    echo "No arguments supplied"
    help
    exit 1
fi

# call main function.
main

exit 0
