#!/bin/sh

set -e

HADDOCK_ARGS=
NAMES=

case $* in
--inplace)
    HADDOCK=../utils/haddock/install-inplace/bin/haddock
    HADDOCK_FILES=`ls -1 */dist/doc/html/*/*.haddock | sort`
    if [ -d dph ]
    then
        HADDOCK_FILES="$HADDOCK_FILES `ls -1 dph/*/dist/doc/html/*/*.haddock | sort`"
    fi
    ;;
*)
    HADDOCK=../../../../bin/haddock
    # We don't want the GHC API to swamp the index
    HADDOCK_FILES=`ls -1 */*.haddock | grep -v "ghc/" | sort`
    ;;
esac

for HADDOCK_FILE in $HADDOCK_FILES
do
    NAME=` echo "$HADDOCK_FILE" | sed 's#.*/##' | sed 's#\.haddock$##' `
    HADDOCK_ARGS="$HADDOCK_ARGS --read-interface=$NAME,$HADDOCK_FILE"
    NAMES="$NAMES $NAME"
done

# Now create the combined contents and index pages
$HADDOCK --gen-index --gen-contents -o . \
         -t "Haskell Hierarchical Libraries" \
         -p "prologue.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

