#! /bin/tcsh -f
# Time-stamp: <2010-12-20 17:33:22 sander>
# create a self-consistent zip file of a new caaba version

if ( "$1" == "" ) then
  echo ""
  echo "Info (before making a new version):"
  echo "- run model with:"
  echo "  ./xcaaba mbl"
  echo "  and plot results"
  set current_version = `grep -i "modver *=" messy_mecca.f90 | sed "s|.*'\(.*\)'.*|\1|g"`
  echo "- update version number (modver = '$current_version'):"
  echo "  e messy_mecca.f90"
  echo "- Enter standard settings:"
  echo "  Makefile:        COMPILER = g95"
  #echo "                   OUTPUT = ASCII or NETCDF?"
  echo "                   F90FLAGS = fast or with checks?"
  #echo "  mecca.nml:       mecca_aero = 'AUTO'"
  echo "  namelist:        ln -fs nml/caaba_simple.nml caaba.nml"
  echo '  setmodelrun.jnl: DEFINE SYMBOL C2 = `{spawn: "cd .. ; pwd"}`'
  echo "                   GO _define_sensi ... latest run (no other plots)"
  echo "- add all changes and current date to CHANGELOG"
  echo "Usage:"
  echo "  ncv nn"
  echo "  (nn = version number)"
  echo "Examples:"
  echo "  snapshot with current date:        ncv `date +'%Y%m%d'`"
  echo "  proper version number, e.g.:       ncv $current_version"
  echo ""
  exit
endif

if (! -e mecca/smcl/messy_mecca_kpp_parameters.f90) then
  echo;echo "Please run xmecca with KPP (not KP4) before executing ncv\!"
  exit 1
endif

if (! -e sfmakedepend) then
  echo;echo "The perl script sfmakedepend does not exist. Please run"
  echo "configure to create it from sfmakedepend.pl.in:"
  echo "  ( cd ../../.. ; ./configure ; cd - )" ; echo
  exit 1
endif

set basedir = `basename $PWD`
set dirname = ${basedir}_$1
set oridir  = $HOME/messy/mecca/ori

if ( -d $oridir) then
  set zipfile = $oridir/$dirname.zip
else
  echo "Error: Directory $oridir/ does not exist."
  exit 1
endif

if ( -e $zipfile) then
  echo;echo "Error, the zip file exists already:"
  ls -la $zipfile
  echo "Please choose another version number\!"
  exit 1
endif

set MECCA_VERSION = `grep -i "modver *=" messy_mecca.f90 | sed "s|.*'\(.*\)'.*|\1|g"`
set infofile = "manual/mecca_info.tex"
set dontedit = "this file was created automatically by ncv, do not edit"
echo "% $dontedit"                          > $infofile
echo "\\def\\meccaversion{$MECCA_VERSION}" >> $infofile

echo "Update User Manual, graphviz and meccanism.pdf? [y/n/q, default=n]"
set inputstring = $<
if ( $inputstring == 'q' ) exit
if ( $inputstring == 'y' ) then
  # select full mechanism:
  cd mecca
  xmecca latex
  cd -
  # it is necessary to repeat the pdflatex calls because meccalit.bib
  # must be updated:
  cd mecca/latex
  pdflatex meccanism.tex
  # extract references from meccanism.aux:
  citetags meccanism.aux                          > citetags.log
  # add references from gas.eqn:
  gawk -f findrefs.awk ../gas.eqn                >> citetags.log
  # add references from gas_mim1.eqn:
  gawk -f findrefs.awk ../gas_mim1.eqn           >> citetags.log
  # add references from aqueous.eqn:
  gawk -f findrefs.awk ../aqueous.eqn            >> citetags.log
  # add references from messy_cmn_gasaq.f90:
  gawk -f findrefs.awk ../../messy_cmn_gasaq.f90 >> citetags.log
  # create *.bib file:
  citefind citetags.log ~/tex/bib/literat.bib > meccalit.bib
  # create *.bbl file:
  bibtex meccanism
  pdflatex meccanism.tex
  pdflatex meccanism.tex
  pdflatex meccanism.tex
  # move meccanism.pdf to manual/ directory, otherwise it would be
  # deleted by "xmecca simple":
  mv meccanism.pdf ../../manual/meccanism.pdf
  cd -
  # select simple mechanism:
  cd mecca
  xmecca simple
  cd -
  # User Manual:
  cd manual
  pdflatex caaba_mecca_manual.tex
  bibtex caaba_mecca_manual.aux
  pdflatex caaba_mecca_manual.tex
  pdflatex caaba_mecca_manual.tex
  cd -
endif

# temporarily rename the caaba directory to include the version:
cd .. ; mv $basedir $dirname ; cd $dirname

# create zip file:
_zipcaaba.tcsh zip

# rename the caaba directory back to its original name:
cd .. ; mv $dirname $basedir ; cd $basedir

# move zip file to final directory:
mv $dirname.zip $zipfile

echo;echo "The zipfile has been created:"
ls -la $zipfile

exit
