#!/bin/bash

function usage
{
    echo "usage:  _bior_annotate_blaster_chunker  <vcfIn>  <startDataLine>  <endDataLine>  <vcfOutPartFileName>  [biorAnnotateConfigFile]"
    exit 0;
}

if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
    usage
fi

### NOTE: This script should usually only be called by bior_annotate_blaster
vcfIn="$1"
start="$2"
end="$3"
vcfOutPartFileName="$4"
configFile="$5"
configOpt=""
if [ -n "$configFile" ] ; then
  configOpt="-c $configFile"
fi

if [ -z "$vcfIn" ] || [ -z "$start" ] || [ -z "$end" ] || [ -z "$vcfOutPartFileName" ] ; then
  usage
fi

### Example file type that "cat" would use: ASCII text  (will have to see if it *contains* this string)
### If uname contains "Darwin", then we're on a Mac, so use gzcat instead of zcat (which demands .Z extensions)
### NOTE: Make sure to use the -L flag to dereference any symbolic links
fileType=$(file -L "$vcfIn")
parser=zcat
if [[ "$fileType" == *ASCII* ]] ; then
    parser=cat
elif [[ "$(uname)" == "Darwin" ]]; then
    parser=gzcat
fi

statusFile=${vcfOutPartFileName}.status
$parser  "$vcfIn"  |  bior_chunk  -s "$start"  -e "$end" --log |  bior_annotate $configOpt  -s "$statusFile" --log | gzip -c > $vcfOutPartFileName

### -----
### Example lines in status file:
###   numLinesIn=60
###   numLinesOut=60
###   numLinesBadData=0
###   isSuccessful=true
### -----
linesIn=`grep numLinesIn $statusFile | sed 's/numLinesIn=//'`
linesOut=`grep numLinesOut $statusFile | sed 's/numLinesOut=//'`
badLines=`grep numLinesBadData $statusFile | sed 's/numLinesBadData=//'`
isSuccessful=`grep isSuccessful $statusFile | sed 's/isSuccessful=//'`

### Error if the status file does not exist or if isSuccessful=false
if [ ! -f "$statusFile" ] || [ "$isSuccessful" != "true" ] ; then
  echo "FAILED:  annotation did not complete successfully."
  touch "$vcfOutPartFileName.done.FAILED"
elif [ $linesIn -ne $linesOut ] ; then
  echo "WARNING:  Lines out does NOT equal lines in!  File: $vcfOutPartFileName"
  echo "Lines in:  $linesIn"
  echo "Lines out: $linesOut"
  touch "$vcfOutPartFileName.done.WARNING"
else
  touch "$vcfOutPartFileName.done"
fi
