#!/bin/bash

set -e

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

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

cd lib/pegasus/perl/
for i in Pegasus/DAX/*.pm; do 
    pod2html --htmldir /tmp/xxx --podroot $PWD --podpath Pegasus/DAX \
             --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"

