#!/usr/bin/env bash
# $LastChangedDate: 2010-10-09 18:36:14 -0400 (Sat, 09 Oct 2010) $
# Author: Jim E. Brooks www.palomino3d.org
# Auto-generate HTML documentation using doxygen.
# Prereq: Must be run from top-level dir.
# Args  : $1 = output directory (where to write HTML files)
# Problems:
# Doxygen acts like a preprocessor and will omit conditionally-compiled code
# so put #defines for doxygen here.
#-------------------------------------------------------------------------------

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

INIT_DIR=`pwd`
INIT_DIR_UNDERSCORES=`echo $INIT_DIR | sed "s/[/]/_/g"`

IMAGE_DIR=${INIT_DIR}/doc/html/images
FOOTER_IMAGE=palominoLogo.png
FOOTER="<img src='${FOOTER_IMAGE}' hspace='8'><a href='http://www.palomino3d.org'>Palomino Flight Simulator</a> documents generated by <a href='http://www.doxygen.org'>doxygen</a> \$doxygenversion on \$datetime </body></html>"
PREDEFINED="PREDEFINED = DOXYGEN=1 ARCH=generic STATS=1 DEBUG=1 GFXSYS_OSG=1 COMPILE_OSG=1 COMPILE_ZLIB=1 COMPILE_SDL=1 COMPILE_GUI=1 COMPILE_PLIB=1 COMPILE_OSSIM=1 NEED_PLANET_CLASS=1 "

# Find src dirs.  Separate by spaces.
for SUBDIR in src/*;
do
    # Skip some directories.
    if [ "$SUBDIR" != "src/lua_lang" ] && [ "$SUBDIR" != "src/deprecated" ]; then
        CC_DIRS="${CC_DIRS} ${INIT_DIR}/${SUBDIR}"
    fi
done
HH_DIRS=$CC_DIRS

# Src file extensions.
CC_EXT=.cc
HH_EXT=.hh

# Any file used to just check if CWD is top-level dir of project.
TOP_LEVEL_FILE=${INIT_DIR}/src/base/module.hh

# Sed script.
SED_SCRIPT=${INIT_DIR}/tools/doxygen.sed

# Files to exclude (some cause doxygen to misparse).
#EXCLUDES="src/base/thread_single.hh src/base/shared_ptr2.hh"
EXCLUDES="src/base/shared_ptr2.hh"

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

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

DOC_DIR=$1

# Ensure doxygen is installed.
if ( ! type -pf doxygen >/dev/null 2>/dev/null ); then
    echo "ERROR[doxygen.sh]: doxygen is required"
    exit 1
fi

# Ensure this script is run from the top-level dir.
if [ ! -f $TOP_LEVEL_FILE ]; then
	echo "ERROR[doxygen.sh]: Not sure if this is the top-level dir (?)."
	exit 1
fi

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

# 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() {
    ORIG_FILE=$1  # save
    FILE=$1

    # Catch unexpanded glob
    if ( echo "$FILE" | grep -q "*" ); then
        return  # dir could be empty
    fi

    if [ ! -f $ORIG_FILE ]; then
        echo "ERROR[doxygen.sh]: orig src file $ORIG_FILE does not exist."
        exit 1
    fi

    # Exclude some files.
    for EX in $EXCLUDES;
    do
        # Use grep to tolerate superfluous paths.
        if ( echo $FILE | grep -q $EX ); then
            return
        fi
    done

    # src/module/file.cc --> src_module_file.cc
    # Transform pathname into a filename by turning '/' to '_'.
    # The reason is that doxygen searches only in the CWD
    # but src code is across subdirs.
    FILE=`echo $FILE | sed "s/[/]/_/g"`

    # Remove leading part of pathname: _home_user_src_*
    FILE=`echo $FILE | sed "s/${INIT_DIR_UNDERSCORES}//g"`

    # Remove leading '.' (depends on prog lang).
    FILE=`echo $FILE | sed "s/^[.]//g"`

    # FILE string is done.
    # Don't overwrite any file!
    if [ -f $FILE ]; then
        echo "ERROR[doxygen.sh]: why does $FILE exist?"
        exit 1
    fi

    # Preprocess src file thru sed.
    cat ${ORIG_FILE} | sed -f ${SED_SCRIPT} > ${FILE}
}

# Process each src file.
for DIR in $CC_DIRS;
do
    for FILE in ${DIR}/*${CC_EXT}; do DoFile $FILE; done
done
for DIR in $HH_DIRS;
do
    for FILE in ${DIR}/*${HH_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
echo "ALPHABETICAL_INDEX = yes" >> doxygen.config

# Gray choices:
# ENABLE_PREPROCESSING=1 works for #include inside a class.
# ENABLE_PREPROCESSING=0 fixes doxygen ignoring conditionally-compiled code.
echo "ENABLE_PREPROCESSING = YES" >> doxygen.config

# Try to make "#if" work.
echo "MACRO_EXPANSION = YES" >> doxygen.config
echo "EXPAND_ONLY_PREDEF = NO" >> doxygen.config
echo "SKIP_FUNCTION_MACROS = NO" >> doxygen.config
echo "${PREDEFINED}" >> doxygen.config

# Tell doxygen to skip unimportant src files.
echo "EXCLUDE_PATTERNS = *deprecated*" >> doxygen.config

# Nice HTML footer that shows logo.
cp ${IMAGE_DIR}/${FOOTER_IMAGE} . || exit 1
echo "$FOOTER" > html_footer
echo "HTML_FOOTER = html_footer" >> doxygen.config

doxygen doxygen.config || exit 1
mv html/* . && rmdir html
# Return above line's exit code.
