#!/bin/bash

set -e

TARGET_DIR=$1
if [ "x$TARGET_DIR" = "x" ]; then
    echo "Please specify a target dir as first argument"
    echo "Example: perl-doc dist/pegasus-3.1/docs/html/perl"
    exit 1
fi

mkdir -p $TARGET_DIR/Pegasus/DAX
TARGET_DIR=`cd $TARGET_DIR && pwd`

PODPATH="Pegasus/DAX"
if grep -e "^8" /etc/debian_version >/dev/null 2>&1; then
    PODPATH=":Pegasus:DAX"
fi

cd lib/pegasus/perl/
for i in Pegasus/DAX/*.pm; do 
    pod2html --htmldir /tmp/xxx --podroot $PWD --podpath $PODPATH \
             --infile $i --outfile $TARGET_DIR/`basename $i .pm`.html
done

# flatten the web space
cd $TARGET_DIR
perl -p -i -e 's:href="/Pegasus/DAX/:href=":g' *.html

# remove references to "manpages"
perl -p -i -e 's/the ([\w:]+) manpage/\1/g' *.html

# provide a nice index.html
cp $TARGET_DIR/Factory.html $TARGET_DIR/index.html

echo "Perl documentation successfully generated"

