#!/bin/sh
# $Id: autogen_docs_html.sh,v 1.3 2006/11/11 04:53:56 devel Exp $
# Author: Jim E. Brooks http://www.jimbrooks.org
# Auto-generate HTML documentation using doxygen.
# Prereq: Must be run from top-level dir.
# Args  : $1 = output directory (where to write HTML files)
#-------------------------------------------------------------------------------

#============= BEGIN CONFIGURATION =============================================

# Source dirs.  Separate by spaces.
C_DIRS="gfx/src input/src palomino/engine/src palomino/game/base/src palomino/game/pony/src"
H_DIRS=$C_DIRS

# Source file extensions.  C_EXT can be used for C++.
C_EXT=.cc
H_EXT=.hh

# Any file used to just check if CWD is top-level dir of project.
TOP_LEVEL_FILE=gfx/GNUmakefile

# Sed script.
SED_SCRIPT=gfx/tools/autogen_docs_html.sed

# Files to exclude
EXCLUDES="gfx_font1.cc gfx_font2.cc"

#============= END CONFIGURATION ===============================================

# Assign vars with args.
if [ "$1" = "" ]; then
	echo "ERROR: Args required"
    exit 1
fi
DOC_DIR=$1

# Ensure doxygen is installed.
if ( ! which doxygen >/dev/null 2>/dev/null ); then
    echo "ERROR: doxygen is required"
    exit 1
fi

# Ensure this script is run from the top-level dir.
[ -f $TOP_LEVEL_FILE ] || exit 1

# Make output directory.
mkdir -p $DOC_DIR
cd $DOC_DIR || exit 1

# Start with clean dir.
rm -f  *${C_EXT}  *${H_EXT}
rm -rf html

# Pass every src file thru a sed script which transforms my style
# for C function comments into what doxygen expects as so:
#
# /*****************************************************************************
#  * 
#  *****************************************************************************/
#
# /** <-- two asterisks is what doxygen looks for
#  * 
#  *****************************************************************************/
#
DoFile() {
    FILE=$1
	if ( echo "$FILE" | grep -q "*" ); then
        return # unexpanded fileglob
    fi
    BASE_FILE=${FILE##*/}
    # Exclude some files.
    for EX in $EXCLUDES;
    do
        if [ "$EX" = "$BASE_FILE" ]; then
            return
        fi
    done
    cat $FILE | sed -f ../${SED_SCRIPT} > ${BASE_FILE}
}
for DIR in $C_DIRS;
do
    for FILE in ../${DIR}/*${C_EXT}; do DoFile $FILE; done
done
for DIR in $H_DIRS;
do
    for FILE in ../${DIR}/*${H_EXT}; do DoFile $FILE; done
done

# Run doxygen.
rm -f doxygen.config
doxygen -g doxygen.config || exit 1
echo "GENERATE_LATEX  = no " >> doxygen.config
echo "EXTRACT_ALL     = yes" >> doxygen.config
echo "EXTRACT_PRIVATE = yes" >> doxygen.config
echo "EXTRACT_STATIC  = yes" >> doxygen.config
echo "TAB_SIZE        = 4  " >> doxygen.config
# Fix doxygen ignoring conditionally-compiled code.
echo "ENABLE_PREPROCESSING = 0" >> doxygen.config

doxygen doxygen.config || exit 1

# Cleanup.
rm -f doxygen.config
rm -f  *${C_EXT} *${H_EXT}

exit 0
