#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.

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

STYLE=$(shell if [ -f "/usr/bin/d2c" ]; then echo "normal"; 	\
	else echo "bootstrap";					\
	fi)
VERSION=2.3.6pre2
MACHINE=$(shell uname -m)
ifeq ("$(MACHINE)", "sparc64")
	MACHINE=sparc
endif

bootstrap-stage1:  bootstrap-stage1-stamp
bootstrap-stage1-stamp:
	# Following is needed for bootstrapping
ifeq ("$(STYLE)", "bootstrap")
		libtoolize --force
		aclocal
		autoconf
		./configure --with-readline \
				--prefix=/usr \
				--enable-mindy-bootstrap \
				--enable-duim=no \
				--with-gnu-ld \
				--with-site-dylan-prefix=/usr
		libtoolize --force
		$(MAKE)
		$(MAKE) DESTDIR=`pwd`/debian/bootstrap install
		mkdir `pwd`/bootstrap_compiler
		ln -sf `pwd`/debian/bootstrap/usr/bin/d2c `pwd`/bootstrap_compiler

		rm -f config.cache
endif

	touch bootstrap-stage1-stamp

bootstrap-stage2:	bootstrap-stage2-stamp
bootstrap-stage2-stamp:  bootstrap-stage1-stamp
ifeq ("$(STYLE)", "bootstrap")
		PATH=`pwd`/debian/bootstrap/usr/bin:$(PATH); \
		    LD_LIBRARY_PATH=`pwd`/debian/bootstrap/usr/lib/dylan/$(VERSION)/$(MACHINE)-linux-gcc:$(LD_LIBRARY_PATH); \
		    ./configure --with-readline \
				--prefix=/usr \
				--enable-duim=no \
				--with-site-dylan-prefix=/usr \
				--with-gnu-ld \
				--with-existing-runtime=`pwd`/debian/bootstrap/usr/lib/dylan/$(VERSION)/$(MACHINE)-linux-gcc
		# End Bootstrapping portion

		touch bootstrap-stage2-stamp
	
		# Add here commands to compile the package.

		export PATH=`pwd`/debian/bootstrap/usr/bin:$(PATH); \
		    export LD_LIBRARY_PATH=`pwd`/debian/bootstrap/usr/lib/dylan/$(VERSION)/$(MACHINE)-linux-gcc:$(LD_LIBRARY_PATH); \
		    $(MAKE)
endif

	touch bootstrap-stage2-stamp
	

build: config build-stamp
build-stamp: bootstrap-stage1-stamp bootstrap-stage2-stamp
ifeq ("$(STYLE)", "normal")

		dh_testdir
		aclocal
		autoconf

		# Add here commands to compile the package.

		$(MAKE)
endif

	touch build-stamp


config: config-stamp
config-stamp:
ifeq ("$(STYLE)", "normal")
		libtoolize --force
		aclocal
		autoconf
		./configure --with-readline \
			--prefix=/usr \
			--with-site-dylan-prefix=/usr \
			--with-gnu-ld
		libtoolize --force
endif
	
	touch config-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp config-stamp bootstrap-stage1-stamp bootstrap-stage2-stamp
	rm -rf `pwd`/bootstrap_compiler
	rm -rf `pwd`/debian/bootstrap
	rm -f config.cache

	# Add here commands to clean up after the build process.
	-$(MAKE) bootstrap_clean

	chmod 0755 `pwd`/debian/delMakefiles
	chmod 0755 `pwd`/debian/delStatics
	`pwd`/debian/delMakefiles `pwd`
	`pwd`/debian/delStatics `pwd`
	-rm -f `pwd`/d2c/compiler/main/d2c.dbc
	-rm -f `pwd`/ltconfig
	-rm -f `pwd`/ltmain.sh
	-rm -f `pwd`/config.guess
	-rm -f `pwd`/config.sub

	dh_clean

install: install-stamp
install-stamp: build-stamp

	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_installdocs CREDITS NEWS ONEWS.html README

	# Add here commands to install the package into debian/tmp.
	$(MAKE) DESTDIR=`pwd`/debian/tmp install

	# First, put everything in FSSTD-compliant places
	mv `pwd`/debian/tmp/usr/man/* `pwd`/debian/tmp/usr/share/man

	# Now, copy some manpages
	cp `pwd`/debian/gen-makefile.1 `pwd`/debian/tmp/usr/share/man/man1
	cp `pwd`/debian/make-dylan-lib.1 `pwd`/debian/tmp/usr/share/man/man1
	cp `pwd`/debian/mk-build-tree.1 `pwd`/debian/tmp/usr/share/man/man1
	cp `pwd`/debian/line-count.1 `pwd`/debian/tmp/usr/share/man/man1

	# Get rid of non-Debian directories and stuff
	rm -rf `pwd`/debian/tmp/usr/man
	rm -f `pwd`/debian/tmp/usr/bin/mk-rcs-links

	# Finally, put some stuff in a sane state
	chmod 0644 `pwd`/debian/tmp/usr/include/*
	chmod 0755 `pwd`/debian/tmp/usr/share/dylan
	chmod 0644 `pwd`/debian/tmp/usr/share/dylan/*

	dh_movefiles

	# Make sure we don't have a bunch of extra stuff in Mindy
	rm -rf `pwd`/debian/mindy/usr/share/man/man4
	rm -rf `pwd`/debian/mindy/usr/share/man/man7
	rm -f `pwd`/debian/mindy/usr/share/man/man1/d2c*
	rm -f `pwd`/debian/mindy/usr/share/man/man1/dybug*
	rm -f `pwd`/debian/mindy/usr/share/man/man1/make*
	rm -f `pwd`/debian/mindy/usr/share/man/man1/mk*
	rm -f `pwd`/debian/mindy/usr/share/man/man1/melange*
	rm -f `pwd`/debian/mindy/usr/share/man/man1/parsergen*
	rm -f `pwd`/debian/mindy/usr/share/man/man1/gen*
	rm -f `pwd`/debian/mindy/usr/share/man/man1/line*

	# Make sure we don't have a bunch of extra stuff in Dev
	rm -rf `pwd`/debian/gwydion-dylan-dev/usr/share/man/man7
	rm -f `pwd`/debian/gwydion-dylan-dev/usr/share/man/man1/mindy*

	# Make sure we don't have a bunch of extra stuff in Dylan
	rm -rf `pwd`/debian/gwydion-dylan/share/man/man1/man1
	rm -rf `pwd`/debian/gwydion-dylan-dev/share/man/man4

	touch install-stamp

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
#	dh_testversion
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installinit
	dh_installcron
#	dh_installmanpages
#	dh_undocumented
	dh_installchangelogs
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_makeshlibs
	dh_md5sums
	dh_builddeb

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

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