#!/bin/bash
# enable next line for debugging purpose
# set -x 

############################################
# User Configuration
############################################

# adapt this path to your needs

#export PATH=~/shared/Training/IMAGE2017:$PATH
#gptPath="~/usr/local/snap6/bin/gpt"
export PATH=~/progs/snap/bin:$PATH
gptPath="/usr/local/snap6/bin/gpt"
#gptPath="gpt"


############################################
# Command line handling
############################################

# first parameter is a path to the graph xml
graphXmlPath="$1"
#graphXmlPath="/home/rus/Desktop/shared/Training/IMAGE2017/split/sp.xml"

#second parameter is a path to a parameter file
#parameterFilePath="$2"



# use third parameter for path to source products
sourceDirectory="$2"
#sourceDirectory="/home/rus/Desktop/shared/Training/IMAGE2017/"

# use fourth parameter for path to target products
targetDirectory="$3"
#targetDirectory="/home/rus/Desktop/shared/Training/IMAGE2017/out"

# the fifth parameter is a file prefix for the target product name, typically indicating the type of processing
targetFilePrefix="$4"

#targetFilePrefix="split"

   
############################################
# Helper functions
############################################
removeExtension() {
    file="$1"
    echo "$(echo "$file" | sed -r 's/\.[^\.]*$//')"
}


############################################
# Main processing
############################################

# Create the target directory
mkdir -p "${targetDirectory}"

# the d option limits the elemeents to loop over to directories. Remove it, if you want to use files.
for F in $(ls -1d "${sourceDirectory}"/S1*.zip); do
   sourceFile="$(realpath "$F")"
  echo ${sourceFile}
  targetFile="${targetDirectory}/${targetFilePrefix}_$(removeExtension "$(basename ${F})").dim"
 echo "Executing :" "{gptPath{graphXmlPath} -e -t targetFile{sourceFile}"
#echo "==============="
#gptPath{graphXmlPath} -e -p parameterFilePath-t{targetFile} sourceFile{gptPath} graphXmlPath-e-t{targetFile} ${sourceFile}
${gptPath} ${graphXmlPath} -e -t ${targetFile} ${sourceFile}
done
