#!/usr/bin/make -f
############################ -*- Mode: Makefile -*- ###########################
## rules ---
## Author           : Marcus Brinkmann <brinkmd@debian.org>
## Created On       : Sat,  1 Aug 1998 21:33:31 +0200
## Created On Node  : localhost
## Last Modified By : Marcus Brinkmann
## Last Modified On : Thu, 23 Sep 1999 01:41:38 +0200
## Last Machine Used: localhost
## Update Count     : 2
## Status           : Unknown, Use with caution!
## HISTORY          :
## Description      :
##
###############################################################################

# The name of the package (for example, `emacs').
package   := hurd

DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# Configuration variables (these should be pretty generic)
CC = cc
CFLAGS = -O2 -g -pipe -Wall
LDFLAGS = -s
PREFIX = /usr
BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/man
INFODIR = $(PREFIX)/info
DOCDIR = $(PREFIX)/share/doc/$(package)

#  Package specific stuff.  The idea is to try to make the rules
#  generic (gradually).

FILES_TO_CLEAN	= debian/files include/*.h
DIRS_TO_CLEAN	= debian/tmp debian/$(package)-dev build
STAMPS_TO_CLEAN	= stamp-build stamp-config

install_file	= install -o root -g root -m 644
install_program	= install -s -o root -g root -m 755
install_script  = install -o root -g root -m 755
make_directory	= install -d -o root -g root -m 755

define checkdir
	test -f debian/rules
endef

define checkroot
	@test 0 = "`id -u`" || (echo need root priviledges; exit 1)
endef

# Next is NOT a phony target.

configure: configure.in
	aclocal
	autoconf

# The next IS a phony target.

config: stamp-config
stamp-config: configure
	$(checkdir)
	-mkdir build
	cd build && ../configure --disable-profile \
	  --build=$(DEB_BUILD_GNU_TYPE) \
	  --host=$(DEB_HOST_GNU_TYPE) --prefix=
	touch stamp-config

all build: config stamp-build 
stamp-build:
	$(checkdir)
	cd build && $(MAKE)
# XXX-doc
	cd build && $(MAKE) -C doc hurd.info
	cd build && $(MAKE) -C doc hurd.ps
	touch stamp-build

clean:
	$(checkdir)
#	-cd build && make clean no_deps=t
	-rm -f  $(FILES_TO_CLEAN) $(STAMPS_TO_CLEAN)
	-rm -rf $(DIRS_TO_CLEAN)
# XXX-doc
	-rm -f doc/hurd.d doc/hurd.info* version.texi
	for NAME in hurd/*.h; do \
	  if [ -L $$NAME ] ; then \
	    rm -f $$NAME ; \
	  fi \
	done
	-rm -f core `find . \( -name '*.o' -name '*.orig' -o -name '*.rej' -o -name '*~' \
                -o -name '*.bak' -o -name '#*#' -o -name '.*.orig' \
                -o -name '.*.rej' -o -name '.SUMS' \) -print`

binary: binary-indep binary-arch

binary-indep:
# We have nothing to do here.

binary-arch: build
	$(checkdir)
	$(checkroot)
	-rm -rf			debian/{tmp,$(package)-dev}

# first the general install
	$(make_directory)	debian/tmp/boot
	cd build && $(MAKE)	install prefix=`pwd`/../debian/tmp no_prof=t
# kill the profiling libs
	-rm -f			debian/tmp/lib/*_p.a
# /etc/motd is in base-files!
	-rm -f			debian/tmp/etc/motd
# probably we'll make debug packages later, for now, strip'em
	-strip --strip-unneeded	debian/tmp/lib/lib*.so
	-strip --strip-debug	debian/tmp/lib/lib*.a
	-strip --strip-all	debian/tmp/bin/*
	-strip --strip-all	debian/tmp/sbin/*
	-strip --strip-all	debian/tmp/boot/*
	-strip --strip-all	debian/tmp/hurd/*
	-strip --strip-all	debian/tmp/libexec/*
	chmod			0644 debian/tmp/lib/lib*.a
	chmod			0755 $(addprefix debian/tmp/,		\
						 include include/hurd	\
						 lib hurd bin sbin	\
						 dev libexec etc etc/login)

# now distribute the files
# first the source package
	$(make_directory)	debian/$(package)-dev/{DEBIAN,usr/{share/doc,bin,lib}}
	ln -s			$(package) debian/$(package)-dev$(DOCDIR)-dev
	mv			debian/tmp/include debian/$(package)-dev/usr/.
	mv			debian/tmp/lib/*.a debian/$(package)-dev/usr/lib/.
	# Create development library links.
	for file in `cd debian/tmp/lib && ls *.so.*`; do \
	  linkname=`echo "$$file" | sed 's/\..*$$/.so/'`; \
	  ln -sf /lib/$$file debian/$(package)-dev/usr/lib/$$linkname; \
	done
	rm -f debian/tmp/lib/*.so

	dpkg-gencontrol		-isp -p$(package)-dev -Pdebian/$(package)-dev
	chown -R root.root	debian/$(package)-dev
	dpkg --build		debian/$(package)-dev ..

# now the shared libs and other stuff
	$(make_directory)	debian/tmp/DEBIAN
	$(make_directory)	debian/tmp$(DOCDIR)
	$(make_directory)	debian/tmp$(INFODIR)

	gzip -9fq		debian/tmp/boot/serverboot

# Only found in CVS, not the distribution.
#	$(install_file)		BUGS debian/tmp$(DOCDIR)
#	$(install_file)		TODO debian/tmp$(DOCDIR)
	$(install_file)		INSTALL debian/tmp$(DOCDIR)
	$(install_file)		NEWS debian/tmp$(DOCDIR)
	$(install_file)		README debian/tmp$(DOCDIR)
	$(install_file)		tasks debian/tmp$(DOCDIR)
	$(install_file)		ChangeLog debian/tmp$(DOCDIR)/changelog
	$(install_file)		debian/README.Debian debian/tmp$(DOCDIR)
	$(install_file)		debian/changelog debian/tmp$(DOCDIR)/changelog.Debian
# XXX-doc
	$(install_file)		build/doc/hurd.ps debian/tmp$(DOCDIR)
	gzip -9frq		debian/tmp$(DOCDIR)/.
	$(install_file)		debian/copyright debian/tmp$(DOCDIR)

# XXX-doc
	$(install_file)		build/doc/hurd.info* debian/tmp$(INFODIR)
	-gzip -9frq		debian/tmp$(INFODIR)
# XXX Remove the vague attempt of the makefiles to install the docs.
	rm -fR			debian/tmp/info

	$(install_file)		debian/servers.boot debian/tmp/boot/servers.boot
	$(make_directory)	debian/tmp/servers

# libexec/runsystem is managed by update-alternatives
	mv			debian/tmp/libexec/runsystem debian/tmp/libexec/runsystem.gnu

# We have our own rc, slightly modified.
	cp			debian/rc debian/tmp/libexec/rc

	$(install_file)		debian/conffiles debian/tmp/DEBIAN/conffiles
	$(install_file)		debian/shlibs debian/tmp/DEBIAN/shlibs
	$(install_script)	debian/postinst debian/tmp/DEBIAN/postinst
	$(install_script)	debian/prerm debian/tmp/DEBIAN/prerm

	# Filter out scripts.
	dpkg-shlibdeps		-p$(package) `for f in debian/tmp/bin/* \
				debian/tmp/libexec/* debian/tmp/hurd/* \
				debian/tmp/sbin/* ; do head -n1 $$f \
				| sed -n -e '/^#!/!a\'$$'\n'$$f ; done`
	dpkg-gencontrol         -isp -p$(package) -Pdebian/tmp
	chown -R root.root      debian/tmp
	dpkg --build            debian/tmp ..

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