#!/usr/bin/make -f
# Sample debian.rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified

package=mercury

MERCURY_DOCS = 	BUGS HISTORY LIMITATIONS NEWS README RELEASE_NOTES \
		README.Linux WORK_IN_PROGRESS
MERCURY_MANS =	doc/c2init.1 doc/mmc.1 doc/mgnuc.1 doc/ml.1 \
		doc/mprof.1 
DEBIAN_DOCS =	debian/README.debian debian/copyright 

PATH	    :=    $(PATH):/usr/local/mercury-DEV/bin:/home/tyson/install/bin

# To build, configure as if we are going to install in /usr,
# then build the compiler. 
build:
	$(checkdir)
	echo $(PATH)
	autoconf
	./configure --prefix=/usr
	make
	touch build

clean:
	$(checkdir)
	echo $(PATH)
	-rm -f build
	autoconf
	./configure 
	-mmake realclean
	-rm -f config.status config.cache config.log configure.log
	-rm -f runtime/mercury_conf.h boehm_gc/mercury_boehm_gc_conf.h
	-rm -f bindist.build_vars bindist/bindist.build_vars
	-rm -f Mmake.common
	-rm -f configure
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp debian/files* core debian/substvars

binary-indep: checkroot build
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.
# XXX This isn't entirely true - all the .int files (and such)
# are architecture independent. But that's not really worth worrying
# about.

# To build the binary package, we install into ./debian/tmp/usr
# However, we have configured as if we are going to install into
# /usr, so we need to convince the "make install" target to
# install elsewhere.
# We override INSTALL_PREFIX to tell it to install to ./debian/tmp/usr.
# Hardcoded paths should be depending on FINAL_INSTALL_PREFIX instead,
# which we leave configured for /usr.
binary-arch: checkroot build
	$(checkdir)
	-rm -rf debian/tmp
	install -d debian/tmp
	cd debian/tmp;install -d `cat ../dirs`
	make install INSTALL_PREFIX=`pwd`/debian/tmp/usr ;\
	cp boehm_gc/README debian/tmp/usr/doc/mercury/README.boehm_gc
# According to debian policy, samples go in examples
	cp -r samples debian/tmp/usr/doc/mercury/examples
# install the various information files into /usr/doc
	install -m 644 $(MERCURY_DOCS) $(DEBIAN_DOCS) debian/tmp/usr/doc/mercury
	install -m 644 debian/changelog \
		debian/tmp/usr/doc/mercury/changelog.Debian
# install the manpages
	install -m 644 $(MERCURY_MANS) debian/tmp/usr/man/man1/
# compress manpages, info pages, and html
	-gzip -9 `find debian/tmp/usr/man -type f 2>/dev/null`
	-gzip -9 `find debian/tmp/usr/info -type f`
	-gzip -9 `find debian/tmp/usr/doc -type f -size +4k ! -name "*.htm*"`
# strip all executables
	strip `find debian/tmp -type f -perm +111 | xargs file | fgrep ELF | sed 's/^\([^:]*\):.*$$/\1/'`
# create DEBIAN directory
	install -d debian/tmp/DEBIAN
# install necessary scrips into debian directory
	install -m 755 debian/postinst debian/postrm debian/tmp/DEBIAN/
	# XXX not entirely sure what to do about the shlibs
	-dpkg-shlibdeps `find debian/tmp -type f -perm +111 | xargs file | fgrep ELF | sed 's/^\([^:]*\):.*$$/\1/'`
	md5sum `find debian/tmp/* -type f ! -regex "debian/tmp/DEBIAN/.*"` >debian/tmp/DEBIAN/checksums
	dpkg-gencontrol
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

define checkdir
	test -f debian/rules
endef

# Below here is fairly generic really

binary: binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep clean checkroot
