# ! /bin/sh
CURDIR=$1
PACKAGES="crystalspace"
BINTYPE="bin"

#=============================================================================
gensgml(){
DATE="`date '+%m %d %Y'`"
SHORTDESC=""
SEEALSO="Web Documentation"

cat <<-FIN
<!doctype refentry PUBLIC "-//OASIS//DTD DocBook V4.1//EN" [

  <!ENTITY dhfirstname "<firstname>Christian</firstname>">
  <!ENTITY dhsurname   "<surname>Bayle</surname>">
  <!-- Please adjust the date whenever revising the manpage. -->
  <!ENTITY dhdate      "<date>$DATE</date>">
  <!ENTITY dhsection   "<manvolnum>$SECTION</manvolnum>">
  <!ENTITY dhemail     "<email>bayle@aist.enst.fr</email>">
  <!ENTITY dhusername  "Christian Bayle">
  <!ENTITY dhucpackage "<refentrytitle>$PACKAGE</refentrytitle>">
  <!ENTITY dhpackage   "$BINARYNAME">

  <!ENTITY debian      "<productname>Debian GNU/Linux</productname>">
  <!ENTITY gnu         "<acronym>GNU</acronym>">
]>

<refentry>
  <refentryinfo>
    <address>
      &dhemail;
    </address>
    <author>
      &dhfirstname;
      &dhsurname;
    </author>
    <copyright>
      <year>2001</year>
      <holder>&dhusername;</holder>
    </copyright>
    &dhdate;
  </refentryinfo>
  <refmeta>
    &dhucpackage;

    &dhsection;
  </refmeta>
  <refnamediv>
    <refname>&dhpackage;</refname>

    <refpurpose>$VERYSHORTDESC</refpurpose>
  </refnamediv>
  <refsynopsisdiv>
    <cmdsynopsis>

      <command>&dhpackage;</command>
FIN

[ "$TYPE"=="Standart" ] && gensgmlopt $1

cat <<-FIN
    </cmdsynopsis>
  </refsynopsisdiv>
  <refsect1>
    <title>DESCRIPTION</title>

    <para>This manual page documents briefly the
      <command>&dhpackage;</command> command.
    </para>

    <para>This manual page was written for the &debian; distribution
      because the original program does not have a manual page.
    </para>

    <para><command>&dhpackage;</command>$SHORTDESC</para>

  </refsect1>
  <refsect1>
    <title>OPTIONS</title>

    <para>These programs follow the usual command line syntax,
      with options starting with a dash ('-').  A summary of
      options is included below.  
    </para>

FIN

echo "<!-- TYPE=$TYPE -->"
[ "$TYPE" == "Standart" ] && gensgmloptdsc $1
[ "$TYPE" != "Standart" ] && gensgmloptnodsc $1

cat <<-FIN
  </refsect1>
  <refsect1>
    <title>SEE ALSO</title>

    <para>$SEEALSO</para>

    <para>The programs are also documented in<citetitle>
      The Crystal Space Jump-Start Page</citetitle> available 
      in the crystalspace-doc Package
    </para>
  </refsect1>
  <refsect1>
    <title>AUTHOR</title>

    <para>This manual page was written by &dhusername; &dhemail; for
      the &debian; system (but may be used by others).  Permission is
      granted to copy, distribute and/or modify this document under
      the terms of the <acronym>GNU</acronym> Free Documentation
      License, Version 1.1 or any later version published by the Free
      Software Foundation; with no Invariant Sections, no Front-Cover
      Texts and no Back-Cover Texts.</para>

  </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:2
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:nil
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->
FIN
}
#=============================================================================
gensgmlopt(){
	cat $1 | grep "^. *-" | sed 's/^. *\(-.[^ ]*\).*/\1/' | while read opt
	do
		echo -n "<arg><option>"
		case $opt in
		-*=*)
			echo -n $opt | sed 's/\(.[^=]*=\)<\(.*\)>/\1<replaceable>\2<\/replaceable>/'
			;;
		*)
			echo -n "$opt"
			;;
		esac
		echo "</option></arg>"
	done
}
#=============================================================================
gensgmloptdsc(){
	export variablelistflag="closed"
	cat $1 | while read line
	do
		case $line in
		[A-Z]*)
    			[ "$variablelistflag" != "closed" ] && echo "</variablelist>" && variablelistflag="closed"
			echo "<para>$line</para>"
			;;
		-*=*)
    			[ "$variablelistflag" != "open" ] && echo "<variablelist>" && variablelistflag="open"
	echo $line | sed 's|\(-.[^=]*=\)<\(.[^>]*\)>.[ ]*\(.*\)$|\
      <varlistentry>\
        <term>\
		<option>\1\2</option>\
        </term>\
        <listitem>\
          <para>\3</para>\
        </listitem>\
      </varlistentry>|'
			;;
		-*)
    			[ "$variablelistflag" != "open" ] && echo "<variablelist>" && variablelistflag="open"
	echo $line | sed 's|\(-.[^ ]*\).[ ]*\(.*\)$|\
      <varlistentry>\
        <term>\
		<option>\1</option>\
        </term>\
        <listitem>\
          <para>\2</para>\
        </listitem>\
      </varlistentry>|'
			;;
		esac
	done

    	#[ "$variablelistflag" != "closed" ] && echo "</variablelist>" && variablelistflag="closed"
    	echo "</variablelist>" && variablelistflag="closed"
}
#=============================================================================
gensgmloptnodsc(){
	echo "<para>"
	cat $1
	echo "</para>"
}
#=============================================================================
rm -f $CURDIR/debian/man/*.txt
#rm -f $CURDIR/debian/sgml/*.sgml*
[ ! -d $CURDIR/debian/man ] && mkdir $CURDIR/debian/man
for package in $PACKAGES
do
	for bintype in $BINTYPE
	do
		[ -d $CURDIR/debian/$package/usr/$bintype/ ] && \
		for fullpathbin in $CURDIR/debian/$package/usr/$bintype/* 
		do
			bin=`basename $fullpathbin`	
			export BINARYNAME="$bin"
			export PACKAGE=`echo $package| tr '[a-z]' '[A-Z]'`
			# SECTION=6 Games
			# SECTION=1 Executable programs or shell commands
			case $bintype in
				bin)
					export SECTION=1
					export VERYSHORTDESC="Crystalspace tool"
					;;
				games)
					export SECTION=6
					export VERYSHORTDESC="Crystalspace demonstration program"
					;;
			esac
			echo "Generating man for $bin	section ($SECTION)	package $PACKAGE"
			cd $CURDIR
			export CRYSTAL=$CURDIR/CS
			case $bin in
				vsh|cszoo)
					export TYPE="No option"
					echo "There is no option for this program" > $CURDIR/debian/man/$bin.txt
					gensgml $CURDIR/debian/man/$bin.txt \
					2>/dev/null \
					> $CURDIR/debian/man/$bin.sgml.$SECTION
					;;
				cs-config|mdl2spr|partedit|md22spr|md32spr|3ds2lev|makedep|csfgen|map2cs|cspicview|nettut|unittest|phystut|levtool|maya2spr|milk2spr|python.cex|csbench|docconv|csdemo|lighter|csstartme|genmeshify|partconv)
					export TYPE="Not standart"
					echo "Not yet documented" > $CURDIR/debian/man/$bin.txt
					gensgml $CURDIR/debian/man/$bin.txt \
					2>/dev/null \
					> $CURDIR/debian/man/$bin.sgml.$SECTION
					;;
				cslight|walktest)
					#dirty hack because of font message
					export TYPE="Standart"
					cd CS; ./$bin -video=null -help | grep -v "^Font f" \
					> $CURDIR/debian/man/$bin.txt
					gensgml $CURDIR/debian/man/$bin.txt \
					2>/dev/null \
					> $CURDIR/debian/man/$bin.sgml.$SECTION
				;;

				*)
					export TYPE="Standart"
					cd CS; ./$bin -help \
					> $CURDIR/debian/man/$bin.txt
					gensgml $CURDIR/debian/man/$bin.txt \
					2>/dev/null \
					> $CURDIR/debian/man/$bin.sgml.$SECTION
				;;
			esac
		done
	done
done
echo "Man generated"
		
