#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess.
#
# This version is for a multibinary package. It also allows you to build any
# of the binary packages independantly, via binary-<package> targets.

# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

include /usr/share/quilt/quilt.make

# From /usr/share/doc/autotools-dev/examples/rules.gz:
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

confflags += --enable-bootstrap-with-devel-snapshot --enable-shared

ProjectVersion=$(shell awk -F\' "/^PACKAGE_VERSION='[0-9.]+'\$$/ { print \$$2 }" configure)

GHC=$(firstword $(shell bash -c "type -p ghc6 ghc"))
EXTRA_CONFIGURE_FLAGS=--with-ghc="$(GHC)"

configure: configure-stamp
configure-stamp: debian/stamp-patched
	dh_testdir

	touch configure

	rm -f mk/build.mk
	echo "XMLDocWays := html" >> mk/build.mk
	echo "HADDOCK_DOCS := YES" >> mk/build.mk
	echo "XSLTPROC_OPTS += --nonet" >> mk/build.mk
ifeq (,$(findstring $(DEB_HOST_ARCH), i386 amd64 kfreebsd-i386 kfreebsd-amd64 powerpc lpia))
	echo "GhcUnregisterised=YES" >> mk/build.mk
else
	echo "GhcUnregisterised=NO" >> mk/build.mk
endif
ifeq (powerpc,$(DEB_HOST_ARCH))
	echo "GhcWithInterpreter=NO" >> mk/build.mk
endif
ifneq (,$(findstring $(DEB_HOST_ARCH), hppa ia64)) # Doesn't fix the problem :-(
	echo "GhcWithInterpreter=NO" >> mk/build.mk
	echo "SplitObjs=NO" >> mk/build.mk
	echo "GhcThreaded=NO" >> mk/build.mk
	echo "GhcDebugged=YES" >> mk/build.mk
endif
ifneq (,$(findstring $(DEB_HOST_ARCH), arm armel))
	echo "SRC_HC_OPTS     += -optc-mlong-calls" >> mk/build.mk
	echo "UseLibFFIForAdjustors=YES" >> mk/build.mk
endif
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	echo "SRC_HC_OPTS   += -H32m -O0" >> mk/build.mk
	echo "GhcHcOpts     += -Rghc-timing -DDEBUG" >> mk/build.mk
	echo "GhcLibHcOpts  += -O -dcore-lint -keep-hc-files " >> mk/build.mk
	echo "SplitObjs     = NO" >> mk/build.mk
	echo "STRIP         = :" >> mk/build.mk
endif
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	echo "SRC_HC_OPTS   += -H32m -O0" >> mk/build.mk
	echo "GhcHcOpts     += -O0" >> mk/build.mk
	# This breaks the build - to be investigated:
	# echo "GhcLibHcOpts  += -O0" >> mk/build.mk
	echo "GhcRtsCcOpts  += -O0" >> mk/build.mk
endif
	# We can't do this with a configure flag in 6.8.1 as libdir is not
	# defined at the point at which we := it
	echo 'bindir  := $${libdir}/bin' >> mk/build.mk
	# docdir doesn't have a configure flag, and unfortunately
	# we also need to explicitly define all of its dependents as they
	# are set with :=
	echo 'docdir  := $$(datarootdir)/doc/ghc6-doc' >> mk/build.mk
	echo 'htmldir := $$(docdir)'                   >> mk/build.mk
	echo 'dvidir  := $$(docdir)'                   >> mk/build.mk
	echo 'pdfdir  := $$(docdir)'                   >> mk/build.mk
	echo 'psdir   := $$(docdir)'                   >> mk/build.mk
	rm -f config.sub
	rm -f config.guess
	ln -s /usr/share/misc/config.sub .
	ln -s /usr/share/misc/config.guess .
	./configure $(confflags) --prefix=/usr $(EXTRA_CONFIGURE_FLAGS)

	touch $@

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	sh debian/watcher.sh "$$PPID" "`pwd`" "`pwd`/build-stamp" 'ghc\|gcc' &
	$(MAKE)

	# Do some very simple tests that the compiler actually works
	rm -rf debian/testghc
	mkdir debian/testghc
	echo 'main = putStrLn "Foo"' > debian/testghc/foo.hs
	inplace/bin/ghc-stage2 debian/testghc/foo.hs -o debian/testghc/foo
	[ "$$(debian/testghc/foo)" = "Foo" ]
	rm debian/testghc/*
	echo 'main = putStrLn "Foo"' > debian/testghc/foo.hs
	inplace/bin/ghc-stage2 debian/testghc/foo.hs -o debian/testghc/foo -O2
	[ "$$(debian/testghc/foo)" = "Foo" ]
	rm debian/testghc/*

	touch $@

FILES = \( -type f -o -type l \)
PROF_FILE = \( -name "*.p_*" -o -name "lib*_p.a" \)

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs /var/lib/ghc-$(ProjectVersion)/package.conf.d

	# Install the basic stuff
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install_docs

	# Delete all the library LICENSE files
	rm debian/tmp/usr/share/doc/ghc6-doc/html/libraries/*/LICENSE
	#rm debian/tmp/usr/share/doc/ghc6-doc/LICENSE

	# Generate lintian overrides
	mkdir -p debian/tmp/usr/share/lintian/overrides
	echo "ghc6 binary: extra-license-file `cd debian/tmp && echo usr/lib/ghc-*/Cabal-*/Distribution/License.hi`" >> debian/tmp/usr/share/lintian/overrides/ghc6
	echo "ghc6-prof binary: extra-license-file `cd debian/tmp && echo usr/lib/ghc-*/Cabal-*/Distribution/License.p_hi`" >> debian/tmp/usr/share/lintian/overrides/ghc6-prof

	# Sort out the package.conf files
	#mv debian/tmp/usr/lib/ghc-$(ProjectVersion)/package.conf \
	#   debian/tmp/usr/lib/ghc-$(ProjectVersion)/package.conf.shipped
	#ln -s debian/tmp/var/lib/ghc-$(ProjectVersion)/package.conf \
	#      debian/tmp/usr/lib/ghc-$(ProjectVersion)/package.conf
	mkdir -p debian/tmp/var/lib/ghc-$(ProjectVersion)
	mv debian/tmp/usr/lib/ghc-$(ProjectVersion)/package.conf.d \
	       debian/tmp/var/lib/ghc-$(ProjectVersion)/package.conf.d
	ln -s debian/tmp/var/lib/ghc-$(ProjectVersion)/package.conf.d \
	      debian/tmp/usr/lib/ghc-$(ProjectVersion)/package.conf.d
	#chmod +x debian/provided_substvars
	#debian/provided_substvars
#	mkdir -p debian/tmp/ghc

	# Sort out the binaries
	mkdir -p debian/tmp/usr/bin
	cd debian/tmp/usr/lib && \
	    if bin/ghc --info | grep '"Have interpreter","NO"'; \
		then rm -f bin/ghci* bin/runghc* bin/runhaskell*; \
	    fi
	cd debian/tmp/usr/lib/bin && \
	    for f in *; do ln -s ../lib/bin/$$f ../../bin/$$f; \
	done
#	    for f in *-$(ProjectVersion); \
#	    do ln -s /usr/lib/bin/$$f \
#	             ../../bin/; \
#	    done
#	cd debian/tmp/usr/lib/bin && \
#	    for f in *; \
#	    do ln -s /usr/lib/bin/$$f \
#	             ../../bin/$${f}6; \
#	    done
#	rm -f debian/tmp/usr/bin/*-$(ProjectVersion)6
#	rm -f debian/tmp/usr/bin/runhaskell6
#	sed -i 's,=/usr/lib,=/var/lib,' debian/tmp/usr/lib/bin/ghc-pkg

	ProjectVersion=$(ProjectVersion) $(MAKE) -f debian/scripts.mk all

#ifeq (YES,$(BUILD_HADDOCK_DOCS))
	mkdir -p debian/tmp/usr/lib/ghc6-doc
	cp debian/gen_contents_index debian/tmp/usr/lib/ghc6-doc/
	chmod +x debian/tmp/usr/lib/ghc6-doc/gen_contents_index
	mkdir -p debian/tmp/usr/share/ghc6-doc/ghc-$(ProjectVersion)/desc/
	mkdir debian/tmp/usr/share/ghc6-doc/ghc-$(ProjectVersion)/haddock/
	inplace/bin/ghc-pkg dump |\
	    grep -E '^(haddock-|name|version|---)' > debian/tmp/usr/share/ghc6-doc/ghc-$(ProjectVersion)/desc/ghc6-doc
	echo "/usr/share/doc/ghc6-doc/html /usr/lib/ghc-$(ProjectVersion)/html" > debian/ghc6-doc.links
	echo "/usr/share/doc/ghc6-doc/html /var/lib/ghc-$(ProjectVersion)/html" >> debian/ghc6-doc.links
	# This is stupid, but if it saves me one build its worth it.
	[ -d debian/tmp/usr/share/doc/ghc6-doc/html/html ] && \
	  { for f in debian/tmp/usr/share/doc/ghc6-doc/html/html/*; do mv $$f debian/tmp/usr/share/doc/ghc6-doc/html; done; \
	    rmdir debian/tmp/usr/share/doc/ghc6-doc/html/html; } || true
	mkdir -p debian/tmp/usr/share/doc/ghc6-doc/libraries
	cp libraries/prologue.txt debian/tmp/usr/share/doc/ghc6-doc/libraries/
#endif

	# manpages
	#echo ".so man1/ghc6.1" > debian/ghc-$(ProjectVersion).1
	#if test -e debian/tmp/usr/bin/ghci-$(ProjectVersion); then \
	#    for m in ghci6 ghci-$(ProjectVersion); do echo ".so man1/ghc6.1" > debian/$$m.1; done; \
	#    cp debian/runghc.man debian/runghc6.1; fi
	#mv debian/tmp/usr/share/man/man1/ghc.1 debian/ghc6.1
	#cp utils/hp2ps/hp2ps.1 debian/hp2ps-ghc6.1
	#cp debian/ghc-pkg6.man debian/ghc-pkg6.1
	#
	#echo debian/*.1 > debian/ghc6.manpages

	# ####################
	# Now all the files are sorted, create the package filelists

	# ghc6
	find debian/tmp/usr/bin $(FILES)                  > debian/ghc6.install
	find debian/tmp/usr/lib/bin $(FILES)              >> debian/ghc6.install
#	find debian/tmp/usr/share/ghc* $(FILES)          >> debian/ghc6.install
	#find debian/tmp/usr/share/man $(FILES)           >> debian/ghc6.install
	find debian/tmp/usr/lib/ghc-$(ProjectVersion) $(FILES) ! $(PROF_FILE)  >> debian/ghc6.install
	find debian/tmp/var/lib/ghc-$(ProjectVersion) $(FILES) ! $(PROF_FILE)  >> debian/ghc6.install
	echo debian/tmp/usr/share/lintian/overrides/ghc6 >> debian/ghc6.install
	# ghc6-prof
	find debian/tmp/usr/lib $(FILES) $(PROF_FILE) > debian/ghc6-prof.install
	echo debian/tmp/usr/share/lintian/overrides/ghc6-prof >> debian/ghc6-prof.install
	# ghc6-doc
	find debian/tmp/usr/share/man    $(FILES)  > debian/ghc6-doc.install
#ifeq (YES,$(BUILD_HADDOCK_DOCS))
	find debian/tmp/usr/share/doc    $(FILES)  >> debian/ghc6-doc.install
	find debian/tmp/usr/share/ghc6-doc    $(FILES)  >> debian/ghc6-doc.install
	find debian/tmp/usr/lib/ghc6-doc $(FILES) >> debian/ghc6-doc.install
#endif
	sed -i s,^debian/tmp,, debian/*.install
	echo "/var/lib/ghc-$(ProjectVersion)/package.conf /usr/lib/ghc-$(ProjectVersion)/package.conf" > debian/ghc6.links
	echo "/var/lib/ghc-$(ProjectVersion)/package.conf.d /usr/lib/ghc-$(ProjectVersion)/package.conf.d" >> debian/ghc6.links
	for x in ghc ghci ghc-pkg hp2ps hpc hsc2hs runghc; do \
	  echo "/usr/bin/$${x}" "/usr/bin/$${x}6" >> debian/ghc6.links; \
	done
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f configure-stamp build-stamp install-stamp binary-common-stamp

	touch configure
	
	[ ! -f mk/config.mk ] || $(MAKE) distclean
	rm -f config.sub config.guess
	rm -f debian/*.install
	rm -f debian/*.1 debian/ghc-pkg6.man
	rm -f debian/ghc6.manpages
	rm -f mk/build.mk
	$(MAKE) -f debian/scripts.mk clean
	rm -rf debian/testghc

	# Hacks:
	rm -f compiler/stage1/ghc_boot_platform.h
	rm -f compiler/stage2/ghc_boot_platform.h
	rm -f ghc/inplace-datadir/package.conf

	rm -f utils/pwd/pwd utils/pwd/pwd.hi utils/pwd/pwd.o
	rm -f config.log
	rm -f libraries/ghc-prim/GHC/PrimopWrappers.hs
	rm -f libraries/ghc-prim/GHC/Prim.hs
	rm -f libraries/frames.html libraries/index-frames.html

	rm -rf .pc
	rm -f libraries/haskeline/a.out

	dh_clean

binary-common: binary-common-stamp
binary-common-stamp: install-stamp
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_install --fail-missing
	dh_installmenu
	dh_installman
	dh_strip
	dh_link
	dh_compress -X.haddock
	dh_fixperms
	dh_installdeb
	dh_shlibdeps -Xusr/lib/ghc-
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	
	touch $@

# binary-indep and binary-arch both build everything, as otherwise
# "dh_install --fail-missing" falls over

# Build architecture independant packages using the common target.
binary-indep: binary-common-stamp

# Build architecture dependant packages using the common target.
binary-arch: binary-common-stamp

binary: binary-indep binary-arch
.PHONY: build install clean binary-indep binary-arch binary-common binary patch
