#!/bin/bash
#===========================================
#== SCRIPT TO RUN CU-GECKO-IGOR EXTRACTOR ==
#==  FROM DIRECTORY <HOME>/CU_EXTRACTOR   ==
#==   OUTPUT TO DIR <HOME>/BOXMOD_RUNS    ==
#==(unless otherwise specified in setup file)
#===========================================

#defaults 
inputfile="ncplot_input_module.f90"

#===========================================
#== SET UP INPUT FILES==
#===========================================
while getopts "f:k:m:r:" opt
do
  case "$opt" in
    f ) inputfile=$OPTARG ;;
    r ) runname=$OPTARG ;;  
# dir paths, obtainable from setup_extractor.dat
#    b ) boxmod_source=$OPTARG ;;
#    h ) home_dir=$OPTARG ;;
#    w ) =$OPTARG ;;
  esac
done

# verify input arguments
if [ $# -lt 1 ]; then
    echo We need at least 1 input argument: -r runname
    echo .... argument -f inputfile is optional
    exit
fi

#-------------------
extractor_input="INPUTS/"${inputfile}
#mech_name=${mechname}
#initfile=${keyfile}
#runname=$mechname"_"$keyfile

#-------------------
# verify inputs
echo " "
echo "Arguments: -r runname (-f inputfile)"
echo ".... inputfile = "$inputfile
#echo ".... keyfile = "$initfile 
#echo ".... mechname = "$mechname 
echo ".... runname = "$runname

#===========================================
#== SET UP GECKO VERSION AND PATHS==
#===========================================
# ... IF SET HERE
#home_dir="/home/julial/GECKO/GIT_COPY"
#gecko_version="GECKO-A"
#run_dir="GECKO_SCRATCH"

# ... IF READ IN FROM file "setup_extractor.dat"
if [ ! -e "setup_extractor.dat" ] ; then
    echo error, extractor setup file could not be found
    echo file paths must be set in script
    exit
fi
home_dir=`      grep "home_dir"      setup_extractor.dat | awk '{print $3}' `
gecko_version=` grep "gecko_version" setup_extractor.dat | awk '{print $3}' `
gecko_inp_dir=` grep "gecko_inp_dir" setup_extractor.dat | awk '{print $3}' `
scratch_dir=`   grep "scratch_dir"   setup_extractor.dat | awk '{print $3}' `
gecko_run_dir=` grep "gecko_run_dir" setup_extractor.dat | awk '{print $3}' `
boxmod_version=` grep "boxmod_version" setup_extractor.dat | awk '{print $3}' `
boxmod_run_dir=` grep "boxmod_run_dir" setup_extractor.dat | awk '{print $3}' `
extractor_version=` grep "extractor_version" setup_extractor.dat | awk '{print $3}' `
extractor_run_dir=` grep "extractor_run_dir" setup_extractor.dat | awk '{print $3}' `

#--construct paths and report to screen.
boxmod_outdir=$scratch_dir/$boxmod_run_dir
extractor_source=$home_dir/$extractor_version
extractor_outdir=$scratch_dir/$extractor_run_dir
gecko_source=$home_dir/$gecko_version

boxmod_workingdir=${boxmod_outdir}/${runname}
echo $boxmod_workingdir
if [ ! -e ${boxmod_workingdir} ] ; then
    echo error, boxmod run directory could not be found
    echo ${boxmod_workingdir}
    echo check boxmod_outdir, runname argument
    echo in $0
    exit
fi

extractor_workingdir=$extractor_outdir/${runname}
echo Creating ${extractor_workingdir}...
if [ -e ${extractor_workingdir} ] ; then
  echo WARNING! directory already exists !
else
  mkdir -p ${extractor_workingdir}
fi

if [ -e "${extractor_source}/${extractor_input}" ] ; then
  echo linking custom extraction options ${extractor_source}/${extractor_input}
  ln -s ${extractor_source}/${extractor_input} ${extractor_workingdir}/INPUTS/ncplot_input_module.f90
  echo ... to ${extractor_workingdir}/INPUTS/ncplot_input_module.f90
else
    echo error, extractor inputs file could not be found
    echo check filename and try again
    exit
fi

echo linking boxmod results file ${boxmod_workingdir}/outdat.nc

if [ -e ${boxmod_workingdir}/outdat.nc ] ; then
  ln -s ${boxmod_workingdir}/outdat.nc ${extractor_workingdir}/outdat.nc
  echo ... to ${extractor_workingdir}/outdat.nc
else
  echo FILE NOT FOUND
  exit 99
fi

echo compiling extractor...
mkdir -p  ${extractor_workingdir}/EXTRACTOR_WKDIR/OBJ
mkdir -p  ${extractor_workingdir}/EXTRACTOR_WKDIR/RUN
cp -r $extractor_source/INPUTS $extractor_source/LIB ${extractor_workingdir}/EXTRACTOR_WKDIR/
cp $extractor_source/OBJ/Makefile $extractor_source/OBJ/template.mk ${extractor_workingdir}/EXTRACTOR_WKDIR/OBJ

# compile the gecko code just in case geckolib.a does not exist
( cd $gecko_source && make cm)
if [ ! -e $gecko_source/OBJ/geckolib.a ] ; then
  echo cannot find $gecko_source/OBJ/geckolib.a needed to compile the extractor
  exit
fi
cp  $gecko_source/OBJ/geckolib.a ${extractor_workingdir}/EXTRACTOR_WKDIR/OBJ

cd ${extractor_workingdir}/EXTRACTOR_WKDIR/OBJ
make ../RUN/ncplot

echo linking extractor executable ${extractor_workingdir}/EXTRACTOR_WKDIR/RUN/ncplot
ln -s ${extractor_workingdir}/EXTRACTOR_WKDIR/RUN/ncplot ${extractor_workingdir}/ncplot
echo ... to ${extractor_workingdir}/extractor

echo running extractor
cd ${extractor_workingdir}
./ncplot

cp post.ascii ${extractor_source}/${runname}.ascii

#--tidy up
rm -R EXTRACTOR_WKDIR
# remove symbolic links
find -lname '*' -delete
