#!/bin/bash

echo Rebuilding the documentation directory...

# Don't want them to be world or group writable

umask 002

# Clean out the documentation directory...
# Don't do this by default as it could be dangerous to some people...
# rm ../docs/*

# Top level files

cat templ/header.txt templ/links.txt templ/title.txt src/doc-index.body templ/footer.txt > ../docs/doc-index.html

# Installation files

cat templ/header.txt templ/links-install.txt templ/title.txt src/install-index.body templ/footer.txt > ../docs/install-index.html

# Configuration files

#-----------------------------------------------------------------------------
# Build the generic flag list body.
#-----------------------------------------------------------------------------
FLAGS=`ls src/genflags | grep -v CVS`
./header "Generic Object: Flags" > tmp/config-genflags.body
echo "<P>
<TABLE BORDER=2 WIDTH=\"100%\" CELLPADDING=5>
<TR VALIGN=TOP><TH>Name of Flag</TH><TH>Description</TH></TR>" >> tmp/config-genflags.body

for flag in $FLAGS
do
    echo "<TR><TD><A NAME=\"#${flag}\">${flag}</A></TD><TD>`cat src/genflags/${flag}`</TD></TR>" >> tmp/config-genflags.body
done

echo "</TABLE></TD></TR></TABLE>" >> tmp/config-genflags.body
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Build the ship flag list body.
#-----------------------------------------------------------------------------
FLAGS=`ls src/shipflags | grep -v CVS`
./header "Ships and Bases: Flags" > tmp/config-shipflags.body
echo "<P>
<TABLE BORDER=2 WIDTH=\"100%\" CELLPADDING=5>
<TR VALIGN=TOP><TH>Name of Flag</TH><TH>Description</TH></TR>" >> tmp/config-shipflags.body

for flag in $FLAGS
do
	echo "<TR><TD><A NAME=\"#${flag}\">${flag}</A></TD><TD>`cat src/shipflags/${flag}`</TD></TR>" >> tmp/config-shipflags.body
done

echo "</TABLE></TD></TR></TABLE>" >> tmp/config-shipflags.body
#-----------------------------------------------------------------------------

cat templ/header.txt templ/links-config.txt templ/title.txt src/config-index.body templ/footer.txt > ../docs/config-index.html
cat templ/header.txt templ/links-config.txt templ/title.txt src/config-consoles.body templ/footer.txt > ../docs/config-consoles.html
cat templ/header.txt templ/links-config.txt templ/title.txt src/config-generic.body templ/footer.txt > ../docs/config-generic.html
cat templ/header.txt templ/links-config.txt templ/title.txt tmp/config-genflags.body templ/footer.txt > ../docs/config-genflags.html
cat templ/header.txt templ/links-config.txt templ/title.txt src/config-ships.body templ/footer.txt > ../docs/config-ships.html
cat templ/header.txt templ/links-config.txt templ/title.txt tmp/config-shipflags.body templ/footer.txt > ../docs/config-shipflags.html
cat templ/header.txt templ/links-config.txt templ/title.txt src/config-shipdamage.body templ/footer.txt > ../docs/config-shipdamage.html

# Coordinate files

cat templ/header.txt templ/links-coords.txt templ/title.txt src/coords-index.body templ/footer.txt > ../docs/coords-index.html

# Event files

cat templ/header.txt templ/links-events.txt templ/title.txt src/events-index.body templ/footer.txt > ../docs/events-index.html
cat templ/header.txt templ/links-events.txt templ/title.txt src/events-combat.body templ/footer.txt > ../docs/events-combat.html
cat templ/header.txt templ/links-events.txt templ/title.txt src/events-contact.body templ/footer.txt > ../docs/events-contact.html
cat templ/header.txt templ/links-events.txt templ/title.txt src/events-land.body templ/footer.txt > ../docs/events-land.html
cat templ/header.txt templ/links-events.txt templ/title.txt src/events-misc.body templ/footer.txt > ../docs/events-misc.html

# Spacecall files

#-----------------------------------------------------------------------------
# Build the alphabetical spacecall list body.
#-----------------------------------------------------------------------------

FLAGS=`ls src/sc | grep -v CVS`
CALLS="sc-alpha sc-object sc-coding sc-comm sc-util sc-nav sc-shield sc-damcon sc-trans sc-shipmisc sc-eng sc-tac"

./header "Spacecalls: Alphabetical" > tmp/sc-alpha.body
./header "Spacecalls: Object Calls" > tmp/sc-object.body
./header "Spacecalls: Miscellaneous Coding Calls" > tmp/sc-coding.body
./header "Spacecalls: Communication Console Calls" > tmp/sc-comm.body
./header "Spacecalls: Utility Calls" > tmp/sc-util.body
./header "Spacecalls: Navigation Console Calls" > tmp/sc-nav.body
./header "Spacecalls: Shield Console Calls" > tmp/sc-shield.body
./header "Spacecalls: Damage Control Console Calls" > tmp/sc-damcon.body
./header "Spacecalls: Transporter Console Calls" > tmp/sc-trans.body
./header "Spacecalls: General Console Calls" > tmp/sc-shipmisc.body
./header "Spacecalls: Engineering Console Calls" > tmp/sc-eng.body
./header "Spacecalls: Tactical Console Calls" > tmp/sc-tac.body

for file in $CALLS
do
    echo "<P>
<TABLE BORDER=2 WIDTH=\"100%\" CELLPADDING=5>
<TR VALIGN=TOP><TH>Spacecall</TH><TH>Return Value</TH><TH>Object</TH><TH>Ship</TH></TR>" >> tmp/${file}.body
done

for flag in $FLAGS
do
    KEYS=`head -n 1 src/sc/${flag}`
    NAME=`head -n 2 src/sc/${flag} | tail -n 1`
    RTRN=`head -n 3 src/sc/${flag} | tail -n 1`"&nbsp;"
    DESC=`tail -n +4 src/sc/${flag}`
    OBJF="&nbsp;"
    SHIPF="&nbsp;"
    
    echo "${KEYS}" | grep "OBJECT" > /dev/null
    if test "$?" = "0"
    then
	OBJF="X"
    fi
    
    echo "${KEYS}" | grep "SHIP" > /dev/null
    if test "$?" = "0"
    then
	SHIPF="X"
    fi
    
    # Alphabetical listing
    
    echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-alpha.body
    echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-alpha.body
    

    echo "${KEYS}" | grep "SCOBJ" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-object.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-object.body
    fi
    

    echo "${KEYS}" | grep "CODING" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-coding.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-coding.body
    fi


    echo "${KEYS}" | grep "COMMS" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-comm.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-comm.body
    fi


    echo "${KEYS}" | grep "UTILITY" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-util.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-util.body
    fi


    echo "${KEYS}" | grep "NAV" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-nav.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-nav.body
    fi


    echo "${KEYS}" | grep "SHIELD" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-shield.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-shield.body
    fi


    echo "${KEYS}" | grep "DAMCON" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-damcon.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-damcon.body
    fi


    echo "${KEYS}" | grep "TRANS" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-trans.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-trans.body
    fi


    echo "${KEYS}" | grep "MISC" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-shipmisc.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-shipmisc.body
    fi


    echo "${KEYS}" | grep "ENG" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-eng.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-eng.body
    fi


    echo "${KEYS}" | grep "TAC" > /dev/null
    if test "$?" = "0"
    then
	echo "<TR><TD ROWSPAN=2><A NAME=\"#${flag}\"><B>${flag}</B>${NAME}</A></TD><TD>${RTRN}</TD><TD ALIGN=CENTER>${OBJF}</TD><TD ALIGN=CENTER>${SHIPF}</TD></TR>" >> tmp/sc-tac.body
    
	echo "<TR><TD COLSPAN=3>${DESC}</TD></TR>" >> tmp/sc-tac.body
    fi
done

for file in $CALLS
do
    echo "</TABLE></TD></TR></TABLE>" >> tmp/${file}.body
done

#-----------------------------------------------------------------------------

for file in $CALLS
do
    cat templ/header.txt templ/links-space.txt templ/title.txt tmp/${file}.body templ/footer.txt > ../docs/${file}.html
done

cat templ/header.txt templ/links-space.txt templ/title.txt src/spacecalls-index.body templ/footer.txt > ../docs/spacecalls-index.html
cp src/spacecalls-*.html ../docs/

# Debug files
cat templ/header.txt templ/links-debug.txt templ/title.txt src/debug-index.body templ/footer.txt > ../docs/debug-index.html

# Copy sample database, jpgs and gifs over

cp src/*gz   ../docs/
cp src/*.jpg ../docs/
cp src/*.gif ../docs/

rm -f tmp/*

umask 007
