#!/bin/bash

#Handler script for IRIS output pipes. Used by IRIS to convert IRIS RAW file to OdimHDF5.
#Calls raw2pb and pb2odimhdf5 converters one by one to generate OdimHDF5 output.

i=0
for arg; do
    argNum=0
    if [[ "$arg" == --ip* ]]
    then
        argNum=$((i + 1))
        input=${!argNum}
    elif [[ "$arg" == --op* ]]
    then
        argNum=$((i + 1))
        output=${!argNum}
    fi
    i=$((i + 1))
done

#Create temp directory if not already present
mkdir -p $(echo $IRIS_TEMP)raw2odimhdf5

#Check the return code of the raw2pb to determine if the last raw file is converted
$(echo $IRIS_PIPES)raw2pb $(echo "$input --op=$(echo $IRIS_TEMP)raw2odimhdf5/raw2odimhdf5_temp") &>> $(echo $IRIS_LOG)raw2odimhdf5.log
status=$?
if [ $status -eq 0 ];
then
    # Last raw file was detected, run the forward converters
    $(echo $IRIS_PIPES)pb2odimhdf5 $(echo "--ip=$(echo $IRIS_TEMP)raw2odimhdf5/raw2odimhdf5_temp $output") &>> $(echo $IRIS_LOG)raw2odimhdf5.log
    rm $(echo $IRIS_TEMP)raw2odimhdf5/raw2odimhdf5_temp &>> $(echo $IRIS_LOG)raw2odimhdf5.log
    exit 0
fi
exit 3