#!@SHELL@

# Doxygen's man page generation is unfortunately very rudimentary.
#
# So in addition to creating the libnet(3) man page from pod file, this
# script also tries to add a few "finishing touches" to man pages
# generated by Doxygen.
#
# It will run after building libnet and after Doxygen generated all its
# documentations. If you ever have to run it manually, you should do the same.
# Apart from a few widely available GNU utilities, you'll also need pod2man
# (aka libpod-latex-perl) to get it to work.

MANPAGENOTE='.SH "SEE ALSO"\n\.IX Header "SEE ALSO"\nlibnet(3)\n.SH "AUTHORS"\n.IX Header "AUTHORS"\nThe original author of libnet is Mike D. Schiffman.\n.PP\nlibnet has been maintained and extensively enhanced since 2009 by Sam Roberts.\n.PP\nIt is maintained by the Libnet Developer Community at GitHub.\n.PP\n.Vb 1\n\&        <https://github.com/libnet/libnet>\n.Ve\n.SH "BUGS"\n.IX Header "BUGS"\nSuggestions, bug reports/fixes, and pull requests, see:\n.PP\n.Vb 1\n\&        <https://github.com/libnet/libnet/issues>\n.Ve\n.SH "COPYRIGHT"\n.IX Header "COPYRIGHT"\nlibnet is licensed under the 2\-Clause \s-1BSD\s0 License.'

die() {
    echo "$*" 1>&2
    exit 1
}

MANDIR=$1
if test x"$MANDIR" = x ; then
    MANDIR=man/
fi

test -d $MANDIR/man3 || die "Could not locate $MANDIR/man3 directory."

# For now, I will let Doxygen still produces its useless and totally misleading
# man page for "libnet.h" and simply delete it now. That's mostly because not
# doing so will also remove all references to "libnet.h" in all documentations.
# And we wouldn't want that, would we?
find $MANDIR/man3/ -name "libnet.h.3" -exec sh -c 'rm -f "$1"' _ {} \;

# Let's create libnet.3 before dealing with the rest.
# pod2man's ISO 8601 date format is fine, even if it differs from doxygen's

pod2man -n LIBNET -c "libnet Programmers Guide" -s 3 -r "@PACKAGE_NAME@-@PACKAGE_VERSION@" @top_srcdir@/doc/libnet.Pod man/man3/libnet.3 || die "Could not create libnet.3 in $MANDIR/man/man3."

# pod2html --title="libnet Programmers Guide" --noindex --infile=libnet.Pod --outfile=libnet.html

cd "$MANDIR/man3" || die "Could not cd into $MANDIR/man3"

# A little housekeeping...
(find . -name "_*" -exec sh -c 'rm "$1"' _ {} \;) || die "could not remove all underscore-prefixed items"

# renaming all man pages form "*.h.3" to just "*.3"
(find . -name "*.h.3" -exec sh -c 'mv "$1" "${1%.h.3}.3"' _ {} \;) || die "could not rename all .h.3-suffixed items"

# Changing the name of the man pages to all UPPERCASE.
# FIXME make Doxygen properly generate docs for all other
# libnet-*.h files and add them here
sed -i -e '1!b;s/libnet\/libnet-functions\.h/LIBNET-FUNCTIONS/' libnet-functions.3
sed -i -e '1!b;s/libnet\/libnet-macros\.h/LIBNET-MACROS/' libnet-macros.3

# Adjusting version string, changing the title, removing both the path and
# the ".h" extension from name section as well as adding a few more sections
# to the end.
for MAN in $(find . -name "libnet-*.3"); do
    sed -i -e '1!b;s/Version\ /libnet-/' \
           -e '1!b;s/"libnet"/"libnet\ Programmers \Guide"/' $MAN
    sed -i -e '5 s/libnet\///' \
           -e '/.SH "Author"/,/from the source code\&./c \nBe' $MAN
    echo -e $MANPAGENOTE >> $MAN
done
