#!/bin/bash
#
# Shell script to make doxygen documentation by specifying a target directory
# on the command line
#
# Gef (gefdavis@dingoblue.net.au) -- August 2001

# TODO:
# - Dynamic ChangeLog (page gets updated with each commit)
# - Have the ability to specify server dox or local dox, which will then
#   change the content on the main page
# - Incorporate a scaled gtkradiant splash image into the pages

#------------------------------------------------------------------------
# Set some variables
#------------------------------------------------------------------------
# WORKINGDIR=`pwd`;
RETVAL=0;
TARGETSTRING='';
EXTRAS_PATH="./Doxygen_files";
CONFIG_OUTPUT="$EXTRAS_PATH/genConf";
DOXYCONFIG="./DoxyConfig";
DOXYFILE="$EXTRAS_PATH/Doxyfile";
NEWDOXYFILE="$EXTRAS_PATH/genDoxyfile";
declare -a TARGETLIST[$#];
COUNTER=0;
TARGETCOUNT=0;
QUIETMODE=0;
# added -k command line option to kill running doxygen procs
KILLON=0

#------------------------------------------------------------------------
# load the functions
#------------------------------------------------------------------------
if [ -f "$EXTRAS_PATH/gendoxfunctions" ] ; then
	. $EXTRAS_PATH/gendoxfunctions
else
  echo -e "Missing critical files...\n";
  exit 1;
fi

#------------------------------------------------------------------------
# parse the command line options
#------------------------------------------------------------------------
COMLINE="$*";
OPTCOUNT="$#";
parse_commandline;
if [ $RETVAL -gt 0 ] ; then
	echo -e "Exiting.";
	exit $RETVAL;
fi


if [ $KILLON -gt 0 ] ; then
  PIDOF_DOXYGEN=`pidof -x doxygen`
  MYPID=$$
  
  if [ -z "$PIDOF_DOXYGEN" ] ; then
    [ $QUIETMODE -gt 0 ] || echo -e " * Killing other doxygen pids";
    killall -q -9 doxygen    
  else
    [ $QUIETMODE -gt 0 ] || echo -e " * Killing other doxygen pids";
    kill -9 $PIDOF_DOXYGEN &> /dev/null
  fi
  
  [ $QUIETMODE -gt 0 ] || echo -e " * Cleaning up gendox pids";
  killall -q -9 `pidof -x gendox | sed -e s/$MYPID//` &> /dev/null
  
fi

# If the output dir hasn't been set yet...
#if [ -z "$OUTPUTDIR" ] ; then
#	OUTPUTDIR="../$(basename `pwd`)-doxygen";
#fi

#------------------------------------------------------------------------
# execute some functions to determine stuff(c)
# Get the perl path (either from the config file, or find it)
#------------------------------------------------------------------------
get_perlpath;
if [ X"$PERLPATH" == "X" ] ; then
	echo -e "\nError: A working install of perl is needed to use doxygen";
	exit 2;
fi
[ $QUIETMODE -gt 0 ] || echo -e " -> Set PERL_PATH to: $PERLPATH";

get_dotpath;
[ $QUIETMODE -gt 0 ] || echo -e " -> Set HAVE_DOT to: $HAVEDOT";
if [ X"$HAVEDOT" == "XYes" ] ; then
	[ $QUIETMODE -gt 0 ] || echo -e " -> Set DOT_PATH to: $DOTPATH";
fi

get_language;
[ $QUIETMODE -gt 0 ] || echo -e " -> Set OUTPUT_LANGUAGE to: $OUPUTLANGUAGE";

get_projectname;
[ $QUIETMODE -gt 0 ] || echo -e " -> Set PROJECT_NAME to: $PROJECTNAME";

get_version;
[ $QUIETMODE -gt 0 ] || echo -e " -> Set PROJECT_NUMBER to: $VERSION";
#------------------------------------------------------------------------
# Got everything we need, now write the DoxyConfig file and run doxygen
#------------------------------------------------------------------------

# Clean up first
clean_up;

# Put the images & reference pages in the right place
move_stuff;
if [ $RETVAL -ge 666 ] ; then
	exit 666;
fi

# Generate the config file
gen_doxyconfig;
if [ $RETVAL -gt 0 ] ; then
	echo -e "Error: You are missing critical files."
	exit RETVAL;
fi

# build the reference page and the index
build_extra_html;

# Generate documentation
RETVAL=0;
run_doxygen;
if [ $RETVAL -gt 0 ] ; then
	echo -e "Doxygen error: returned $RETVAL";
	echo -e " Check doxygen.log for details";
elif [ $RETVAL -lt 0 ] ; then
	echo -e "Doxygen error: Doxygen returned $RETVAL";
fi

# if the log file is empty, remove it
if [ ! -s ./doxygen.log ] ; then
	rm -f ./doxygen.log
fi

#------------------------------------------------------------------------
# Done.
#------------------------------------------------------------------------
[ $QUIETMODE -gt 0 ] || echo -e "Finished...";
[ $QUIETMODE -gt 0 ] || echo -e "Duration: $SECONDS seconds\n";

# echo -e "** Removing output while in debug mode **";
# echo -e "** Output dir: $OUTPUTDIR **\n";
# rm -rf $OUTPUTDIR

exit 0;

