#!/bin/sh

set -e

HADDOCK_ARGS=
NAMES=

case $* in
--inplace)
    GHC_PKG=../utils/ghc-pkg/ghc-pkg-inplace
    for DIR in */
    do
        NAME=${DIR%/}
        HADDOCK_FILE=$NAME/dist/doc/html/$NAME/$NAME.haddock
        if [ -f $HADDOCK_FILE ]
        then
            FULLNAME=$NAME-`$GHC_PKG field $NAME version | sed 's#^version: *##'` 
            HADDOCK_ARGS="$HADDOCK_ARGS --read-interface=$FULLNAME,$HADDOCK_FILE"
            NAMES="$NAMES $NAME"
        fi
    done
    ;;
*)
    HADDOCK_FILES=`ls -1 */*.haddock | sort`
    for HADDOCK_FILE in $HADDOCK_FILES
    do
        NAME=`echo "$HADDOCK_FILE" | sed "s#/.*##"`
        HADDOCK_ARGS="$HADDOCK_ARGS --read-interface=$NAME,$HADDOCK_FILE"
        NAMES="$NAMES $NAME"
    done
    ;;
esac

# Automagically create the prologue for the combined index via a
# header, the package prologues (in alphabetical order of the
# packages) and a footer.
{
    cat libraries-header.txt
    echo
    # Hack to find out if we're in a build tree or installed docs
    for NAME in $NAMES
    do
        if [ "$NAME" != haskell98 ]
        then
            echo "[@${NAME}@]"
            grep -v '^ *$$' "$NAME"/prologue.txt
            echo
        fi
    done
    cat libraries-footer.txt
    echo
} > libraries.txt

# Now create the combined contents and index pages
haddock --gen-index --gen-contents -o . \
        -t "Haskell Hierarchical Libraries" \
        -p libraries.txt \
        $HADDOCK_ARGS

# Unhandled Windows help stuff?:

#libraries.HxS : libraries.txt
#	 haddock ...
#		-k libraries
#		--html-help=mshelp2
#	( cd $(HTML_DIR) && if Hxcomp -p libraries.HxC -o ../$@ ; then false ; else true ; fi ) || true
#
#libraries.chm : libraries.txt
#	haddock ...
#		-k libraries \
#		--html-help=mshelp \
#	( cd $(HTML_DIR) && if hhc libraries.hhp ; then false ; else true ; fi && mv libraries.chm .. ) || true

