#  Root Makefile for the Haskell Ports Library
#
#  Author : Manuel M. T. Chakravarty
#  Derived: 24 May 2000
#
#  Version $Revision: 1.7 $ from $Date: 2001/06/29 08:29:44 $
#
#  Copyright (c) 2000 Manuel M. T. Chakravarty
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Library General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Library General Public License for more details.
#
#  = DOCU =====================================================================
#
#  * Derived from the corresponding Gtk+HS file.
#

#  ***************************************
#  !!! This makefile requires GNU make !!!
#  ***************************************

.PHONY: default
default: all

include mk/config.mk mk/common.mk

# directories
#
LIBDIRS  =lib
LIBEXDIRS=$(LIBDIRS) tests

# files lists
#
CODEFILES=$(wildcard lib/*) $(wildcard tests/*)
HSFILES  =$(filter %.hs %/Makefile %.hs.in, $(CODEFILES))
DOCFILES =$(wildcard doc/*)
SGMLFILES=$(filter %.sgml %/Makefile, $(DOCFILES))
TARFILES =AUTHORS COPYING.LIB ChangeLog INSTALL NEWS README TODO\
	  Makefile configure.in configure config.guess config.sub install-sh\
	  ports-config.in ports.spec.in\
	  $(filter-out mk/config.mk, $(filter %.mk %.mk.in, $(wildcard mk/*)))\
	  $(filter-out lib/PortsConfig.hs, $(HSFILES))\
	  $(SGMLFILES)

# files that contain a `versnum = "x.y.z"' line
#
VERSFILE =lib/PortsVersion.hs

# this is far from elegant, but extracts the plain version number
#
VERSION =$(shell $(GREP) '^versnum' $(VERSFILE)\
		 | $(SED) '-e s/versnum.* "//' '-e s/"//')

# base directory for tar balls and exclude patterns
#
TARBASE=ports
TAREXCL=--exclude='*/CVS*' --exclude='*~'


.PHONY: prep
prep:
	@echo "Entering preparation phase..."
	@for dir in $(LIBDIRS); do\
	  $(MAKE) -C $$dir $@ || exit;\
	done

.PHONY: all
all: prep
	@echo "Entering build phase..."
	$(MAKE) -C lib libportsHS.a

.PHONY: ghci
ghci: prep
	@echo "Starting interactive session..."
	$(MAKE) -C lib ghci

.PHONY: install
install: all
	@echo "Entering installation phase..."
	$(INSTALL_PROGRAM) ports-config $(DESTDIR)$(bindir)/ports-config
	$(INSTALL) -d $(DESTDIR)$(pkglibdir)
	$(INSTALL_DATA) lib/libportsHS.a $(DESTDIR)$(pkglibdir)/libportsHS.a
	$(INSTALL) -d $(DESTDIR)$(pkgimportdir)
	@for hi in lib/*.hi; do\
	  echo $(INSTALL_DATA) $$hi\
	       $(DESTDIR)$(pkgimportdir)/`$(BASENAME) $$hi`;\
	  $(INSTALL_DATA) $$hi $(DESTDIR)$(pkgimportdir)/`$(BASENAME) $$hi`;\
	done

.PHONY: doc
doc:
	make -C doc all

.PHONY: install-doc
install-doc: doc
	make -C doc install

.PHONY: clean spotless
clean:
	@for dir in $(LIBEXDIRS) $(EXTDIRS); do\
	  $(MAKE) -C $$dir $@ || exit;\
	done
spotless:
	@for dir in $(LIBEXDIRS) $(EXTDIRS); do\
	  $(MAKE) -C $$dir $@ || exit;\
	done
	-$(RM) config.cache

# tar a distribution
#
TARCMD=$(TAR) -c -z $(TAREXCL) -h -f
.PHONY: tar
tar:
	-$(LN) -s . $(TARBASE)-$(VERSION)
	$(TARCMD) $(TARBASE)-$(VERSION).tar.gz\
	  $(addprefix $(TARBASE)-$(VERSION)/,$(TARFILES))
	$(RM) $(TARBASE)-$(VERSION)
