#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

SHELL = bash

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif


# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_CPU  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
DEB_BUILD_GNU_SYSTEM  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)

# Add backward compatibility to allow backport to stable
ifeq ($(DEB_HOST_GNU_SYSTEM), linux)
DEB_HOST_GNU_SYSTEM=linux-gnu
endif
ifeq ($(DEB_BUILD_GNU_SYSTEM), linux)
DEB_BUILD_GNU_SYSTEM=linux-gnu
endif

GRUB_VERSION=0.97
GRUB_DISK_IMAGE=grub-${GRUB_VERSION}-$(DEB_BUILD_GNU_CPU)-pc

ifeq ($(DEB_BUILD_GNU_SYSTEM),linux-gnu)
filesystems=e2fs jfs minix reiserfs xfs fat
endif
ifeq ($(DEB_BUILD_GNU_SYSTEM),gnu)
filesystems=e2fs ffs minix
endif
ifeq ($(DEB_BUILD_GNU_SYSTEM),kfreebsd-gnu)
filesystems=e2fs ffs ufs2
endif
ifeq ($(DEB_BUILD_GNU_SYSTEM),knetbsd-gnu)
filesystems=e2fs ffs ufs2
endif

ifeq ($(DEB_BUILD_GNU_CPU),x86_64)
  LDFLAGS=-static
endif

CC=gcc

patch: patch-stamp
patch-stamp:
	dh_testdir

	# Add here commands to patch the source..
	set -e ; for i in `cat debian/patches/00list | grep -v ^#` ; \
	do \
	echo "--- $$i ---" ; patch -p1 < debian/patches/$$i ; \
	done

	touch patch-stamp

unpatch:
	dh_testdir

	# Add here commands to reverse patchs to the source.
	if [ -f patch-stamp ] ; then \
	set -e ; for i in `tac debian/patches/00list | grep -v ^#` ; \
	do \
	echo "--- $$i ---" ; patch -R -p1 < debian/patches/$$i ; \
	done ; \
	fi \

	if [ -f patch-stamp ] ; then \
	rm patch-stamp ; \
	fi

configure: configure-stamp
configure-stamp: patch-stamp
	dh_testdir

	# Add here commands to configure the package.
	aclocal-1.9 && automake-1.9 && autoconf
	CC=$(CC) LDFLAGS=$(LDFLAGS) ./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--disable-auto-linux-mem-opt

	touch configure-stamp

build: build-arch

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	## the creation of these manpages here is temporary,
	## when building grub finally works with the version
	## of autoconf in debian we can use MAINTAINER_MODE_TRUE
	# create man page for grub
	( cd docs && ./help2man \
	--name="the grub shell" \
	--include=grub.8.additions \
	--section=8 --output=grub.8 \
	../grub/grub )

	# create man page for grub-install
	( cd util && chmod 755 grub-install )
	( cd docs && ./help2man \
	--name="install GRUB on your drive" \
	--include=grub-install.8.additions \
	--section=8 --output=grub-install.8 \
	../util/grub-install )

	# create man page for mbchk
	( cd docs && ./help2man \
	--name="check the format of a Multiboot kernel" \
	--section=1 --output=mbchk.1 \
	../util/mbchk )

	# create man page for grub-md5-crypt
	( cd util && chmod 755 grub-md5-crypt )
	( cd docs && ./help2man \
	--name="Encrypt a password in MD5 format" \
	--section=8 --output=grub-md5-crypt.8 \
	../util/grub-md5-crypt )

	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp
	dh_testdir

	# create html documentation from texi files
	( cd docs && texi2html -split_chapter grub.texi )
	( cd docs && texi2html -split_chapter multiboot.texi )

	touch build-indep-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp config.log

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

	# The Makefile doesn't clean up the generated info files so we will do
	# it here so the doco is uptodate.
	-( cd docs && rm -f *.info* )

	# clean up generated html documentation
	-( cd docs && rm -f *.html )

	# clean up documentation directory removing created dirs.
	-( cd docs && rm -rf grub multiboot )

	# remove files that will be change due our automake and autoconf rebuilding
	-rm -f $(shell find . -name 'Makefile.in' -o \
                    -name 'aclocal.m4' -o \
                    -name 'configure')

	# reverse patches we applied. this has to happen after make distclean
	-$(MAKE) -s -f debian/rules unpatch

	dh_clean

install: install-indep install-arch
install-indep: install-arch
	dh_testdir
	dh_testroot
	dh_clean -k -i 
	dh_installdirs -i

	# Add here commands to install the indep part of the package into
	# debian/<package>-doc.
	#INSTALLDOC#

	# grub-disk
	mkdir -p $(CURDIR)/debian/tmp/root/boot/grub
	cp $(CURDIR)/debian/grub/usr/lib/grub/*-*/{stage1,e2fs_stage1_5,stage2} \
		$(CURDIR)/docs/menu.lst \
		$(CURDIR)/debian/tmp/root/boot/grub
	tar -C $(CURDIR)/debian/tmp/root \
		-cvzf $(CURDIR)/debian/tmp/grub.tar.gz boot
	chmod +x $(CURDIR)/util/mkbimage
	$(CURDIR)/util/mkbimage -t 1.44 \
		-d $(CURDIR)/debian/tmp/ -s ext2 \
		-f $(CURDIR)/debian/tmp/grub.tar.gz

	rm -f $(CURDIR)/debian/tmp/root/boot/grub/*stage*
	cp $(CURDIR)/debian/grub/usr/lib/grub/*-*/stage2_eltorito \
		$(CURDIR)/debian/tmp/root/boot/grub
	mkisofs \
		-b boot/grub/stage2_eltorito \
		-no-emul-boot -boot-load-size 4 -boot-info-table \
		-o $(CURDIR)/debian/tmp/raw.iso \
		-r $(CURDIR)/debian/tmp/root

	gzip -c9 $(CURDIR)/debian/tmp/1.44.image > \
		$(CURDIR)/debian/grub-disk/usr/share/grub-disk/${GRUB_DISK_IMAGE}.ext2fs.gz
	cp $(CURDIR)/debian/tmp/raw.iso \
		 $(CURDIR)/debian/grub-disk/usr/share/grub-disk/${GRUB_DISK_IMAGE}.iso

	dh_install -i

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k -s 
	dh_installdirs -s

	# Add here commands to install the package into debian/grub.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/grub/

	# make install puts the info files in the grub package but we want them
	# in the grub-doc package so lets clean shop and let dh_installinfo do
	# the work
	rm -rf debian/grub/usr/share/info

	# Cleanup filesystems not used natively in Debian.
	# (non-native filesystems are supported via stage2)
	mkdir debian/grub/usr/lib/grub/tmp/
	mv debian/grub/usr/lib/grub/*-*/*_stage1_5 \
		debian/grub/usr/lib/grub/tmp/
	for i in $(filesystems) ; do \
		mv debian/grub/usr/lib/grub/tmp/$${i}_stage1_5 \
		debian/grub/usr/lib/grub/*-*/ ; \
	done
	rm -rf debian/grub/usr/lib/grub/tmp/

	dh_install -s

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
    # Add a wrapper for update-grub. Remove it post-etch
	cp debian/update-grub.wrapper debian/grub/sbin/update-grub

	# Add a wrapper for grub-install. Remove it post-etch
	cp debian/grub-install.wrapper debian/grub/sbin/grub-install

	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate	
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
	dh_installinfo
	dh_installman
#	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Grab fixes from CVS using our version as reference.
cvs-sync:
	cvs -d:pserver:anonymous@cvs.savannah.gnu.org:/cvsroot/grub checkout -d grub-cvs grub; \
	UVERSION=$$(dpkg-parsechangelog | grep Version | sed 's,Version: ,,g;s,-[0-9]*$$,,g;s,\.,_,g'); \
	cd grub-cvs && \
	cvs diff -uN -r release_$$UVERSION -r HEAD \
         | sed 's,^\(---\|+++\) \(.*\)$$,\1 foo/\2,g' \
         | filterdiff -x '*/.cvsignore' -x '*/docs/version.texi' -x '*/docs/stamp-vti' > ../debian/patches/cvs-sync.patch; \
	cd .. && rm -rf grub-cvs
binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure patch unpatch
