#!/bin/bash
#
# $Id: speak,v 1.1.1.1 2002/10/27 21:18:34 adicvs Exp $
#
# Play back the temperature on the IRLP node
#
# This script reads the cached temperature as provided by the get_temp
# script. It processes the numerical temperature values, generates a
# list of wav files and playes the wav files on the node.
#
# Read the README file for further instructions ans requirements.
#

# Make sure we are user repeater!!!
if [ `/usr/bin/whoami` != "repeater" ] ; then
  echo "This program must be run as user REPEATER!"
  exit 1
fi

# Make sure we have sourced the environment file
if [ "$RUN_ENV" != "TRUE" ] ; then
  echo "You must source the environment file first. Do this by running:"
  echo ". /home/irlp/custom/environment"
  exit 1
fi

# Set the path and source the config if needed
if [ ! "$homedir" ]; then
    homedir=`dirname $0`
fi

# Source config
. "$homedir/config"

# List of wav files
wav=""

# Usage information
usage ()
{
    prg=`basename $0`
    cat<<EOF
Usage: $prg <scale>
    Play temperature reading on IRLP node.
 
    celsius        temperature in degrees celsius (default)
    farenheit      temperature in degrees farenheit

Report bugs to <adi@adis.on.ca>.
EOF
}

# Create wav from 2 digit number
two_to_wav ()
{
    num=$1
    num_1=`echo $num | cut -b 2`
    num_10=`echo $num | cut -b 1`
    case $num in
      00)
        wav="$wav $wavpath/0.wav"
      ;;
      ?|1?|?0)
        wav="$wav $wavpath/$num.wav"
        ;;
      0?)
        wav="$wav $wavpath/0.wav"
        wav="$wav $wavpath/$num_1.wav"
        ;;
      *)
        wav="$wav $wavpath/${num_10}0.wav"
        wav="$wav $wavpath/$num_1.wav"
        ;;
    esac
}

# Create wav from 3 digit number
three_to_wav ()
{
    num=$1
    num_100=`echo $num | cut -b 1`
    case $num in
      ?00)
        wav="$wav $wavpath/$num.wav"
        ;;
      ?0?)
        num=`echo $num | cut -b 3-`
        wav="$wav $wavpath/${num_100}00.wav"
        two_to_wav $num
       ;;
      ???)
        num=`echo $num | cut -b 2-`
        wav="$wav $wavpath/${num_100}00.wav"
        two_to_wav $num
        ;;
      *)
        two_to_wav $num
        ;;
    esac
}

# Temperature
temp_to_wav ()
{
    # Announce the temperature
    wav="$wav $wavpath/temperature_is.wav"

    # Get the temperature
    if [ "$1" = "farenheit" ]; then
        temp=`head -1 $tempf | sed 's/ *//g'`
    else
        temp=`head -1 $tempc | sed 's/ *//g'`
    fi
    
    # Process negative temperature
    if [ `echo $temp | cut -b 1` = "-" ]; then
        wav="$wav $wavpath/minus.wav"
        temp=`echo $temp | cut -b 2-`
    fi

    # Extract integer and decimal from temperature value
    int=`echo $temp | sed 's/\(.*\)\..*/\1/'`
    dec=`echo $temp | sed 's/.*\.\([0-9]\).*/\1/'`

    three_to_wav $int
    wav="$wav $wavpath/point.wav"
    wav="$wav $wavpath/$dec.wav"
    
    # Add the scale
    if [ "$1" = "farenheit" ]; then
        wav="$wav $wavpath/degrees_farenheit.wav"
    else
        wav="$wav $wavpath/degrees_celsius.wav"
    fi
}

# Play the wav files from this script
my_wavplay ()
{
    # This sequence of events has been taken from $SCRIPT/wavplay
    $BIN/coscheck
    # $BIN/forcekey
    $BIN/key
    usleep 900000
    "$BIN"/play $wav
    usleep 500000
    # $BIN/forceunkey
    $BIN/unkey
}

##
## Main
##

# Make sure we have cached temperatures to process
if [ -s "$tempf" -a -s "$tempc" ]; then

    # Process arguments
    case "$1" in
        celsius)
            temp_to_wav
            ;;
        farenheit)
            temp_to_wav farenheit
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            temp_to_wav
            ;;
    esac
else

    # Show error message and play error wav
    echo "$tempf and $tempc files empty or not found!"
    echo "Read README and run $homedir/get_temp"
    wav="$wavpath/oops.wav"
fi

# Make sure we have something to process
if [ "$wav" = "" ]; then
    echo "An error orrured, no .wav files to play."
    wav="$wavpath/oops.wav"
fi

# Control IRLP node and play the wave file
if [ -f "$ACTIVE" ]; then
    # We are connected to a node.  Terminate data stream from 
    # remote node, play wav and let user know we are
    # connected.
    killall ispeaker >&/dev/null 2>&1
    killall ispeaker_PCI >&/dev/null 2>&1
    killall sfswrapper >&/dev/null 2>&1
    CONNECTED=`cat $ACTIVE`
    my_wavplay
    "$SCRIPT"/wavplay connected "$CONNECTED"
    "$SCRIPT"/sfswrapper
else
    # We are not connected. Just play the wav files.
    my_wavplay
fi

