#!/bin/sh

current_acquire_host () {
  local acquirehosts

  if [ "$G_CONFIG_DIR" != "" ]; then
    acquirehosts=$G_CONFIG_DIR/acquirehosts.dat
  else
    acquirehosts=$GIANT_DATA_DIR/default/acquirehosts.dat
  fi

  awk -vhostname=`hostname` '
    /CURRENT:/ {
      current=$2
    }
    /ACQUIRE_HOSTS {/ {
      in_acquire_hosts_section=1
      section_hostname=""
      current_host=""
    }
    /HOSTS {/ {
      in_hosts_section=1
    }
    /}/ {
      if (in_hosts_section) {
        in_hosts_section = 0
      }
      else if (in_acquire_hosts_section) {
        if (section_hostname==hostname) {
          current=current_host
        }
        in_acquire_hosts_section = 0
      }
    }
    /HOSTNAME:/ { section_hostname=$2 }
    /CURRENT_HOST:/ {
      current_host=$2
    }
    END {
      print current
    }
  ' <$acquirehosts
}


options=$*
uname_out=`uname`
if [ `expr $uname_out : 'IRIX'` = 4 ]; then
  AWK=nawk
elif [ "$uname_out" = "Linux" ]; then
  AWK=awk
else
  echo "Unknown platform."
  exit 1
fi

cd $BIO_RTTRACK_DIR
rm -f audio_data.wav id_data.dat imu_data.dat stream_data.dat

echo "GIANT_DATA_DIR=$GIANT_DATA_DIR"

# Setting the BIO_ACQUIRE_HOST is no longer needed for versions of
# rttrack after 0.25.2.  This is here for backward compatibility.
export BIO_ACQUIRE_HOST=`current_acquire_host`

echo BIO_ACQUIRE_HOST=$BIO_ACQUIRE_HOST

on_sound=$GIANT_DATA_DIR/audio/realtime_on_sound
off_sound=$GIANT_DATA_DIR/audio/realtime_off_sound

if [ -f $on_sound ]; then
  (sleep 1;play $on_sound)&
fi
rttrack -assignment_accuracy 1 -floor_y 0 $options -r -b
if [ -f $off_sound ]; then
  play $off_sound &
fi
