#!/bin/bash

######################################################################
#
# AUTHOR: Kurt Schwehr, schwehr@ri.cmu.edu
# START DATE: Sep-1999
#
# $Id: sgiSay.bash,v 1.3 1999/11/23 21:12:08 schwehr Exp $
#
# PROJECT: mvacs SSI
# 
# DESCRIPTION: Should do everything required for processing stereo
# pairs to produce output for the WITS user interface.
#
# INPUTS: see help
#
# ENV VARIABLES USED:
#
# OUTPUT: A whole bunch of things.
#
# Notes: Designed to be called by either a luser or fwMaster
#
# See help for more.
#
######################################################################

echo ------------------------------------------------------------
echo --- Welcome to sgiSpeech.bash
echo ------------------------------------------------------------

declare -ri EXIT_FAILURE=1
declare -ri EXIT_SUCCESS=0


declare -ri TERSE=1
declare -ri TRACE=4
declare -ri VERBOSE=8
declare -ri BOMBASTIC=16

if [ -z "$SSI_VERBOSITY" ]; then
    declare -i debugLevel=4
else
    declare -i debugLevel=$SSI_VERBOSITY
fi

# Twisted way to get down to the fundamental script name.
tmp=${0##/*/}
tmp=${tmp%%.bash}
tmp=${tmp##*.}
tmp=${tmp##*/}
declare -r SCRIPT_NAME=$tmp

# $1 is the level to compare against debugLevel
# $2 is the string to echo to stdout.
DebugEcho()
{
    declare -ir val=$1
    if [ "$debugLevel" -ge "$val" ]; then
	#echo $2
	echo "${SCRIPT_NAME}(`printf "%02d" $1`) $2"
    fi
}

#DebugEcho $TERSE     "Terse is on"
#DebugEcho $TRACE     "Trace is on"
DebugEcho $VERBOSE   "Verbose is on"
DebugEcho $BOMBASTIC "Bombastic is on"

DebugEcho $TERSE "debugLevel           = $debugLevel"


######################################################################
# Global variables and constants
######################################################################

if [ -z "$SSI_TOP_DIR" ]; then
    declare -r SSI_TOP_DIR=/proj/ops/ssi/preMORT5
    export SSI_TOP_DIR
    DebugEcho $TRACE "setting top dir = $SSI_TOP_DIR"
fi

if [ -z "$SCRIPT_DIR" ]; then
    declare -r SCRIPT_DIR=${SSI_TOP_DIR}/bin
    export SCRIPT_DIR
    DebugEcho $VERBOSE "setting script dir = $SSI_SCRIPT_DIR"
fi

if [ -z "$BIN_DIR" ]; then
    declare -r BIN_DIR=${SSI_TOP_DIR}/bin/`uname`
    export BIN_DIR
    DebugEcho $VERBOSE "setting bin dir = $SSI_BIN_DIR"
fi

if [ -z "$SSI_DATA_TOP_DIR" ]; then
    declare -r SSI_DATA_TOP_DIR=${SSI_TOP_DIR}/data
    export SSI_DATA_TOP_DIR
    DebugEcho $VERBOSE "setting data top dir = $SSI_DATA_TOP_DIR"
fi

if [ -z "$SSI_STATUS_DIR" ]; then
    declare -r SSI_STATUS_DIR=${SCRIPT_DIR}/status
    DebugEcho $VERBOSE "setting status dir = $SSI_STATUS_DIR"
fi

SetStatus()
{
    echo -n "sgiSay      -- $1" > $SSI_STATUS_DIR/say.status
}

SetStatus "starting up"

#if [ -z "$SSI_RUNTIME_CONTROL_DIR" ]; then
#    echo "FIX: Runtime control should be same as queue dir."
#    declare -r SSI_RUNTIME_CONTROL_DIR=${SCRIPT_DIR}/runtime_control
#    DebugEcho $VERBOSE "setting runtime control dir= $SSI_RUNTIME_CONTROL_DIR"
#fi

if [ -z "$SSI_SAY_QUEUE_DIR" ]; then
    declare -r SSI_SAY_QUEUE_DIR=${SSI_TOP_DIR}/queues/toSay
    DebugEcho $TRACE "ssi say queue dir = $SSI_SAY_QUEUE_DIR"
fi

if [ ! -d $SSI_SAY_QUEUE_DIR ]; then
    echo "ERROR: Queue directory does not exist: "
    echo     $SSI_SAY_QUEUE_DIR
    exit $EXIT_FAILURE
fi

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE  "Image Extension      = $ext"
DebugEcho $VERBOSE  "SSI_TOP_DIR          = $SSI_TOP_DIR"
DebugEcho $VERBOSE  "SSI_DATA_TOP_DIR     = $SSI_DATA_TOP_DIR"
DebugEcho $VERBOSE  "SSI_SAY_QUEUE_DIR = $SSI_SAY_QUEUE_DIR"
DebugEcho $VERBOSE  ""
DebugEcho $VERBOSE  "SCRIPT_DIR           = $SCRIPT_DIR"
DebugEcho $VERBOSE  "BIN_DIR              = $BIN_DIR"

declare -i sleepTime=30	# Default how long to sleep between checks, in seconds.

# SGI say program.
declare -r say=/usr/local/bin/say
#declare -r stereopair2wits=${SCRIPT_DIR}/stereopair2wits.bash
declare -r rm=/bin/rm	# GNU rm seems to segfault a lot on IRIX 6.5???
declare -r gnuDate=${BIN_DIR}/gnu_date # Use the GNU date program.

###############################################
# Go to the queue dir so it is out working dir.

# This will keep the path length down with the for loop.
#cd $SSI_STEREO_QUEUE_DIR
cd $SSI_SAY_QUEUE_DIR

# force all control files to be cleared.
$rm -f *.rtc

DebugEcho $VERBOSE  "FIX: write the PID to $SSI_STATUS_DIR  write say.pid"

HOSTNICK=`uname -n`


######################################################################
# GENERIC HELPER FUNCTIONS
######################################################################

# This script may use stuff not implimented in bash 1.x
checkBashVersion()
{
    declare -ir minBashVersion=2
    declare -ir bashMajorVersion=${BASH_VERSION%%.*}

    if [ $minBashVersion -gt $bashMajorVersion ]; then
	echo "*** ERROR:"
	echo "  Found bash version $BASH_VERSION."
	echo "  Need version ${minBashVersion}.x or higher."
	exit $EXIT_FAILURE
    fi
    DebugEcho $VERBOSE  "  Bash version okay."
}

# Just for safety, let's make sure we are in a known environment.
# If not, then someone will probably have to do some tweaking.
checkOS()
{
    declare -r okOSName=IRIX64
    declare -r OSName=`uname`
    if [ $okOSName != $OSName ]; then
	echo "*** ERROR:"
	echo "  Found OS: $OSName"
	echo "  Expected: $okOSName"
	exit $EXIT_FAILURE
    fi

    #declare -r okOSrev="5.7"
    #declare -r OSrev=`uname -r`
    #if [ $okOSrev != $OSrev ]; then
	#echo "*** WARNING:"
	#echo "  Found OS rev: $OSrev"
	#echo "  Expected: $okOSrev"
	#echo "  CONTINUE AT YOUR OWN RISK"
    #fi
    #DebugEcho $VERBOSE  "  OS checks out okay."
}

######################################################################
# Work Functions
######################################################################

declare -r PAUSE_FILE="pause.rtc"
declare -r RESUME_FILE="resume.rtc"
declare -r STOP_FILE="stop.rtc"

checkRuntimeControl()
{
    if [ -e "$STOP_FILE" ]; then
	$rm -f "$STOP_FILE"
	echo "Found stop file... EXITING NOW!"
	SetStatus "Stopped/done.  Found stop.rtc"
	exit $EXIT_SUCCESS
    fi

    if [ -e "$PAUSE_FILE" ]; then
	$rm -f "$PAUSE_FILE"
	DebugEcho $TRACE "Found pause file.  Waiting for $RESUME_FILE"

	while [ ! -e "$RESUME_FILE" ]; do
	    DebugEcho $BOMBASTIC "Sleeping 10 seconds"
	    SetStatus "Paused.  Waiting for resume.rtc"
	    sleep 10
	done
	$rm -f $RESUME_FILE
	DebugEcho $TRACE "Resuming"
	SetStatus "Resuming"
    fi
}

# Watch the queue for new images, and start the processing of them.
update ()
{
    SetStatus "Scanning directory"

    for file in *.say
    do
        checkRuntimeControl

	if [ ! -f $file ]; then
	    DebugEcho $VERBOSE "Queue directory empty"
	    DebugEcho $VERBOSE "cannot open $file - this may be fine."
	    true # nop - empty directory.
	else
	    SetStatus "Saying stuff"
	    DebugEcho $TRACE "Files remaining: `ls -1 *.say 2>/dev/null|wc -l`"
	    DebugEcho $VERBOSE  "say: Calling say $file"
	    DebugEcho $TRACE "Saying: `cat $file`"
	    $say "`cat $file`"
	    $rm $file
	fi
    done
    SetStatus "done with update"
}



######################################################################
# Main code - starts here
######################################################################

DebugEcho $VERBOSE  ""
DebugEcho $VERBOSE  "Paranoid Mode:"
checkBashVersion
checkOS

#
# Command line options
#
case "$1" in

u | 'update')
    DebugEcho $VERBOSE  "update:"
    DebugEcho $VERBOSE 
    update
    ;;

uc | 'update-continuous')
    DebugEcho $VERBOSE  "update-continuous:"
    DebugEcho $VERBOSE 

    if [ $# -eq 2 ]; then
	sleepTime=$2
    fi
    DebugEcho $VERBOSE  "Time between update starts = $sleepTime (seconds)"
    if [ 1 -gt $sleepTime ]; then
	DebugEcho $VERBOSE  "ERROR: must specify sleep time 1 or more seconds."
	exit 1
    fi

    declare -i startTime
    declare -i curTime
    declare -i deltaTime

    DebugEcho $VERBOSE  "Setting up startTime"
    startTime=`$gnuDate \+\%s`
    startTime=$startTime-$sleepTime

    while [ 1 ]; do

        # Look for responce from the lusers who control me.
        checkRuntimeControl

	DebugEcho $VERBOSE  ""
	startTime=`$gnuDate \+\%s`
	update
	curTime=`$gnuDate \+\%s`
	deltaTime=$curTime-$startTime

	if [ $deltaTime -lt $sleepTime ]; then
	    deltaTime=$sleepTime-$deltaTime
	    DebugEcho $VERBOSE  ""
	    DebugEcho $VERBOSE  "sleeping for $deltaTime seconds..."
	    SetStatus "Idle but running"
	    sleep $deltaTime
	fi

    done
    ;;

*)
    echo ""
    echo "usage: $0 {update|update-continuous|clean-data}"
    #echo "usage: $0 {update|update-continuous|clean|clean-links|clean-nuke}"
    echo ""
    echo "  update      -- check for new files; update symbolic links & queue"
    echo "  update-continuous [optionalNumSeconds]"
    echo "              -- run update every x seconds"
    echo "                 Defaults to $sleepTime seconds"
    echo ""
    echo "  "
    exit 1
    ;;

esac



######################################################################
# END OF FILE

#
# $Log: sgiSay.bash,v $
# Revision 1.3  1999/11/23 21:12:08  schwehr
# This MUST be run on an SGI.
#
# Revision 1.2  1999/11/23 21:11:16  schwehr
# Removed extra cruft from MCP.bash
#
# Revision 1.1  1999/11/23 21:00:13  schwehr
# sgiSay.bash
#

# Last MCP version before
# Revision 1.18  1999/11/20 01:22:29  schwehr
# Minor debug echo cleanup.
