#!/bin/csh -f
#
# Earth System Modeling Framework
# Copyright (c) 2002-2024, University Corporation for Atmospheric Research, 
# Massachusetts Institute of Technology, Geophysical Fluid Dynamics 
# Laboratory, University of Michigan, National Centers for Environmental 
# Prediction, Los Alamos National Laboratory, Argonne National Laboratory, 
# NASA Goddard Space Flight Center.
# Licensed under the University of Illinois-NCSA License.
#
# $Id$
#
#===============================================================================
# do_latex
#===============================================================================
# Build document with bibliography if available. 
#
# Usage: do_latex <comp> <document type>
#
#        <comp> is the name of the component, library, or module used as the
#               basis for document filenames (e.g., coupler)
#
#        <document type> is one of the following:
#               req  - requirements document
#               arch - architecture document
#               des  - detailed design document        
#               ref  - reference manual
#               usr  - user's guide
#               phys - algorithmic or physical description
#
# C. DeLuca                                                       March 15, 2000
#===============================================================================

if ($#argv != 2) then
  echo "Usage: do_latex <component> <document type>"
  exit
endif

if ( $?TEXINPUTS) then 
 echo "in do_latex, TEXINPUTS =", $TEXINPUTS
else
 echo "in do_latex, TEXINPUTS unset" 
endif

# if needed, pre-process as top-level constituent or standalone document
if (-e $1_$2doc.ctex) then

  # move original out of the way
  if (-e $1_$2doc.tex) then
    mv $1_$2doc.tex $1_$2doc.tex.ORIG
  endif

  # perform pre-processing
  if(($1 == "ESMF")||($1 == "ESMC")) then
    gcc -E -x c -P -C -nostdinc -DCONSTITUENT $1_$2doc.ctex | grep -v "^#pragma" > $1_$2doc.tex
    gcc -E -x c -P -C -nostdinc $1.cbib | grep -v "^#pragma" > $1.bib
  else
    gcc -E -x c -P -C -nostdinc -DSTANDALONE $1_$2doc.ctex | grep -v "^#pragma" > $1_$2doc.tex
  endif

endif

# there used to be a test if $1 was ESMF, but the 2 sets of
# commands it did based on that test was identical, so i removed
# the test.  nsc 29jul04

rm -f $1_$2doc.bib
cp $1.bib $1_$2doc.bib

latex $1_$2doc

bibtex $1_$2doc
latex $1_$2doc

bibtex $1_$2doc
latex $1_$2doc

rm -f $1_$2doc.bib
