#!/bin/sh
# Copyright Anton Melser 2004, this documentation script is BSD-type licenced software. See the generated docs for further information.

#This script should make everything nicely
#Needless to say, my scripting sux!

MAKEPDF="no"
MAKEHTML="no"
MAKEAPI="no"
DOCHOMEHTML="dochome.html"
CUSTOMSINGLE="doc/gdocs/customsingle.xsl"
CUSTOMMULTI="doc/gdocs/custommulti.xsl"

debug()
{
  echo "PATH is: $PATH"
  echo "applications: doxygen, dot, htmldoc, xsltproc, xmlto"
  echo "doxygen: `which doxygen`"
  echo "dot: `which dot`"
  echo "htmldoc: `which htmldoc`"
  echo "xsltproc: `which xsltproc`"
  echo "xmlto: `which xmlto`"
}

#Check if doxygen is installed
capi() { if [ -z $(which doxygen 2> /dev/null) ]
    	then
		return 1
	else
		return 0
	fi; }
	
#Check if xsltproc is installed
html() { if [ -z $(which xsltproc 2> /dev/null) ]
    	then
		return 1
	else
		return 0
	fi; }
	
#Check if htmldoc is installed
pdf() { if [ -z $(which htmldoc 2> /dev/null) ]
    	then
		html
		let "htmlret = $? + 1"
		return $htmlret
	else
		html
		let "htmlret = $? + 0"
		return $htmlret
	fi; }
	
#Check if all required apps are installed
all() { 
	myret=0
	capi
	let "myret = $?"
	html
	let "myret = $? + myret"
	pdf
	let "myret = $? + myret"
	return $myret; 
	}

#General help messages to be outputted whenever something unexpected is put on the command line.
helpinfo () { 
	echo ""
	echo "Usage - You may only use the options:"
	echo "     -h (for html of the parameters passed)" 
	echo "     -p (for pdf of the parameters passed)"
	echo "     -a (for both/all)"
	echo  "Or a combination to build the desired documentation. If you want to build anything but the api "
	echo "then you must specify one or else the script will do nothing."
	echo ""
	echo "Respecting case, you must specify one or more of the following parameters:"
	echo ""
	echo "    general - for a general, non-technical introduction to the G System Framework."
	echo "    technical - for a technical introduction to the G System Framework."
	echo "    tutorial - for a set of tutorials on programming with the G System."
	echo "    featureplan - for details of the current and planned features of the components of the system"
	echo "    philocorner - for a set of philosophical articles and writings pertanent to the project."
	echo "    all - to build all the above sets of documentation."
	echo "    api - to build the api documentation (not included in all)"
	echo "        api may be called without options as they have no effect anyway."
	echo ""
	echo "e.g., $ makedocs -h general technical"
	echo "Will build the html docs for the general and technical introductions"
	echo ""
	echo "      $ makedocs -a philocorner api"
	echo "Will build both the pdf and html for philocorner and will also build the api documentation."
	echo ""
	echo "All the documentation created can be accessed via doc/dochome.html"
	echo ""
	echo "Please report any bugs you find at the G System forum or mailing list. See www.g-system.at/index.php"
	echo "for more details."
	echo ""; }

#Check whether docbook is installed and we can get the DTD files locally or we need to get them from the web
localinstall () {
	if [ -f /etc/xml/docbook ]
	then
		return 0
	else
		return 1
	fi;
	}

#xmlto also uses local xsl files if they exist. Not a pretty solution to the actual problem, but oh well!
hasxmlto () { if [ -z $(which xmlto 2> /dev/null) ]
    		then
			return 1
		else 
			return 0
		fi;
	}

#function name says it all	
endmessage () {
echo ""
echo "You will find the introductions, articles and tutorial documentation in the html and/or pdf "
echo "directories of the respective subdirectories of doc/gdocs and the api docs in doc/gdocs/api-reference."
echo ""
echo "The html flag produces both a single page and multile page html versions."
echo "The single page file is called nameofdoc.html, and the index for the multi version is index.html"
echo ""
echo "You can easily navigate all the documentation by loading doc/gdocs/dochome.html"
echo "into your browser. This script automatically adds the entries you created."
echo ""
echo "Please report any bugs you find at the G System forum or mailing list."
echo "See www.g-system.at/index.php for more details."
echo ""
}

#Writes the start of the dochome.html file
startdochome () {
echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\"><title>Documentation Home</title><meta name=\"keywords\" content=\"G, System, Philosophy\"></head>" > $DOCHOMEHTML
echo "<a href=\"http://www.g-system.at/index.php\"><img src=\"bits/logosmall.png\" width=\"150\" height=\"113\" /></a>" >> $DOCHOMEHTML
echo "<body><h1>G System Documentation Home</h1>" >> $DOCHOMEHTML
echo "<p>Here are links to the documents that were created with the makedocs script. If you want to access other documentation then you will have to run the script again in the top directory of the source distribution, this time passing the options and parameters you want. Enter $ ./makedocs -help for more information.<p>" >> $DOCHOMEHTML

echo "<table width=\"600\" cellspacing=\"3\" border=\"3\" cellpadding=\"3\" align=\"center\"><caption><h2>G System documentation links</h2></caption><tbody><tr>" >> $DOCHOMEHTML

if  [ "$MAKEHTML" = "yes" ]
then
	echo "<th>Multifile html</th><th>Single file html</th>" >> $DOCHOMEHTML
fi
if  [ "$MAKEPDF" = "yes" ]
then
	echo "<th>pdf files</th>" >> $DOCHOMEHTML
fi
echo "</tr>" >> $DOCHOMEHTML
}

enddochome () {
#Make the licence agreement and print to dochome.html
xsltproc -o doc/gdocs/commonDocbook/html/fdl.html $CUSTOMSINGLE doc/gdocs/commonDocbook/fdlwrapper.docbook
xsltproc -o doc/gdocs/commonDocbook/html/BSD.html $CUSTOMSINGLE doc/gdocs/commonDocbook/BSDLicence.docbook

echo "<tr><th>GNU Free Doc Licence</th><th colspan="2">BSD Code Licence</th><tr>" >> $DOCHOMEHTML

echo "<tr><td align="center"><a href=\"commonDocbook/html/fdl.html\">GNU Free Documentation Licence</a></td>" >> $DOCHOMEHTML
echo "<td colspan="2" align="center"><a href=\"commonDocbook/html/BSD.html\">BSD Code Licence</a></td></tr></tbody></table></body></html>" >> $DOCHOMEHTML

echo "" >> $DOCHOMEHTML
}

fixPaths() 
{
	if [ -f dochome.html ]
 	then
		sed 's/\/doc\/gdocs//g' dochome.html > doc/gdocs/dochome.html
	fi
	if [ -f doc/gdocs/dochome.html ]
	then
		rm -f dochome.html
	fi
}	

# uncomment this to see some debug output (PATH and available applications)
#debug

#nothing at all passed on the CL
if [ $# -eq 0 ]
then
	helpinfo
	
	exit 1
fi

#only when api is the only thing called can there be no options passed
if [ $# = 1 ] && [ $1 != "api" ]
then
	helpinfo
	
	exit 1
fi

#Get the options and set flags, checking to make sure that the necessary apps are installed
while getopts ":hpa" Option
do
  case $Option in

    h ) html
    	if [ $? -gt 0 ]
	then
		echo ""
		echo "You must have the programme \"xsltproc\" installed to create the html docs"
		echo ""
		exit 2
	else
		MAKEHTML="yes"
	fi;;
    
    p ) pdf
    	if [ $? -gt 0 ]
    	then
		echo ""
		echo "You must have \"htmldoc\" AND \"xsltproc\" installed to make the .pdfs"
		echo ""
		exit 2
	else
		MAKEPDF="yes"
	fi;;
	
    a ) all
    	if [ $? -gt 0 ]
    	then
		echo ""
		echo "You must have \"htmldoc\" AND \"xsltproc\" installed to make all the docs"
		echo ""
		exit 2
	else
		MAKEPDF="yes"
		MAKEHTML="yes"

	fi;;
    * ) helpinfo
   	exit 2;;
  esac
done
shift $(($OPTIND - 1))

MYCWD="."

#If only options and no parameters were passed, else
if [ $# -eq 0 ] 
then
	helpinfo
	exit 4
	
else	#create a list of all the documents the user wants html and/or pdf for
	until [ -z "$1" ]  # Until all parameters used up...
	do
	case $1 in
		api ) capi
        		if [ $? -gt 0 ]
    			then
				echo ""
				echo "You must have \"doxygen\" installed to make the code api reference"
				echo ""
				exit 2
			else
				MAKEAPI="yes"
			fi;; 
		general ) doclist=$(echo $doclist $MYCWD/doc/gdocs/general/general.docbook);;
		philocorner ) doclist=$(echo $doclist $MYCWD/doc/gdocs/philocorner/philocorner.docbook);;
		technical ) doclist=$(echo $doclist $MYCWD/doc/gdocs/technical/technical.docbook);;
		tutorial ) doclist=$(echo $doclist $MYCWD/doc/gdocs/tutorial/tutorial.docbook);;
		featureplan ) doclist=$(echo $doclist $MYCWD/doc/gdocs/featureplan/featureplan.docbook);;
		all ) doclist=$(echo $MYCWD/doc/gdocs/general/general.docbook $MYCWD/doc/gdocs/philocorner/philocorner.docbook $MYCWD/doc/gdocs/technical/technical.docbook $MYCWD/doc/gdocs/tutorial/tutorial.docbook $MYCWD/doc/gdocs/featureplan/featureplan.docbook);;
		* ) helpinfo
		    exit 4;;
	esac
	shift
	done	
fi
	
echo "Making the documentation for the G System"

startdochome

if [ "$MAKEHTML" = "yes" ] || [ "$MAKEPDF" = "yes" ]
then

	
   for docset in $doclist
   do	#Assign base variables for the pass
	working=$(dirname $docset)
	echo $working
	filname=$(basename $docset)
	mybase=${filname%%.*}
	
	if [ ! -d $working/html ]
	then
		mkdir $working/html
	fi
	
	echo "<tr>" >> $DOCHOMEHTML
	
	#checks for a local install of docbook and then for the presence of xmlto
	#localinstall
	#go=$?
	#hasxmlto
	#let "go = go + $?"
	
	#Temporarily disabled, cos I don't think it makes a difference as xsltproc uses the schema anyway.
	go=2
	
	#Make chunked html docs
	if [ "$MAKEHTML" = "yes" ]
	then
		echo
		echo "Making the html docs in " $working/html 
		echo
		#Either use local install of docbook or go net
		if [ $go -eq 0 ]
		then
			$(which xmlto) html -o $working/html -x $CUSTOMMULTI $docset
		else	
			xsltproc -o $working/html/ $CUSTOMMULTI $docset
		fi
		#$(which xmlto) html -o $working/html $docset
		
		#Add the relevant entries in dochome.html
		echo "<td align="center"><a href=\"$working/html/index.html\">$mybase</a></td><td align="center"><a href=\"$working/html/$mybase.html\">$mybase</a></td>" >> $DOCHOMEHTML
		
		#Copy all png images to the destination directory
		cp $working/*.png $working/html
	fi
	#Either use local install of docbook or go net to make non-chunked html docs
	if [ $go -eq 0 ]
	then
		$(which xmlto) html-nochunks -o $working/html  -x $CUSTOMMULTI $docset
	else
		xsltproc -o $working/html/$mybase.html $CUSTOMSINGLE $docset
	fi
	
	#If pdfs are desired then make them - I know, I know! I couldn't get xsltproc working for pdfs so this is an ugly hack
	if [ ! -d $working/pdf ] && [ "$MAKEPDF" = "yes" ]
	then
		mkdir $working/pdf
	fi
	if [ "$MAKEPDF" = "yes" ]
	then
		echo
		echo "Making the pdf docs in " $working/pdf
		echo
	
		$(which htmldoc) -f $working/pdf/$mybase.pdf $working/html/$mybase.html
		
		echo "<td align="center"><a href=\"$working/pdf/$mybase.pdf\">$mybase</a></td>" >> $DOCHOMEHTML
	fi
	
	#Remove the non-chunked html used to make the pdfs, cos we don't want them
	if [ "$MAKEHTML" = "no" ]
	then
		rm -rf $working/html
	fi
	
	echo "</tr>" >> $DOCHOMEHTML
	
   done
fi

#Make the API docs
if [ "$MAKEAPI" = "yes" ]
then

 	echo ""
 	echo "Making API documentation for the G System"
 	echo ""

 	doxygen
	
	echo "<tr><th colspan="3">API Documentation</th><tr>" >> $DOCHOMEHTML
	echo "<tr><td colspan="3" align="center"><a href=\"api-reference/html/index.html\">API documentation</a></td><tr>" >> $DOCHOMEHTML
	
 	echo ""
	echo "Finished making the API docs."
	echo ""
fi

enddochome

endmessage

fixPaths

exit 0



syntax highlighted by Code2HTML, v. 0.9.1