# -----------------------------------------------------------------------------

TOP=.
include $(TOP)/mk/boilerplate.mk

# -----------------------------------------------------------------------------

SUBDIRS = base haskell98 template-haskell

ifeq "$(GhcLibsWithUnix)" "YES"
SUBDIRS += unix
endif

SUBDIRS += Cabal
SUBDIRS += $(wildcard parsec)

# Set GhcBootLibs=YES from the command line to work with just the libraries
# needed to bootstrap GHC.
ifneq "$(GhcBootLibs)" "YES"
SUBDIRS += $(wildcard haskell-src)
SUBDIRS += $(wildcard network)
SUBDIRS += $(wildcard QuickCheck)
SUBDIRS += $(wildcard HUnit)
SUBDIRS += $(wildcard mtl)
SUBDIRS += $(wildcard fgl)
SUBDIRS += $(wildcard X11)
ifeq "$(Windows)" "YES"
SUBDIRS += Win32
endif
SUBDIRS += $(wildcard HGL)
SUBDIRS += $(wildcard OpenGL)
SUBDIRS += $(wildcard GLUT)
SUBDIRS += $(wildcard OpenAL)
SUBDIRS += $(wildcard stm)
ifeq "$(GhcLibsWithObjectIO)" "YES"
SUBDIRS += ObjectIO
endif
endif

ifeq "$(GhcLibsWithReadline)" "YES"
SUBDIRS += readline
endif

# -----------------------------------------------------------------------------

DIST_CLEAN_FILES += config.cache config.status

include $(TOP)/mk/target.mk

# -----------------------------------------------------------------------------
# Generating the combined contents/index pages for the library docs

ifneq "$(NO_HADDOCK_DOCS)" "YES"

HTML_DIR = html

# ATTENTION, incomprehensible shell stuff ahead: Automagically create the
# prologue for the combined index via a header, the package prologues (in
# alphabetical order of the packages) and a footer. Not very nice, but much
# better than redundancy or a strong coupling with the packages.
libraries.txt: libraries-header.txt libraries-footer.txt $(foreach f,package.conf.in prologue.txt,$(addsuffix /$(f),$(SUBDIRS)))
	$(RM) $@
	( cat libraries-header.txt ; echo ; \
	for i in `for j in $(SUBDIRS) ; do echo $$j ; done | $(SORT) -f` ; do \
	   if test -f $$i/$$i.haddock; then \
	      echo "[@$$i@]"; \
	      grep -v '^ *$$' $$i/prologue.txt; \
	      echo; \
	   fi; \
	done ; \
	cat libraries-footer.txt ; echo ) > $@

CLEAN_FILES += libraries.txt

$(HTML_DIR)/index.html : libraries.txt
	@$(INSTALL_DIR) $(HTML_DIR)
	$(HADDOCK) --gen-index --gen-contents -o $(HTML_DIR) \
		$(HADDOCK_OPTS) \
		-t "Haskell Hierarchical Libraries" \
		-p libraries.txt \
		$(foreach pkg, $(filter-out haskell98,$(SUBDIRS)), \
		   $(foreach p, $(wildcard $(pkg)/$(pkg).haddock), \
		      --read-interface=$(pkg),$(pkg)/$(pkg).haddock))

html :: $(HTML_DIR)/index.html

extraclean ::
	$(RM) -rf $(HTML_DIR)

install-docs :: $(HTML_DIR)/index.html
	@$(INSTALL_DIR) $(datadir)/html/libraries/
	@for i in $(HTML_DIR)/*; do \
	   echo $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(datadir)/html/libraries; \
	   $(INSTALL_DATA) $(INSTALL_OPTS) $$i $(datadir)/html/libraries; \
	done

endif # NO_HADDOCK_DOCS
