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

############################################
# Main processing
############################################
projectDirectory="$PWD"
graphXmlPath="${projectDirectory}/SBAS/snap_sbas_wf/back_geoc_sbas.xml"
oaDirectory="${projectDirectory}/SBAS/snap_prepro/spl_oa"
supmaster="$(cat ${projectDirectory}/SBAS/snap_sbas_wf/superior_master.txt)"
supmstpath="${projectDirectory}/SBAS/snap_prepro/superior_master/$supmaster.dim"
dbDirectory="${projectDirectory}/SBAS/snap_prepro/stack_deb"

#this command call is normally generated by the gpt script from SNAP installation only the s1tbx extra luster was added
callgpt="java -Dinstall4j.jvmDir=/usr/lib/jvm/java-8-openjdk-amd64/jre -Dexe4j.moduleName=/home/thho/snap2/snap-desktop/snap-application/target/snap/bin/gpt -Dsnap.mainClass=org.esa.snap.core.gpf.main.GPT -Dsnap.home=/home/thho/snap2/snap-desktop/snap-application/target/snap/bin//.. -Dsnap.extraClusters=/home/thho/snap2/s1tbx/s1tbx-kit/target/netbeans_clusters/s1tbx -Djava.awt.headless=true -Dinstall4j.noLoggingFix=true -Dinstall4j.launcherId=781 -Dinstall4j.swt=false -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4j.vpt=true -classpath /home/thho/snap2/snap-desktop/snap-application/target/snap/.install4j/i4jruntime.jar:/home/thho/snap2/snap-desktop/snap-application/target/snap/snap/modules/ext/org.esa.snap.snap-core/org-esa-snap/snap-runtime.jar org.esa.snap.runtime.Launcher"

cd "${projectDirectory}/SBAS/snap_sbas_wf"

for C in $(awk -F, '{print $1 "_" $2}' $projectDirectory"/SBAS/snap_sbas_wf/mst_slv_info.csv"); do
  mst="$oaDirectory/${C:0:8}.dim"
  slv="$oaDirectory/${C:9:8}.dim"
  fileList="${supmstpath},${mst},${slv}"
  targetFile="$dbDirectory/${C:0:8}_${C:9:8}.dim"
  echo "fileList=$fileList">back_geoc_sbas.properties
  echo "file=$targetFile">>back_geoc_sbas.properties
  parameterFilePath="$projectDirectory/SBAS/snap_sbas_wf/back_geoc_sbas.properties"
  #${gptPath} ${graphXmlPath} -e -p ${parameterFilePath}
  ${callgpt} ${graphXmlPath} -e -p ${parameterFilePath}
done
