#!/bin/sh
#
# calibrate_cue <cue name>
#
# version 0.3.0
#
# history
#
# 0.3.0  2014-10-03     Switched to using GIANT_DATA_DIR and GIANT_DIR
#                       environment variables to find the sound files as
#                       well as looking relative to where this script
#                       was run.
#
# 0.2.0  2000-11-15   	Added usage, platform dependent audio player. Added
#			checks for banner program.  Now allowing the audio
#                       files to be in two different locations.
#
## 0.1.0  2000-10-04	Added version number.

if [ $# = 0 ]; then
  (
    echo
    echo "Usage: calibrate_cue <cue name>"
    echo "Usage: calibrate_cue -path <cue name>"
    echo
  ) >&2
  exit 1
fi

show_path=0

if [ $# = 2 ]; then
  if [ "$1" = "-path" ]; then
    show_path=1
    cue_name=$2
  else
    echo "Unknown option ${cue_name}" >&2
    exit 1
  fi
else
  cue_name=$1
fi


if [ $show_path = 0 ]; then
  #
  # pick an audio player command for the platform
  #

  uname_out=`uname`
  if [ `expr $uname_out : 'IRIX'` = 4 ]; then
    audio_player=sfplay
  elif [ "$uname_out" = "Linux" ]; then
# adjust the -v parameter to get the volume you want
    audio_player="aplay"
  else
    echo "Unknown platform."
    exit 1
  fi

  #
  # print the cue in the terminal window, use the banner command if it exists 
  # otherwise do the next best thing.
  #

  banner_path=`which banner`

  if [ "$banner_path" != "" ]; then
    banner ${cue_name}
  else
    echo ""
    echo ""
    echo ""
    echo "\t\t\t${cue_name}"
    echo ""
    echo ""
    echo ""
  fi

  #
  # use the X bell
  #

  echo "\007"
fi

#
# play the audio file.  allow for the sound files to be in one of two locations.
#
# note that under Linux only the user that has logged in has access to the
# audio device.  if you run this command after doing a "su" to a different user
# you will not be able to play audio files.  the x bell will still work however.
#

exe_dir=`dirname $0`

if [ "$GIANT_DIR" = "" ]; then
  GIANT_DIR=$HOME/Documents/giant
fi

if [ "$GIANT_DATA_DIR" = "" ]; then
  GIANT_DATA_DIR=$GIANT_DIR/workspace/default
fi

name=${cue_name}_sound

for audiofile_path in \
    "$GIANT_DATA_DIR/audio/calibrate/$name.wav" \
    "$GIANT_DATA_DIR/audio/calibrate/$name" \
    "$GIANT_DATA_DIR/audio/$name.wav" \
    "$GIANT_DATA_DIR/audio/$name" \
         "$GIANT_DIR/audio/calibrate/$name.wav" \
         "$GIANT_DIR/audio/$name.wav" \
    "$exe_dir/../share/audio/calibrate/$name.wav"; do
  if [ -f "$audiofile_path" ]; then
    # if you need to find out why the audio command isn't working,
    # take off the >/dev/null and 2>/dev/null from the end of the command
    # so that any errors that are occuring will be displayed.
    if [ $show_path = 1 ]; then
      echo "$audiofile_path"
      exit 0
    fi
    "$audio_player" "$audiofile_path" >/dev/null 2>/dev/null
    exit 0
  fi
done

echo "Could not find sound file for cue $cue_name" >&2

if [ $show_path = 1 ]; then
  exit 1
fi

exit 0
