#! /bin/tcsh -f
#
# [Gromov, MPI-C Mainz, 2007-2009]
#
# script to tag MECCA kinetic chemistry mechanism
#
# parameters:
# ($1) - normal mode (any value) or null-initialise (give "zero")
# ($2) - optional batchfile param from xmecca
#
# steps:
# 0 cleanup to zero
# 1 choose desired configuration(s)
# 2 tag using imtag
# 3 link generated modules to the corresponding dirs

# --------------------------------------------------------------------------
# VARIABLES
# --------------------------------------------------------------------------

# paths are relative
set batchdir     = "../batch"           # batch files location
set meccadir     = ".."                 # where MECCA + equation/species files are located
set f90smcl      = "../smcl"            # where f90-files should go to
set f90box       = "../.."              #                        -- BOX ONLY
set f90si        = "../../../../smil"   #                        -- SI ONLY
set box2tag      = "mecca/tag"          # relative path from box to tag
set smcl2tag     = "../tag"             #         - " -      smcl to tag
set si2tag       = "../mbm/caaba/mecca/tag"     # - " -      si to tag

set bindir       = "../../../../../bin" # binaries location
set toolsdir     = "../../../../tools"  # tools location
set fpc_call     = "fpc -l -viwnh -B"   # fpc compiler call
set do_recompile = "false"              # set to "true" to recompile the executable(s)
set do_kpp       = "false"              # defines if kpp is used as the integrator

# detecting ECHAM5/MESSy
if ( -d ../../../../smil ) then
  set bm3d
endif

# just to initialize to zero configuration and exit
if ( "$1" == "zero" ) goto setzero

# --------------------------------------------------------------------------
# BATCH INFO TO BE READ HERE
# --------------------------------------------------------------------------

if ( "$2" != "" ) then
  # type batchfile with or without extension .bat:
  set batchfile = `basename $2 .bat`.bat
  if ( -f $batchdir/$batchfile ) then
    echo "\nusing batchfile: $batchfile" # | tee -a ../$logfile
    set batch
    source $batchdir/$batchfile
  else
    echo "ERROR: $batchfile does not exist"
    exit 1
  endif
endif

# --------------------------------------------------------------------------
# BEGINNING, LINKING FILES, INITIALIZING CONFs
# --------------------------------------------------------------------------

setzero:

# --------------------------------------------------------------------------
#echo "initializing configuration(s) setup..."

# linking mecca.eqn & mecca.spc to current folder
ln -fs $meccadir/mecca.eqn ./mecca.eqn
ln -fs $meccadir/mecca.spc ./mecca.spc
# tracer definitions file
ln -fs $meccadir/gas.tex ./gas.tex

# cleaning files
rm -f *.log
rm -f messy_mecca_tag*.f90 messy_mecca_tag*.inc messy_mecca_tag*.dot
rm -f messy_mecca_tag*.spc messy_mecca_tag*.eqn messy_mecca_tag*.kpp messy_mecca_tag*.tex
rm -f $f90box/messy_mecca_tag*.f90 $f90box/messy_mecca_tag*.inc
rm -f $f90smcl/messy_mecca_tag*.f90 $f90smcl/messy_mecca_tag*.inc

# default empty "zero" configuration
cp -f messy_mecca_tag_box.f90-zero $f90box/messy_mecca_tag_box.f90
cp -f messy_mecca_tag_parameters.inc-zero messy_mecca_tag_parameters.inc
cd $f90smcl; ln -fs $smcl2tag/messy_mecca_tag_parameters.inc .; cd -
cd $f90box;  ln -fs $box2tag/messy_mecca_tag_parameters.inc .;  cd -

# in ECHAM5/MESSy setup
if (${?bm3d}) then
  rm -f $f90si/messy_mecca_tag*.f90 $f90si/messy_mecca_tag*.inc
  # mz_rs_20100325+
  # file does not exist
  # cp -f messy_mecca_tag_si.f90-zero $f90si/messy_mecca_tag_si.f90
  # mz_rs_20100325-
  cd $f90si; ln -fs $si2tag/messy_mecca_tag_parameters.inc .; cd -
endif

#echo "done"
#echo ""

# just to initialize to zero configuration and exit
if ( "$1" == "zero" ) exit 0

# TAGGING is switched off in CAABA/MECCA
echo ""
echo "Tagging is the optimised version of doubling dedicated for EMAC."
echo "Please rerun xcaaba/xmecca and select doubling for CAABA/MECCA."
echo ""
exit 1

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

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

#echo "Switching off tagging..."
echo ""
echo "[ XTAG: script to tag selected MECCA kinetic chemistry mechanism ]"
echo "  (note: no aerosol-phase chemistry supported so far)"
echo ""

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

# --------------------------------------------------------------------------
# TAGGING
# --------------------------------------------------------------------------

# these params left from older versions, keeping so far
set do_tag = "true"

if ("$do_tag" == "true") then

  # --------------------------------------------------------------------------
  # run imtag - tagging

  echo ">>> tagging with imtag >>>"
  echo ""

  ./imtag.exe mecca.spc mecca.eqn $cfglist | tee -a imtag.log
  set exitstatus = "$status"

  echo "----- list of warnings: -----"
  grep \<\!\> imtag.log
  echo ""

  echo "exit status from imtag is: $exitstatus"
  echo ""
  if ( "$exitstatus" != "0" ) then
    echo "please, see the imtag.log file"
    exit
  endif

  # ----- integration with KPP -------------------------------------------------
  if ("$do_kpp" == "true") then
    source ./xtagkpp
  endif

  # ----- distributiong files to the directories -------------------------------

  # decapitalizing names of all produced files
  set tagfiles = (messy_mecca_tag*)
  foreach tagfile ($tagfiles)
    mv -f $tagfile `echo $tagfile | gawk '{print tolower($0)}'` >&! /dev/null
  end

  # applying changes to the regular mecca.eqn & .spc
  cat messy_mecca_tag.eqn > $meccadir/mecca.eqn
  cat messy_mecca_tag.spc > $meccadir/mecca.spc

  # linking generated code to smcl/xbox directories
  set tagfiles = (messy_mecca_tag*.f90 messy_mecca_tag*.inc)
  foreach tagfile ($tagfiles)

    set base = `basename $tagfile .f90`
   #set isbox = `echo $base | awk '{print substr($1,length($1)-2,3)}'`

    set isbox = `echo $tagfile "_box." | awk '{print index($1,$2)}'`
    set issi = `echo $tagfile "_si." | awk '{print index($1,$2)}'`
    set fext = $tagfile:e

    # default directory (smcl), or dedicated box/si
    set destdir = $f90smcl
    if ( "$isbox" != "0" ) set destdir = $f90box
    if ( "$issi" != "0" ) then
      if (${?bm3d}) then
        set destdir = $f90si
      else
        set destdir = ""
      endif
    endif

    foreach ddir ($destdir)
      if ( "$fext" == "inc" ) then
        # linking include files to all dirs, not copying
        cd $f90smcl; ln -fs $smcl2tag/$tagfile .; cd -
        cd $f90box;  ln -fs $box2tag/$tagfile .;  cd -
        if (${?bm3d}) then
          cd $f90si; ln -fs $si2tag/$tagfile .; cd -
        endif  
      else
        # copying to destination, linking back to tag
        if ("$destdir" != "") then
          if (-e $ddir/$tagfile) rm -f $ddir/$tagfile
          mv -f $tagfile $ddir/.
          ln -s $ddir/$tagfile .
          echo linking $tagfile from $ddir
        endif
      endif  
    end
  end

  echo ""
  echo "<<< done with tagging <<<"

endif

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

unsetenv $cfglist

echo ""
echo "all done --> returning"
echo ""
exit 0



# - THIS IS LEFT FROM THE PREVIOUS VERSION - please keep so far ------------
# --------------------------------------------------------------------------
if ("$do_recompile" == "true") then
  echo "recompiling imtag executable..."
  echo "(see compile.log for details)"
  rm ./imtag.exe
  if ("$do_kpp" == "true") set fpc_call = "$fpc_call -dCRT_INTEQN"
  $fpc_call imtag -oimtag.exe > compile.log
  if ("$status" != "0") then
    echo "not done"
    echo "--- listing compile.log ---"
    cat compile.log
    exit 1
  endif
  echo "done"
  echo ""
endif
