#!/bin/sh

filename="$1"
common_prop_file=/exports/csce/eddie/geos/groups/LURG/models/PLUM/output/common_properties
output_dir=/exports/csce/eddie/geos/groups/LURG/models/PLUM/output

if [[ $* == *-O* ]]; then overwrite=1
else overwrite=0
fi

if [ "$#" -lt 1  ]; then
   echo "Need to specify scenario table file"
   exit
fi

echo "Generating scenarios into $output_dir"

while read -r datarow
do
  IFS=',' read -r -a array <<< "$datarow"

  if [ -z "$header" ]; then
     header=( "${array[@]}" )
  else
     scenario="${array[0]}"
     scenario_dir=$output_dir/$scenario
     echo "$scenario in $scenario_dir"
     if [ ! -d "$scenario_dir" ]; then
        mkdir $scenario_dir
     elif [ $overwrite -ne 1 ]; then
        echo "Already have scenario $scenario, set -O to overwrite existing scenarios"
        continue;
     fi

     config_file=$scenario_dir/config.properties
     cp $common_prop_file $config_file
     echo "OUTPUT_DIR=$scenario_dir" >> $config_file

     for index in "${!array[@]}"
     do
        echo "${header[index]}"="${array[index]}" >> $config_file
     done

     echo "$scenario generated"
  fi
done < $filename

