#!/bin/bash
MY_PATH="`dirname \"$0\"`"              # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`"  # absolutized and normalized
if [ -z "$MY_PATH" ] ; then
  # error; for some reason, the path is not accessible
  # to the script (e.g. permissions re-evaled after suid)
  exit 1  # fail
fi

function usage {
 echo "Usage : `basename ${0}` -exec executable -i geometry [-size \"X.YYY1 X.YYY2 ...\"]"
 echo " check if meshes geometryX.YYY1.msh geometryX.YYY2.msh exist"
 echo " call executable for mesh geometryX.YYY1.msh geometryX.YYY2.msh ..."
 exit 0
}

if [[ $# -eq 0 ]] ; then
usage
fi

while [[ $# -gt 0 ]] && [[ ."$1" = .-* ]] ;
do
    opt="$1";
    shift;              #expose next argument
    case "$opt" in
        "-exec" )
           EXFILE=${1}; shift;;
        "-exec="* )     # alternate format: -i=argdat
           EXFILE="${opt#*=}" ;;
        "-i" )
           FILE=${1}; shift;;
        "-i="* )     # alternate format: -i=argdat
           FILE="${opt#*=}" ;;  
        "-size" )
           SIZE="$1"; shift;;
        "-size="* )
           SIZE="${opt#*=}";;
        *) echo >&2 "Invalid option: $@"; exit 1;;
   esac
done
if [ -z ${SIZE+x} ];
then
. sizelist.conf
else
  sizelist=${SIZE}
fi

if [ -z ${sizelist+x} ];
then
. ${MY_PATH}/sizelist.conf
fi

#check for meshes
${MY_PATH}/make_meshes.sh -i ${FILE}.geo -size "${sizelist}"

for size in ${sizelist}
do
echo Processing size ${size}...
${MY_PATH}/compute.sh -exec ${EXFILE} -i ${FILE} -size ${size}
done

