#!/bin/sh

# DART software - Copyright UCAR. This open source software is provided
# by UCAR, "as is", without charge, subject to all terms of use at
# http://www.image.ucar.edu/DAReS/DART/DART_download
#
# DART $Id$

# generate a family of directories for each of the executables
# in this directory (created by scanning the path_names_* files)
#
# before using this script, copy it to a model's work directory, along 
# with one of the doxygen configuration template files listed below.

# pick one: the "devel" version tries to create diagrams showing the calling
# and called relationship between subroutines, but the number and size
# of files it generates is much larger.

if [[ $1 == 'devel' || $1 == '-devel' ]]; then
  template=doxygen-devel-config-template
else
  template=doxygen-config-template
fi

# this assumes we are running from some work directory.
# if you are building from some other named dir, change
# 'work' to that dir name in the line below.
model=`pwd | sed -e 's;/work;;' -e 's;^.*/;;' `
umodel=`echo $model | tr '[a-z]' '[A-Z]' `

# figure out how far down we are in the directory hierarchy.
if [ -f        ../../../CHANGELOG ]; then
   toppath=`cd ../../../; pwd`
elif [ -f   ../../../../CHANGELOG ]; then
   toppath=`cd ../../../../; pwd`
elif [ -f   ../../CHANGELOG ]; then
   toppath=`cd ../../; pwd`
elif [ -f   ../../../../../CHANGELOG ]; then
   toppath=`cd ../../../../../; pwd`
else
   echo cannot find the top level DART directory
   echo based on the current working directory.
   exit -1
fi

# the first option puts all docs under DART/documentation.
# try putting them into models/bob/documentation instead?
# (this script needs to be run from a work directory)
#destdir=$toppath/documentation/doxygen/$model
destdir=../documentation
doxygen_dir=$toppath/documentation/doxygen

echo Generating documentation for all executables in the current directory
echo for ${model}.   All html files will be located under the directory
echo $destdir

if [ ! -d $destdir ]; then mkdir -p $destdir; fi

for i in path_names_*
do

  exename=`echo $i | sed -e 's/path_names_//' `
  echo generating documentation for $exename

  lcount=`wc -l $i | sed -e 's/^[\t ]*\([0-9][0-9]*\)*.*$/\1/'`
  let lcount=lcount-1
  sed -e "1,${lcount}s/\$/ \\\\/" -e "s;^;${toppath}/;" $i > flist

  # THISEXECUTABLE -> exename
  # THISMODEL -> model
  # THISDESTDIR -> destdir
  # flist -> multiline path_names_xx content with \ after every
  # line except the last one

  sed -e "s;THISDESTDIR;${destdir};" -e "s/THISEXECUTABLE/$exename/" -e "s/THISMODEL/$model/" -e "/^INPUT  /r flist" $doxygen_dir/$template > doxygen-$exename

  doxygen doxygen-$exename
  
  rm doxygen-$exename
done

# create an overall TOC file

cat > index.html <<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>${umodel}/DART executables</title>
</head>
<body>
<div> 

<H1>${umodel}/DART executables</H1>
<br />

<H3>Documentation for the following ${umodel}/DART executables:
<ul>
EOF

for i in path_names_*
do
  exename=`echo $i | sed -e 's/path_names_//' `
  echo "<li><a href=\"${exename}/html/index.html\">${exename}</a></li>" >> index.html
done

cat >> index.html <<EOF
</ul>

</div>
</body>
</html>
EOF

mv index.html $destdir

echo Open $destdir/index.html
echo in your browser to access the documentation.

exit 0

# <next few lines under version control, do not edit>
# $URL$
# $Revision$
# $Date$
