#!/bin/sh

headerLinesPerImage=8

function printUsage {
    echo "Usage fixDAQImages <options> -f <file>"
    echo "  file = base name of the <file>.header and <file>.images files"
    echo "         both .header and .images files must exist!"
    echo "  Options:"
    echo "    -h print this help information"
    echo "    -d print debug information"
}

debug=0
baseFileName=""

while getopts ":df:h" opt; do
    case $opt in
	d)
	    debug=1
	    ;;
	f)
	    baseFileName=$OPTARG
	    ;;
	h)
	    printUsage
	    exit 0
	    ;;
	\?)
	    echo "Invalid option: -$OPTARG"
	    ;;
	:)
	    echo "Option -$OPTARG requires an argument."
	    exit -1
	    ;;
    esac
done

if [ "$baseFileName" == "" ]; then
    printUsage
    exit -1
fi

if [ "$debug" -eq "1" ]; then
    echo "Base file name is \"$baseFileName\""
fi

headerFile=$baseFileName.header
imagesFile=$baseFileName.images

if [ ! -e $headerFile ]; then
  echo "=== ERROR ===================================================="
  echo "File \"$headerFile\" does not exist."
  echo "=============================================================="
  exit -1
fi

if [ ! -e $imagesFile ]; then
  echo "=== ERROR ===================================================="
  echo "File \"$headerFile\" does not exist."
  echo "=============================================================="
  exit -1
fi

numImages=`grep Sequence $headerFile | wc -l | xargs -ikey expr key`


if [ "$debug" -eq "1" ]; then
    echo "Found $numImages images."
fi

imageSizeX=`head -7 $headerFile | tail -1 | cut -d" " -f 2`
imageSizeY=`head -7 $headerFile | tail -1 | cut -d" " -f 1`
imageSizeBytes=`expr $imageSizeX \* $imageSizeY \* 2`

temporaryImage="./image"

expectedBulkSize=`expr $imageSizeBytes \* $numImages`
bulkSize=`stat -c%s $imagesFile`

if [ "$expectedBulkSize" -ne "$bulkSize" ]; then
  echo "=== ERROR ===================================================="
  echo "Detected discrepancy between size of \"$imagesFile\" file and"
  echo "image X and Y sizes declared in header file."
  echo "File \"$imagesFile\" has $bulkSize bytes."
  echo "Expected $imageSizeBytes bytes."
  echo ""
  echo "Please check files... exiting..."
  echo "=============================================================="
  exit -1
fi

for i in $(eval echo "{1..$numImages}"); do
  startLine=`expr $headerLinesPerImage \* $i`
  pulseid=`head -n $startLine $headerFile | tail -$headerLinesPerImage| grep PULSEID | cut -d":" -f2 | sed "s/^[ ]//g"`

  imageFileName=`echo $headerFile | cut -d"-" -f1,2,3,4,5,6`-$pulseid.pgm

  if [ "$debug" -eq "1" ]; then
      echo Extracting $imageFileName...
  fi

  head -n `expr $headerLinesPerImage \* $i` $headerFile | tail -$headerLinesPerImage > $imageFileName

  dd ibs=$imageSizeBytes obs=$imageSizeBytes count=1 skip=`expr $i - 1` if=$imagesFile of=$temporaryImage 2> /dev/null

  if [ "$?" -ne "0" ]; then
      echo "=== ERROR ===================================================="
      echo "Failed to extract image $i from $imagesFile file."
      echo "PULSEID: $pulseid"
      echo "=============================================================="
      exit -1
  fi

  cat $temporaryImage >> $imageFileName
done

\rm $temporaryImage
