# -*- mode: makefile -*-
# Copyright 2008 Kari Pahula <kaol@debian.org>
# Description: A class for Haskell library packages
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or (at
# your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.

_cdbs_scripts_path ?= /usr/lib/cdbs
_cdbs_rules_path ?= /usr/share/cdbs/1/rules
_cdbs_class_path ?= /usr/share/cdbs/1/class

CABAL_PACKAGE = $(shell cat *.cabal |\
 perl -ne 'if (/^name:\s*(.*)$$/i) {$$_ = $$1; tr/A-Z/a-z/; print; exit 0;}')
CABAL_EXECS = $(shell sed 's/\r$$//' *.cabal | sed -n 's/^[Ee]xecutable[ \t:][ \t:]*\(.*\)\s*/\1/p')
GHCVERSION = $(shell ghc --version | sed 's/^.*version \([0-9\.]*\)$$/\1/')
DOCDIR = $(shell dpkg --compare-versions $(GHCVERSION) ge 6.10 && echo /usr/share/doc/ghc || echo /usr/share/doc/ghc6-doc)

ENABLE_PROFILING = $(shell egrep -qe '^Package: libghc6-.*-prof$$' debian/control && echo --enable-library-profiling; exit 0)

DEB_COMPRESS_EXCLUDE += .haddock

include $(_cdbs_rules_path)/debhelper.mk$(_cdbs_makefile_suffix)

# TODO:
# - some of this would probably be useful for generic Haskell programs,
#   not just libraries
# - provide more hooks
# - get this included in the cdbs package once this gets mature enough (maybe?)

# For now, you can find the newest version in
# http://people.debian.org/~kaol/repos/hlibrary/

clean::
	test ! -e setup-bin || ./setup-bin clean
	rm -rf dist dist-ghc6 dist-hugs setup-bin Setup.hi Setup.ho Setup.o .*config*
	rm -f build-ghc6-stamp build-hugs-stamp

setup-bin:
	if test ! -e Setup.lhs -a ! -e Setup.hs; then echo "No setup script found!"; exit 1; fi
	for setup in Setup.lhs Setup.hs; do if test -e $$setup; then ghc6 -package Cabal $$setup -o setup-bin; exit 0; fi; done

dist-ghc6: setup-bin
	./setup-bin configure --ghc --prefix=/usr/lib/haskell-packages/ghc6 -v2 $(ENABLE_PROFILING) $(DEB_SETUP_GHC6_CONFIGURE_ARGS)
	mv dist dist-ghc6

build-ghc6-stamp: dist-ghc6
	mv dist-ghc6 dist
	./setup-bin build
	mv dist dist-ghc6
	touch build-ghc6-stamp

build/libghc6-$(CABAL_PACKAGE)-prof build/libghc6-$(CABAL_PACKAGE)-dev:: build-ghc6-stamp

# Make sure we get a build even when the package has no library
$(patsubst %, build/%, $(CABAL_EXECS)):: build-ghc6-stamp

# Provide two alternate names for the -doc package
build/haskell-$(CABAL_PACKAGE)-doc build/libghc6-$(CABAL_PACKAGE)-doc:: dist-ghc6
	mv dist-ghc6 dist
# The build target may be called by buildds without having the
# build-depends-indep installed
	[ ! -x /usr/bin/haddock ] || ./setup-bin haddock
	mv dist dist-ghc6

dist-hugs: setup-bin
	./setup-bin configure --hugs --prefix=/usr -v2
	mv dist dist-hugs

build/libhugs-$(CABAL_PACKAGE):: dist-hugs
	mv dist-hugs dist
	./setup-bin build $(DEB_SETUP_HUGS_CONFIGURE_ARGS)
	mv dist dist-hugs

install/libghc6-$(CABAL_PACKAGE)-dev:: setup-bin
	mv dist-ghc6 dist
	./setup-bin copy --destdir=debian/libghc6-$(CABAL_PACKAGE)-dev
	find debian/libghc6-$(CABAL_PACKAGE)-dev/usr/lib/haskell-packages/ghc6/lib/ -name "*_p.a" -exec rm '{}' ';'
	find debian/libghc6-$(CABAL_PACKAGE)-dev/usr/lib/haskell-packages/ghc6/lib/ -name "*.p_hi" -exec rm '{}' ';'
	rm -rf debian/libghc6-$(CABAL_PACKAGE)-dev/usr/share/doc/*
	rm -rf debian/libghc6-$(CABAL_PACKAGE)-dev/usr/lib/haskell-packages/ghc6/bin/
	dh_haskell_prep -plibghc6-$(CABAL_PACKAGE)-dev
	cp dist/installed-pkg-config debian/libghc6-$(CABAL_PACKAGE)-dev/usr/lib/haskell-packages/ghc6/lib/*/
	rm -rf debian/libghc6-$(CABAL_PACKAGE)-dev/usr/lib/haskell-packages/ghc6/share/
	dh_haskell_depends -plibghc6-$(CABAL_PACKAGE)-dev
	# Update the substvar file
	[ ! -x /usr/bin/cabal-debian ] || cabal-debian --substvar Dev
	mv dist dist-ghc6

install/libghc6-$(CABAL_PACKAGE)-prof:: setup-bin install/libghc6-$(CABAL_PACKAGE)-dev
	mv dist-ghc6 dist
	./setup-bin copy --destdir=debian/libghc6-$(CABAL_PACKAGE)-prof
	find debian/libghc6-$(CABAL_PACKAGE)-prof/usr/lib/haskell-packages/ghc6/lib/ -name "*[^p].a" -exec rm '{}' ';'
	find debian/libghc6-$(CABAL_PACKAGE)-prof/usr/lib/haskell-packages/ghc6/lib/ -name "*.o" -exec rm '{}' ';'
	find debian/libghc6-$(CABAL_PACKAGE)-prof/usr/lib/haskell-packages/ghc6/lib/ -name "*.hi" -exec rm '{}' ';'
	rm -rf debian/libghc6-$(CABAL_PACKAGE)-prof/usr/share/doc/*
	rm -rf debian/libghc6-$(CABAL_PACKAGE)-prof/usr/lib/haskell-packages/ghc6/bin/
	dh_haskell_prep -plibghc6-$(CABAL_PACKAGE)-prof
	rm -rf debian/libghc6-$(CABAL_PACKAGE)-prof/usr/lib/haskell-packages/ghc6/share/
	dh_haskell_depends -plibghc6-$(CABAL_PACKAGE)-prof
	# Update the substvar file
	[ ! -x /usr/bin/cabal-debian ] || cabal-debian --substvar Prof
	mv dist dist-ghc6

install/haskell-$(CABAL_PACKAGE)-doc install/libghc6-$(CABAL_PACKAGE)-doc:: setup-bin
	mv dist-ghc6 dist
	mkdir -p debian/$(notdir $@)$(DOCDIR)/libraries/
	# Get the path to where the haddock directory is supposed to appear
	# and install the haddock files there.  Then create a link back to the
	# global haskell library documentation directory.
	sed -n 's/^haddock-html: \(.*\)$$/\1/p' dist/installed-pkg-config | \
	while read haddockdir; do \
	  mkdir -p debian/$(notdir $@)$${haddockdir}; \
	  cp -r dist/doc/html/* debian/$(notdir $@)$${haddockdir}; \
	  cd debian/$(notdir $@)$(DOCDIR)/libraries && ln -s ../../../../..$${haddockdir}/* && \
	  for i in *; do mv $$i $${i}.haddock; done; \
	done
	# Update the substvar file
	[ ! -x /usr/bin/cabal-debian ] || cabal-debian --substvar Doc
	# Create the doc package postinst and postrm if not present
	for script in postinst postrm; do \
	  [ -e debian/libghc6-$(CABAL_PACKAGE)-doc.$${script} ] || \
	  { echo "#!/bin/sh"; cat /usr/share/debhelper/autoscripts/$${script}-ghc-doc; echo "exit 0"; } > \
		debian/libghc6-$(CABAL_PACKAGE)-doc.$${script}; done
	mv dist dist-ghc6

install/libhugs-$(CABAL_PACKAGE):: setup-bin
	mv dist-hugs dist
	./setup-bin copy --destdir=debian/libhugs-$(CABAL_PACKAGE)
	rm -rf debian/libhugs-$(CABAL_PACKAGE)/usr/share/doc/*
	dh_haskell_prep -plibhugs-$(CABAL_PACKAGE)
	mv dist dist-hugs

common-binary-fixup-arch::
	# Move The executables into their respective packages.  If
	# there is a library deb, the packages will be part of the dev
	# package, and we can just create a link.  Otherwise we need
	# to do the install here.
	for EXEC in $(CABAL_EXECS); do \
	  if [ -f dist-ghc6/build/$${EXEC}/$${EXEC} ]; then \
	    DEBNAME=`echo $${EXEC} | tr 'A-Z' 'a-z'`; \
	    echo "Installing executable $${EXEC} into $${DEBNAME}..."; \
	    mkdir -p debian/$${DEBNAME}/usr/bin; \
	    if [ -d debian/libghc6-$(CABAL_PACKAGE)-dev ]; then \
	      cp -ax dist-ghc6/build/$${EXEC}/$${EXEC} debian/$${DEBNAME}/usr/bin/$${EXEC}; \
	    else \
	      install -m 755 dist-ghc6/build/$${EXEC}/$${EXEC} debian/$${DEBNAME}/usr/bin/$${EXEC}; \
	    fi; \
	  fi; \
	done
