#! /bin/tcsh -f
#
# [Gromov, MPI-C Mainz, 2010-2015]
#
# script to perform an extending budgeting of MECCA kinetic chemistry mechanism
#

# imported from xtag, otherwise defaults
if (! ${?cfgdir}  ) set cfgdir    = "./cfg"              # directory wuth configurations
if (! ${?bindir}  ) set bindir    = "../../../../../bin" # binaries location
if (! ${?toolsdir}) set toolsdir  = "../../../../tools"  # tools location
if (! ${?fpc_call}) set fpc_call  = "fpc -l -viwnh -B"   # fpc compiler call

echo ""
echo "[ XEMBUD: script to budget (tagged) MECCA kinetic chemistry mechanism ]"
echo ""

# checking if embudget tool is available
source ./xchecktool embudget
if ("$?" != "0") exit 1

# --------------------------------------------------------------------------
# SELECTION OF DESIRED CONFIGURATION(S)
# --------------------------------------------------------------------------

# invoking configuration selection script
source ./xselectcfg emb\*.cfg
if ("$?" != "0") exit 1

# --------------------------------------------------------------------------
# BUDGETING
# --------------------------------------------------------------------------

#./embudget.exe mecca.spc:mecca.eqn gas.tex ${cfglist} >&! embudget.log
 ./embudget.exe mecca.spc:mecca.eqn ${chempropfile}:${meccadir}/${xmecca_gastblfile} ${cfglist} >&! embudget.log
set exitstatus = "${status}"

echo "exit status from embudget is: ${exitstatus}"
echo ""

echo "----- list of warnings: -----"
grep '<!>' embudget.log | grep 'warn' | sort -b | uniq
echo ""

echo "----- list of errors: -----"
grep '<!>' embudget.log | grep 'err' | sort -b | uniq
echo ""

if ( "${exitstatus}" != "0" ) then
  echo "please, see the embudget.log file"
  exit ${exitstatus}
endif

# cleanup
rm imcom.tmp

# updating MECCA files
cp messy_mecca_tag_embudget.eqn ../mecca.eqn
cp messy_mecca_tag_embudget.spc ../mecca.spc

# --------------------------------------------------------------------------
# ALL DONE
# --------------------------------------------------------------------------

unsetenv ${cfglist}
exit 0
