##############################################################
#
# PURPOSE: Convert binary files generated by the flight code into
#          Matlab-readable ASCII files.  ABBREVIATED VERSION
# AUTHOR:  rsm
# DATE:    99/12/20
# NOTES:   See the companion Matlab script loadData.m
#          00/1/11: The -a option, which supposedly checks file existance
#                   doesn't appear to work.  -f determines regularity.
#
#
##############################################################
#
  if [ -f simulation.log -a -s simulation.log ]
  then
    echo "Converting simulation.log"
    #
    # If an argument exists, skip that many records.
    #
    if [ $1 ]
    then
      logToMatlab -skip $1 simulation.log > dataS.m
    else
      logToMatlab simulation.log > dataS.m
    fi
  fi

  if [ -f navigation.log -a -s navigation.log ]
  then
    echo "Converting navigation.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 navigation.log > dataN.m
    else
      logToMatlab navigation.log > dataN.m
    fi
  fi

  if [ -f dynamicControl.log -a -s dynamicControl.log ]
  then
    echo "Converting dynamicControl.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 dynamicControl.log > dataD.m
    else
      logToMatlab dynamicControl.log > dataD.m
    fi
  fi

  if [ -f ctdlog.log -a -s ctdlog.log ]
  then
    echo "Converting ctdlog.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 ctdlog.log > dataCTD.m
    else
      logToMatlab ctdlog.log > dataCTD.m
    fi
  fi

  if [ -f ctdDriver.log -a -s ctdDriver.log ]
  then
    echo "Converting ctdDriver.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 ctdDriver.log > dataCTD.m
    else
      logToMatlab ctdDriver.log > dataCTD.m
    fi
  fi

  if [ -f ctdDriver2.log -a -s ctdDriver2.log ]
  then
    echo "Converting ctdDriver2.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 ctdDriver2.log > dataCTD2.m
    else
      logToMatlab ctdDriver2.log > dataCTD2.m
    fi
  fi

  if [ -f seabird.log -a -s seabird.log ]
  then
    echo "Converting seabird.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 seabird.log > dataCTD2.m
    else
      logToMatlab seabird.log > dataCTD2.m
    fi
  fi

  if [ -f seabird1.log -a -s seabird1.log ]
  then
    echo "Converting seabird1.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 seabird1.log > dataCTD1.m
    else
      logToMatlab seabird1.log > dataCTD1.m
    fi
  fi

  if [ -f seabird2.log -a -s seabird2.log ]
  then
    echo "Converting seabird2.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 seabird2.log > dataCTD2.m
    else
      logToMatlab seabird2.log > dataCTD2.m
    fi
  fi

  if [ -f tailCone.log -a -s tailCone.log ]
  then
    echo "Converting tailCone.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 tailCone.log > dataTailCone.m
    else
      logToMatlab tailCone.log > dataTailCone.m
    fi
  fi

  if [ -f bluefinBattlog.log -a -s bluefinBattlog.log ]
  then
    echo "Converting bluefinBattlog.log; file name is dataBFL.m"
    if [ $1 ]
    then
      logToMatlab -skip $1 bluefinBattlog.log > dataBFL.m
    else
      logToMatlab bluefinBattlog.log > dataBFL.m
    fi
  fi

  if [ -f metrabytelog.log -a -s metrabytelog.log ]
  then
    echo "Converting metrabytelog.log; file name is dataMBL.m"
    if [ $1 ]
    then
      logToMatlab -skip $1 metrabytelog.log > dataMBL.m
    else
      logToMatlab metrabytelog.log > dataMBL.m
    fi
  fi

  if [ -f PS8000.log -a -s PS8000.log ]
  then
    echo "Converting PS8000.log; file name is dataPS8.m"
    if [ $1 ]
    then
      logToMatlab -skip $1 PS8000.log > dataPS8.m
    else
      logToMatlab PS8000.log > dataPS8.m
    fi
  fi

  if [ -f parosci.log -a -s parosci.log ]
  then
    echo "Converting parosci.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 parosci.log > dataPS.m
    else
      logToMatlab parosci.log > dataPS.m
    fi
  fi

  if [ -f psa916log.log -a -s psa916log.log ]
  then
    echo "Converting psa916log.log"
    if [ $1 ]
    then
      logToMatlab -skip $1 psa916log.log > dataPSA.m
    else
      logToMatlab psa916log.log > dataPSA.m
    fi
  fi

  if [ -f gps.log -a -s gps.log ]
  then
   echo "Converting gps.log"
   logToMatlab gps.log > dataGPS.m
  fi

  if [ -f Env.log -a -s Env.log ]
  then
   echo "Converting Env.log"
   logToMatlab Env.log > dataE.m
  fi
#
# Again skip for brevity.  The important stuff is passed through
# to Navigation anyway.
#
  if [ ]    ###############
  then

  if [ -f kvhInclinometer.log -a -s kvhInclinometer.log ]
  then
    echo "Converting kvhInclinometer"
    if [ $1 ]
    then
      logToMatlab -skip $1 kvhInclinometer.log > dataINC.m
    else
      logToMatlab kvhInclinometer.log > dataINC.m
    fi
  fi

  if [ -f kvhCompass.log -a -s kvhCompass.log ]
  then
    echo "Converting kvhCompass"
    if [ $1 ]
    then
      logToMatlab -skip $1 kvhCompass.log > dataCOM.m
    else
      logToMatlab kvhCompass.log > dataCOM.m
    fi
  fi

  fi      ############# 

  if [ -f crossbow.log -a -s crossbow.log ]
  then
    echo "Converting Crossbow data"
    if [ $1 ]
    then
      let nlines=$1+5
      logToMatlab -skip $nlines crossbow.log > dataCrossB.m
    else
      logToMatlab -skip 5 crossbow.log > dataCrossB.m
    fi
  fi

  if [ -f dvl.log -a -s dvl.log ]
  then
    echo "Converting DVL Data.  The file name is dataDVL.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines dvl.log > dataDVL.m
    else
      logToMatlab dvl.log > dataDVL.m
    fi
  fi

  if [ -f fuelcell.log -a -s fuelcell.log ]
  then
    echo "Converting fuel cell Data.  The file name is dataFuelCell.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines fuelcell.log > dataFuelCell.m
    else
      logToMatlab fuelcell.log > dataFuelCell.m
    fi
  fi

  if [ -f octans.log -a -s octans.log ]
  then
    echo "Converting Octans Data.  The file name is dataOct.m"
    echo "The array name is dataO"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines octans.log > dataOct.m
    else
      logToMatlab octans.log > dataOct.m
    fi
  fi

  if [ -f kearfott.log -a -s kearfott.log ]
  then
    echo "Converting Kearfott Data.  The file name is dataKft.m"
    echo "The array name is dataK"
    if [ $1 ]
    then
      let nlines=$1
      let nlines=nlines*5
      logToMatlab -skip $nlines kearfott.log > dataKft.m
    else
      logToMatlab -skip 5 kearfott.log > dataKft.m
    fi
  fi

#
# Convert Ips4 log file
#
  if [ -f ipslog.log -a -s ipslog.log ]
  then
    echo "Converting Ips4 Data.  The file name is dataIps4.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines ipslog.log > dataIps4.m
    else
      logToMatlab ipslog.log > dataIps4.m
    fi
  fi

#
# Convert Isus log file
#
  if [ -f isuslog.log -a -s isuslog.log ]
  then
    echo "Converting Isus Data.  The file name is dataIsus.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines isuslog.log > dataIsus.m
    else
      logToMatlab isuslog.log > dataIsus.m
    fi
  fi

#
# Convert PA200LR log file
#
  if [ -f PA200LRlog.log -a -s PA200LRlog.log ]
  then
    echo "Converting PA200LR Data.  The file name is dataPA200LR.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines PA200LRlog.log > dataPA200LR.m
    else
      logToMatlab PA200LRlog.log > dataPA200LR.m
    fi
  fi

#
# Convert Buoy Launcher log file
#
  if [ -f buoylauncherlog.log -a -s buoylauncherlog.log ]
  then
    echo "Converting Buoy Launcher Data.  The file name is dataBuoy.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines buoylauncherlog.log > dataBuoy.m
    else
      logToMatlab buoylauncherlog.log > dataBuoy.m
    fi
  fi

#
# Convert biolume log file
#
  if [ -f biolume.log -a -s biolume.log ]
  then
    echo "Converting Biolume Data.  The file name is dataBiolume.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines biolume.log > dataBiolume.m
    else
      logToMatlab biolume.log > dataBiolume.m
    fi
  fi

#
# Convert Hydroscat log file
#
  if [ -f hydroscatlog.log -a -s hydroscatlog.log ]
  then
    echo "Converting HS-2 Data.  The file name is dataHS2.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines hydroscatlog.log > dataHS2.m
    else
      logToMatlab hydroscatlog.log > dataHS2.m
    fi
  fi

#
# Convert LISST-100 log file
#
  if [ -f lisstlog.log -a -s lisstlog.log ]
  then
    echo "Converting LISST-100 Data.  The file name is dataLisst.m"
    if [ $1 ]
    then
      let nlines=$1
      logToMatlab -skip $nlines lisstlog.log > dataLisst.m
    else
      logToMatlab lisstlog.log > dataLisst.m
    fi
  fi

#
# If the mission had waypoints, gobble up syslog and extract 
# waypoint information.
#
  if [ -s syslog ]
  then
    grep wplog syslog > temp.txt
  else
    echo "Syslog is empty."
  exit -1
  fi

  if [ -s temp.txt ]
  then
    echo "Extracting waypoints from syslog."
  else
    exit -1
  fi
  #
  # This gnarly sed command inserts a ' at the start and end of each line.
  #
  cat temp.txt | sed 's/^/'\''/' | sed 's/$/'\''/' > temp1.txt

  echo "dataWPstr = [ ... " > waypoint.m
  cat temp1.txt >> waypoint.m
  echo " ];" >>    waypoint.m

  rm temp.txt
  rm temp1.txt
