#!/bin/sh

# 01/25/2019 - Handle the case of no optical data.
# 11/27/2018 - Fixed some problems when -mkprj wasn't used.
# 04/14/2016 - Better handling of projects with spaces in the name.
# 11/??/2003 - Added remote tracking
# ??/??/2003 - Added back the reversing of rawdata, etc.  so that
#              realtime doesn't have to wait for it to finish.
# 04/15/2002 - Updated to handle processing ranges
#              Removed code for reversing rawdata, etc.
#              This is now handled by process.tcl

overwrite=0
input_map=
reverse=0
notrack=0
prjname=
motion=
host=
local=0
capturedir=$GIANT_DATA_DIR/capture

args=$*

while [ $# -gt 0 ]; do
  if [ "$1" = "-mkprj" ]; then
    prj=$2
    prjname=`basename "$prj" .prj`
    if [ "x`basename $prj`" = "x$prj" ]; then
      capturedir=$GIANT_DATA_DIR/capture
    else
      capturedir=`dirname "$prj"`
    fi
    shift 2
  elif [ "$1" = "-notrack" ]; then
    options="$options -n"
    notrack=1
    shift
  elif [ "$1" = "-thorough" ]; then
    options="$options -feedback"
    shift
  elif [ "$1" = "-overwrite" ]; then
    overwrite=1
    shift
  elif [ "$1" = "-start" ]; then
    start_frame=$2
    shift 2
  elif [ "$1" = "-end" ]; then
    end_frame=$2
    shift 2
  elif [ "$1" = "-start_id" ]; then
    start_id=$2
    options="$options -start_id $start_id"
    shift 2
  elif [ "$1" = "-reverse" ]; then
    reverse=1
    options="$options -reverse_ids"
    shift
  elif [ "$1" = "-input_map" ]; then
    input_map=$2
    shift 2
  elif [ "$1" = "-port" ]; then
    options="$options -port $2"
    shift 2
  elif [ "$1" = "-host" ]; then
    host=$2
    shift 2
  elif [ "$1" = "-local" ]; then
    local=1
    shift 1
  else
    break
  fi
done

if [ x"$prjname" != x"" ]; then
  prjdir=$capturedir/$prjname
fi

if [ x"$host" != x -a $local != 1 ]; then
  rsh -n "$host" env BIO_DIR=$BIO_DIR GIANT_DATA_DIR=$GIANT_DATA_DIR BIO_RTTRACK_DIR=$BIO_RTTRACK_DIR NEW_BIO_DIR=$NEW_BIO_DIR PATH=$PATH start_rttrack_process -local $args
  code=$?
  rm $prjdir/process_status
  exit $code
fi

if [ x"$prjdir" != x ]; then
  options="$options -status '$prjdir/process_status'"
fi

if [ "$prjname" != "" ]; then
  rawdata=$prjdir/$prjname.raw
  glob_ids=$prjdir/$prjname.gid
  options="$options -mkprj '$prj'"
  if [ $notrack = 0 ]; then
    motion=$prjdir/$prjname.bmo
    options="$options -w '$motion'"
  fi
fi

if [ $# = 0 ]; then
  glob_data=""
else
  glob_data=$1
fi


# Normal operation
cd "$BIO_RTTRACK_DIR"

#Read the acquire host from the acquirehosts file.
if [ "$NEW_BIO_DIR" != "" ]; then
  acquirehosts=$GIANT_DATA_DIR/default/acquirehosts.dat
else
  acquirehosts=$GIANT_DATA_DIR/DEFAULTS/acquirehosts.dat
fi
if [ "$overwrite" = 1 ]; then
  rm -f "$glob_ids"
fi

if [ "$start_frame" != "" ]; then
  options="$options -i `expr $start_frame - 1`"
else
  start_frame=1
fi

if [ "$end_frame" != "" ]; then
  nframes=`expr $end_frame - $start_frame + 1`
  options="$options -exit $nframes"
fi

echo "Tracking $glob_data"

if [ "$input_map" != "" ]; then
  options="$options -input_map '$input_map'"
fi

if [ "$prjname" != "" ]; then
  stream=$prjdir/$prjname.str
else
  stream=$BIO_RTTRACK_DIR/stream_data.dat
fi

temp_drift=""

if [ "$prjname" != "" ]; then
  prj_drift=$prjdir/$prjname.drift
  if [ -f "$prj_drift" ]; then
    temp_drift=$BIO_RTTRACK_DIR/drift_data.dat
    cp "$prj_drift" "$temp_drift" || exit 1
    echo "Copied $prj_drift to $temp_drift" >&2
  else
    echo "Missing $prj_drift" >&2
  fi
fi

if [ -f "$stream" ]; then
  options="$options -input_str $stream"
fi

if [ "$prjname" != "" ]; then
  imu=$prjdir/$prjname.imu
else
  imu=$BIO_RTTRACK_DIR/imu_data.dat
fi

if [ -f "$imu" ]; then
  options="$options -input_imu $imu"
fi

if [ $local = 0 ]; then
  options="$options -r"
fi

if [ "$prjdir" != "" ]; then
  echo "on `hostname`" >"$prjdir/process_status"
  trap "rm '$prjdir/process_status'" EXIT
fi


options="$options -pred_weight 1"

if [ "$glob_data" != "" ]; then
  eval echo rttrack "$options" "'$glob_data'" >&2 || exit 1
  eval rttrack "$options" "'$glob_data'" || exit 1
else
  eval echo rttrack "$options" >&2 || exit 1
  eval rttrack "$options" || exit 1
fi


if [ "$prjname" != "" ]; then
  rawdata=$prjdir/$prjname.raw
  glob_ids=$prjdir/$prjname.gid

  fixrdframecount "$rawdata" || exit 1
fi

if [ "$motion" != "" ]; then
  fixmotionfieldcount "$motion" || exit 1
fi

if [ $reverse = 1 ]; then
  echo "reversing" > $prjdir/process_status
  echo "Reversing raw data" >&2
  reverserawdata -new_end $start_id $rawdata $rawdata.reverse || exit 1
  mv $rawdata.reverse $rawdata || exit 1
  if [ "$motion" != "" ]; then
    echo "Reversing motion" >&2
    reversemotion -new_end $start_id "$motion" "$motion.reverse" || exit 1
    mv "$motion.reverse" $motion || exit 1
  fi
  echo "Reversing glob ids" >&2
  reversegids -new_end $start_id $glob_ids $glob_ids.reverse || exit 1
  mv $glob_ids.reverse $glob_ids || exit 1
fi

if [ "$temp_drift" != "" ]; then
  rm "$temp_drift"
fi

echo "start_rttrack_process finished"
