#! /usr/bin/make -f
############################ -*- Mode: Makefile -*- ###########################
## debian.rules ---
## Author	    : Manoj Srivastava ( srivasta@pilgrim.umass.edu )
## Created On	    : Sat Apr 27 06:36:31 1996
## Created On Node  : melkor.pilgrim.umass.edu
## Last Modified By : Manoj Srivastava
## Last Modified On : Mon May 11 01:42:27 1998
## Last Machine Used: tiamat.datasync.com
## Update Count	    : 276
## Status	    : Unknown, Use with caution!
## HISTORY	    :
## Description	    :
## $Id: rules,v 1.39 1998/05/11 06:55:49 srivasta Exp $
###############################################################################

# The version of kernel-package this belongs to
kpkg_version := =K=V

# The maintainer information.
maintainer = Herbert Xu
email= herbert@greathan.apana.org.au

pgp=$(maintainer)

# The Debian revision of the kernel packages (for example, `2').
debian := 1.00

# decide if image is meant to be in /boot rather than /
image_in_boot :=
# Can we use symlinks?
no_symlink :=
# If so, where is the real file (usually, vmlinuz-X.X.X is real, and
# vmlinuz is the link, this variable reverses it.
reverse_symlink :=

# The version numbers for kernel-image, kernel-headers and
# kernel-source are deduced from the Makefile (see below,
# and footnote 1 for details)

# Whether to look for and install kernel patches by default.
# Be very careful if you do this.
patch_the_kernel := NO

# do not create libc headers by default
make_libc_headers := NO

# run make clean after build
do_clean := YES

# The default architecture (all if architecture independent)
architecture:=$(shell dpkg --print-architecture)

# The kernel image type (i.e. zImage or bzImage)
ifneq ($(strip $(filter m68k ppc powerpc,$(architecture))),)
kimage := zImage
else
ifneq ($(strip $(filter sparc,$(architecture))),)
kimage := vmlinux
else
kimage := bzImage
endif
endif

# Sparcs use silo, not lilo. Try to accomodate them
# PowerPCs use yet another loader: quik.
# m68k doesn't use lilo (atari lilo is alpha software; amiga lilo is
# stable but unpackaged; mac lilo, who knows?).
ifeq ($(strip $(architecture)),sparc)
loaderdep=silo
loader=silo
loaderdoc=SiloDefault
else
ifeq ($(strip $(architecture)),powerpc)
loaderdep=quik
loader=quik
loaderdoc=QuikDefault
else
ifeq ($(strip $(architecture)),m68k)
loaderdep=
loader=lilo
loaderdoc=
else
loaderdep=lilo (>= 19.1)
loader=lilo
loaderdoc=LiloDefault
endif
endif
endif

ifneq ($(strip $(loaderdep)),)
int_loaderdep := $(loaderdep),
else
int_loaderdep :=
endif


#  Package specific stuff
# The following variable is used to simplify the process of making
# diffs and orig targets, Please set it if AND ONLY IF no patches
# have been applied to the upstream source, in other words, we have
# just added debian* files.  If any patches were applied, please
# leave it empty

# NO_SRC_CHANGES =
NO_SRC_CHANGES = yes

VERSION:=$(shell grep ^VERSION Makefile 2>/dev/null | \
                 sed -e 's/[^0-9]*\([0-9]*\)/\1/')
PATCHLEVEL:=$(shell grep ^PATCHLEVEL Makefile 2>/dev/null | \
                    sed -e 's/[^0-9]*\([0-9]*\)/\1/')
SUBLEVEL:=$(shell grep ^SUBLEVEL Makefile 2>/dev/null | \
                  sed -e 's/[^0-9]*\([0-9]*\)/\1/')

# If you want to have more than one kernel configuration per kernel
# version, set FLAVOUR in the top level kernel Makefile *before*
# invoking make-kpkg -- it will be appended to UTS_RELEASE in
# version.h (separated by a hyphen). This affects everything -- the
# names and versions of the image, source, headers, and doc packages,
# and where the modules are searched for in /lib/modules.

ifdef FLAVOUR
# uhm - should check if we really have a Makefile capable of Flavours?
endif

FLAVOUR:=$(shell grep ^FLAVOUR Makefile 2>/dev/null | \
                  perl -ple 's/FLAVOUR[\s:=]+//g')

ifneq ($(strip $(FLAVOUR)),)
INT_FLAV := -$(FLAVOUR)
FLAV_ARG := FLAVOUR=$(FLAVOUR)
else
INT_FLAV :=
FLAV_ARG :=
endif

version = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(INT_FLAV)

# SUBARCH is used to distinguish Amiga, Atari, Macintosh, etc. kernels
# for Debian/m68k.  If you need it (exteremely unlikely), set it in
# the top level kernel Makefile *before* invoking make-kpg.  It only
# affects the naming of the kernel-image as the source and doc
# packages are architecture independent and the kernel-headers do not
# vary from one sub-architecture to the next.
# Note: Debian/powerpc will eventually use SUBARCH too.

SUBARCH:=$(shell grep ^SUBARCH Makefile 2>/dev/null | \
                  perl -ple 's/SUBARCH[\s:=]+//g')

ifneq ($(strip $(SUBARCH)),)
INT_SUBARCH := -$(SUBARCH)
else
INT_SUBARCH :=
endif



# The name of the package (for example, `emacs').
package   := kernel-source-$(version)
h_package := kernel-headers-$(version)
i_package := kernel-image-$(version)$(INT_SUBARCH)
d_package := kernel-doc-$(version)
l_package := libc-kheaders

FILES_TO_CLEAN = modules/modversions.h modules/ksyms.ver debian/files
STAMPS_TO_CLEAN = stamp-build stamp-configure stamp-source stamp-image \
                  stamp-headers stamp-src stamp-diff stamp-doc \
                  stamp-buildpackage stamp-libc-kheaders
DIRS_TO_CLEAN  = debian/tmp-source debian/tmp-headers debian/tmp-image

CONFLOC:=/etc/kernel-pkg.conf
LIBLOC:=/usr/lib/kernel-package
MODULE_LOC := /usr/src/modules

PATCH_DIR        := /usr/src/kernel-patches/$(architecture)
DIR_PATCH_APPLY   = $(PATCH_DIR)/apply
DIR_PATCH_UNPATCH = $(PATCH_DIR)/unpatch

# The destination of all .deb files
# (suggested by Rob Browning <osiris@cs.utexas.edu>)
DEB_DEST := ..

SRCTOP := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
DEBDIR := $(LIBLOC)
DEBDIR_NAME:= $(shell basename $(DEBDIR))

ifeq ($(strip $(architecture)),sparc)
DEBCONFIG:= $(DEBDIR)/config.sparc
else
DEBCONFIG:= $(DEBDIR)/config
endif

ifeq ($(strip $(architecture)),powerpc)
KERNEL_ARCH:=ppc
else
KERNEL_ARCH:=$(architecture)
endif

# The file which has local configuration
CONFIG_FILE := $(shell if test -e .config ; then \
                           echo .config; \
                       elif test -e $(DEBDIR)/config ; then \
                           echo $(DEBCONFIG); \
                       else echo /dev/null ; \
                       fi)

# define MODULES_ENABLED if appropriate
ifneq ($(strip $(shell egrep ^[^\#]*CONFIG_MODULES $(CONFIG_FILE))),)
MODULES_ENABLED = yes
endif

install_file=    install -p    -o root -g root -m 644
install_program= install -p    -o root -g root -m 755
make_directory=  install -p -d -o root -g root -m 755
deb_rule = $(MAKE) -f $(DEBDIR)/rules

# Include any site specific overrides here.
-include $(CONFLOC)

# Over ride the config file from the environment/command line
ifneq ($(strip $(KPKG_MAINTAINER)),)
maintainer=$(KPKG_MAINTAINER)
endif

ifneq ($(strip $(KPKG_EMAIL)),)
email=$(KPKG_EMAIL)
endif

ifneq ($(strip $(PGP_SIGNATURE)),)
pgp=$(PGP_SIGNATURE)
endif

ifneq ($(strip $(DEBIAN_REVISION)),)
debian := $(DEBIAN_REVISION)
endif

ifneq ($(strip $(IMAGE_IN_BOOT)),)
image_in_boot := $(IMAGE_IN_BOOT)
endif

ifneq ($(strip $(NO_SYMLINK)),)
no_symlink := $(NO_SYMLINK)
endif

ifneq ($(strip $(REVERSE_SYMLINK)),)
reverse_symlink := $(REVERSE_SYMLINK)
endif

ifneq ($(strip $(IMAGE_TYPE)),)
kimage = $(IMAGE_TYPE)
endif

ifneq ($(strip $(PATCH_THE_KERNEL)),)
patch_the_kernel = $(PATCH_THE_KERNEL)
endif

ifneq ($(strip $(MAKE_LIBC_HEADERS)),)
make_libc_headers = $(MAKE_LIBC_HEADERS)
endif

ifneq ($(strip $(ROOT_CMD)),)
root_cmd = $(ROOT_CMD)
endif


ifneq ($(strip $(CLEAN_SOURCE)),)
do_clean = $(CLEAN_SOURCE)
endif

# See if we are being run in the kernel directory
IN_KERNEL_DIR := $(shell if test -d drivers -a -d kernel -a -d fs -a \
                                 -d include/linux ; then \
                            echo YES; \
                         fi )
# See if the version numbers are valid
HAVE_VALID_PACKAGE_VERSION := $(shell \
      if test -x $(DEBDIR)/kpkg-vercheck; then \
	$(DEBDIR)/kpkg-vercheck $(debian) ; \
      else \
        echo "Could not find $(DEBDIR)/kpkg-vercheck" ; \
      fi )

ifeq ($(strip $(IN_KERNEL_DIR)),)
# Hah! Not in kernel directory!!
build configure clean binary kernel_source kernel-source kernel-headers\
stamp-source kernel_headers stamp-headers kernel_image stamp-image \
kernel-image kernel-doc kernel_doc stamp-doc buildpackage \
libc_kheaders libc-kheaders stamp-libc-kheaders:
	@echo "You should invoke this command from the top level directory of"
	@echo "a linux kernel source directory tree, and as far as I can tell,"
	@echo "the current directory:"
	@echo "	$(SRCTOP)"
	@echo "is not a top level linux kernel source directory. "
	@echo ""
	@echo "	(If I am wrong then kernel-packages and the linux kernel"
	@echo "	 are so out sync that you'd better get the latest versions"
	@echo "	 of the kernel-package package and the Linux sources)"
	@echo ""
	@echo "Please change directory to wherever linux kernel sources"
	@echo "reside and try again."
else
ifneq ($(strip $(HAVE_VALID_PACKAGE_VERSION)),YES)
# Hah! Bogus version number
build configure clean binary kernel_source kernel-source kernel-headers\
stamp-source kernel_headers stamp-headers kernel_image stamp-image \
kernel-image kernel-doc kernel_doc stamp-doc buildpackage:
	@echo "Problems ecountered with the version number $(debian)."
	@echo "$(HAVE_VALID_PACKAGE_VERSION)"
	@echo ""
	@echo "Please re-read the README file and try again."
else
all build: configure
# Builds the binary package.
# debian.config contains the current idea of what the image should
# have.
	-test -f stamp-configure || $(deb_rule) configure
	$(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) $(kimage)
ifdef MODULES_ENABLED
	$(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) modules
endif
	touch stamp-build

buildpackage: stamp-buildpackage
stamp-buildpackage: configure
	test -f stamp-configure || $(deb_rule) configure
	echo 'Building Package' > stamp-building
ifneq ($(strip $(root_cmd)),)
	dpkg-buildpackage -r$(root_cmd) -m"$(maintainer) <$(email)>"
else
	dpkg-buildpackage -m"$(maintainer) <$(email)>"
endif
	rm -f stamp-building
	touch stamp-buildpackage

configure: stamp-configure
stamp-configure: .config
	@test -f $(DEBDIR)/rules || \
            echo Error: Could not find $(DEBDIR)/rules
	-test ! -f stamp-configure && test ! -f debian/official && \
	      rm -rf debian && $(make_directory) debian
ifeq ($(strip $(patch_the_kernel)),YES)
	-test -d $(DIR_PATCH_APPLY) && run-parts --verbose $(DIR_PATCH_APPLY)
endif
	-test ! -f stamp-configure && \
	   sed -e 's/=V/$(version)/g'        -e 's/=D/$(debian)/g' \
	       -e 's/=A/$(architecture)/g'   -e 's/=SA/$(INT_SUBARCH)/g' \
               -e 's/=L/$(int_loaderdep) /g' \
               -e 's/=M/$(maintainer) <$(email)>/g'  \
		         $(DEBDIR)/Control > debian/control
ifeq ($(strip $(make_libc_headers)),YES)
	-test ! -f stamp-configure && \
	   cat $(DEBDIR)/Control.libc >> debian/control
endif
	-test ! -f stamp-configure && test ! -f debian/official && \
	   sed -e 's/=V/$(version)/g' -e 's/=D/$(debian)/g' \
	    -e 's/=A/$(architecture)/g' -e 's/=M/$(maintainer) <$(email)>/g' \
		$(DEBDIR)/changelog > debian/changelog
	-test ! -f stamp-configure && test ! -f debian/official && \
	   $(install_program) $(DEBDIR)/rules debian/rules
	-test ! -f stamp-configure && \
	  (test -f .config || \
                $(install_file) $(DEBDIR)/config .config) \
	  && $(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) oldconfig \
          && $(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) dep \
          && $(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) clean && \
              touch stamp-configure

.config:
	test -f .config || (test -f .config.save && \
		            cp -pf .config.save .config) || \
			true
	test -f .config || cp -pf $(DEBDIR)/config .config


clean:
ifeq ($(strip $(patch_the_kernel)),YES)
	-test -d $(DIR_PATCH_UNPATCH) && run-parts --verbose $(DIR_PATCH_UNPATCH)
endif
	rm -f $(FILES_TO_CLEAN) $(STAMPS_TO_CLEAN)
	cp -pf .config config.precious
	-test -f Makefile && $(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) distclean
	mv -f config.precious .config
	rm -rf $(DIRS_TO_CLEAN)
	test -f stamp-building || test -f debian/official || rm -rf debian

binary:       binary-indep binary-arch
binary-indep: kernel_source kernel_doc
binary-arch:  kernel_image  kernel_headers


kernel-source kernel_source: stamp-source
stamp-source: configure
	@test root = "`whoami`" || (echo need root priviledges; exit 1)
	test -f stamp-configure || $(deb_rule) configure
	rm -rf debian/tmp-source
	$(make_directory) debian/tmp-source/DEBIAN
	$(make_directory) debian/tmp-source/usr/src/$(package)
	$(make_directory) debian/tmp-source/usr/doc/$(package)
	sed -e 's/=P/$(package)/g' -e 's/=V/$(version)/g' \
	    $(DEBDIR)/include.postinst > debian/tmp-source/DEBIAN/postinst
	chmod 755 debian/tmp-source/DEBIAN/postinst
	sed -e 's/=P/$(package)/g' -e 's/=V/$(version)/g' \
		$(DEBDIR)/include.postrm > debian/tmp-source/DEBIAN/postrm
	chmod 755 debian/tmp-source/DEBIAN/postrm
	$(install_file) debian/changelog \
	         debian/tmp-source/usr/doc/$(package)/changelog.Debian
	gzip -9qf debian/tmp-source/usr/doc/$(package)/changelog.Debian
	$(install_file) $(DEBDIR)/LinkPolicy \
	         debian/tmp-source/usr/doc/$(package)/LinkPolicy
	gzip -9qf debian/tmp-source/usr/doc/$(package)/LinkPolicy
	$(install_file) $(DEBDIR)/README \
	         debian/tmp-source/usr/doc/$(package)/debian.README
	gzip -9qf debian/tmp-source/usr/doc/$(package)/debian.README
	$(install_file) $(DEBDIR)/README.headers \
	         debian/tmp-source/usr/doc/$(package)/README.headers
	gzip -9qf debian/tmp-source/usr/doc/$(package)/README.headers
	$(install_file) README \
	         debian/tmp-source/usr/doc/$(package)/README
	gzip -9qf debian/tmp-source/usr/doc/$(package)/README
	$(install_file) $(DEBDIR)/Headers \
	         debian/tmp-source/usr/doc/$(package)/Headers
	gzip -9qf debian/tmp-source/usr/doc/$(package)/Headers
	$(install_file) $(DEBDIR)/Flavours \
	         debian/tmp-source/usr/doc/$(package)/Flavours
	gzip -9qf debian/tmp-source/usr/doc/$(package)/Flavours
	$(install_file) $(DEBDIR)/Rationale \
	         debian/tmp-source/usr/doc/$(package)/Rationale
	gzip -9qf debian/tmp-source/usr/doc/$(package)/Rationale
	$(install_file) $(DEBDIR)/copyright.source \
		debian/tmp-source/usr/doc/$(package)/copyright
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	         debian/tmp-source/usr/doc/$(package)/Buildinfo
	(cd debian/tmp-source/usr/doc/$(package); \
         ln -s ../../src/$(package)/Documentation Documentation;)
	-tar cf - `echo * | sed -e 's/debian//g' -e 's/\.deb//g' ` \
	| (cd debian/tmp-source/usr/src/$(package); umask 000; tar xsf -)
	(cd debian/tmp-source/usr/src/$(package); \
            $(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) distclean)
	(cd debian/tmp-source/usr/src/$(package); \
                        rm -f stamp-building $(STAMPS_TO_CLEAN))
	(cd debian/tmp-source/usr/src/$(package)/include; rm -f asm; \
		ln -s asm-$(KERNEL_ARCH) asm)
	if test -f debian/official -a -f debian/README.Debian ; then \
           $(install_file) debian/README.Debian \
		debian/tmp-source/usr/src/$(package)/README.Debian ; \
	else \
	    sed -e 's/=V/$(version)/g' -e 's/=A/$(architecture)/g' \
                 $(DEBDIR)/README.source > \
		 debian/tmp-source/usr/src/$(package)/README.Debian ; \
	fi
	$(install_file) .config debian/tmp-source/usr/src/$(package)/.config.save
	dpkg-gencontrol -p$(package) -Pdebian/tmp-source/
	chown -R root.root debian/tmp-source
	chmod -R og=rX debian/tmp-source
	dpkg --build debian/tmp-source $(DEB_DEST)
	rm -f -r debian/tmp-source
	touch stamp-source

libc-kheaders libc_kheaders: stamp-libc-kheaders
stamp-libc-kheaders: configure
	@test root = "`whoami`" || (echo need root priviledges; exit 1)
	test -f stamp-configure || $(deb_rule) configure
	rm -rf                     debian/tmp-kheaders
	$(make_directory)          debian/tmp-kheaders/DEBIAN
	$(make_directory)          debian/tmp-kheaders/usr/include
	$(make_directory)          debian/tmp-kheaders/usr/doc/$(l_package)
	$(install_file)            debian/changelog \
	         debian/tmp-kheaders/usr/doc/$(l_package)/changelog.Debian
	$(install_file)            $(DEBDIR)/README.headers \
	         debian/tmp-kheaders/usr/doc/$(l_package)/README.debian
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	         debian/tmp-kheaders/usr/doc/$(l_package)/Buildinfo
	(cd include;               tar cf - linux asm-$(KERNEL_ARCH) ) | \
                 (cd debian/tmp-kheaders/usr/include/; umask 000; tar xsf -)
	(cd debian/tmp-kheaders/usr/include/; mv asm-$(KERNEL_ARCH) asm)
	gzip -9qfr                 debian/tmp-kheaders/usr/doc/
	$(install_file)            $(DEBDIR)/copyright.headers \
		  debian/tmp-kheaders/usr/doc/$(l_package)/copyright
	dpkg-gencontrol            -p$(l_package) -Pdebian/tmp-kheaders/
	chown -R                   root.root debian/tmp-kheaders
	chmod -R og=rX    	   debian/tmp-kheaders
	dpkg --build      	   debian/tmp-kheaders $(DEB_DEST)
	rm -rf            	   debian/tmp-kheaders
	touch             	   stamp-libc-kheaders


kernel-headers kernel_headers: stamp-headers
stamp-headers: configure
	@test root = "`whoami`" || (echo need root priviledges; exit 1)
	test -f stamp-configure || $(deb_rule) configure
	rm -rf debian/tmp-headers
	$(make_directory) debian/tmp-headers/DEBIAN
	$(make_directory) debian/tmp-headers/usr/src/$(h_package)
	$(make_directory) debian/tmp-headers/usr/doc/$(h_package)
	sed -e 's/=P/$(h_package)/g' -e 's/=V/$(version)/g' \
		$(DEBDIR)/include.postinst > debian/tmp-headers/DEBIAN/postinst
	chmod 755 debian/tmp-headers/DEBIAN/postinst
	sed -e 's/=P/$(h_package)/g' -e 's/=V/$(version)/g' \
		$(DEBDIR)/include.postrm > debian/tmp-headers/DEBIAN/postrm
	chmod 755 debian/tmp-headers/DEBIAN/postrm
	$(install_file) $(DEBDIR)/copyright.headers \
		debian/tmp-headers/usr/doc/$(h_package)/copyright
	$(install_file) debian/changelog \
	         debian/tmp-headers/usr/doc/$(h_package)/changelog.Debian
	gzip -9qf debian/tmp-headers/usr/doc/$(h_package)/changelog.Debian
	$(install_file) $(DEBDIR)/LinkPolicy \
	       debian/tmp-headers/usr/doc/$(h_package)/LinkPolicy
	gzip -9qf debian/tmp-headers/usr/doc/$(h_package)/LinkPolicy
	$(install_file) $(DEBDIR)/README.headers \
	         debian/tmp-headers/usr/doc/$(h_package)/debian.README
	gzip -9qf debian/tmp-headers/usr/doc/$(h_package)/debian.README
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	         debian/tmp-headers/usr/doc/$(h_package)/Buildinfo
	-tar cf - include | \
            (cd debian/tmp-headers/usr/src/$(h_package); umask 000; tar xsf -)
	(cd debian/tmp-headers/usr/src/$(h_package)/include; rm -f asm; \
		ln -s asm-$(KERNEL_ARCH) asm)
	dpkg-gencontrol -p$(h_package) -Pdebian/tmp-headers/
	chown -R root.root debian/tmp-headers
	chmod -R og=rX debian/tmp-headers
	dpkg --build debian/tmp-headers $(DEB_DEST)
	rm -rf debian/tmp-headers
	touch stamp-headers

kernel-doc kernel_doc: stamp-doc
stamp-doc: configure
	@test root = "`whoami`" || (echo need root priviledges; exit 1)
	test -f stamp-configure || $(deb_rule) configure
	rm -rf            debian/tmp-doc
	$(make_directory) debian/tmp-doc/DEBIAN
	$(make_directory) debian/tmp-doc/usr/doc/$(d_package)
	$(install_file) debian/changelog \
	         debian/tmp-doc/usr/doc/$(d_package)/changelog.Debian
	$(install_file) $(DEBDIR)/LinkPolicy \
	       debian/tmp-doc/usr/doc/$(d_package)/LinkPolicy.Debian
	$(install_file) $(DEBDIR)/README.doc \
	         debian/tmp-doc/usr/doc/$(d_package)/README.Debian
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	         debian/tmp-doc/usr/doc/$(d_package)/Buildinfo
	-tar cf - Documentation | \
            (cd debian/tmp-doc/usr/doc/$(d_package); umask 000; tar xsf -)
	-gzip -9fqr debian/tmp-doc/usr/doc/$(d_package)
	$(install_file) $(DEBDIR)/copyright.doc \
		debian/tmp-doc/usr/doc/$(d_package)/copyright
	dpkg-gencontrol -p$(d_package) -Pdebian/tmp-doc/
	chown -R root.root debian/tmp-doc
	chmod -R og=rX debian/tmp-doc
	dpkg --build debian/tmp-doc $(DEB_DEST)
	rm -rf debian/tmp-doc
	touch stamp-doc

kernel-image kernel_image: stamp-image
stamp-image: configure build
	@test root = "`whoami`" || (echo need root priviledges; exit 1)
	rm -f -r ./debian/tmp-image ./debian/tmp-image.deb
	test -f stamp-configure || $(deb_rule) configure
	test -f stamp-build || $(deb_rule) build
	$(make_directory) debian/tmp-image/DEBIAN
	$(make_directory) debian/tmp-image/boot
ifdef MODULES_ENABLED
	$(make_directory) debian/tmp-image/lib/modules/$(version)/cdrom
	$(make_directory) debian/tmp-image/lib/modules/$(version)/fs
	$(make_directory) debian/tmp-image/lib/modules/$(version)/ipv4
	$(make_directory) debian/tmp-image/lib/modules/$(version)/ipv6
	$(make_directory) debian/tmp-image/lib/modules/$(version)/misc
	$(make_directory) debian/tmp-image/lib/modules/$(version)/net
	$(make_directory) debian/tmp-image/lib/modules/$(version)/scsi
	$(make_directory) debian/tmp-image/lib/modules/$(version)/block
endif
	$(make_directory) debian/tmp-image/usr/doc/$(i_package)
	sed -e 's/=V/$(version)/g'    -e 's/=B/$(image_in_boot)/g'   \
            -e 's/=S/$(no_symlink)/g' -e 's/=R/$(reverse_symlink)/g' \
            -e 's/=K/$(kimage)/g'     -e 's/=L/$(loader)/g'          \
             $(DEBDIR)/image.postinst > debian/tmp-image/DEBIAN/postinst
	chmod 755 debian/tmp-image/DEBIAN/postinst
	sed -e 's/=V/$(version)/g'    -e 's/=L/$(loader)/g'          \
	             $(DEBDIR)/image.prerm > debian/tmp-image/DEBIAN/prerm
	chmod 755 debian/tmp-image/DEBIAN/prerm
	$(install_file) debian/changelog \
	         debian/tmp-image/usr/doc/$(i_package)/changelog.Debian
	gzip -9qf debian/tmp-image/usr/doc/$(i_package)/changelog.Debian
ifneq ($(strip $(architecture)),m68k)
	$(install_file) $(DEBDIR)/$(loaderdoc) \
	         debian/tmp-image/usr/doc/$(i_package)/$(loaderdoc)
	gzip -9qf debian/tmp-image/usr/doc/$(i_package)/$(loaderdoc)
endif
	$(install_file) $(DEBDIR)/README.image \
	         debian/tmp-image/usr/doc/$(i_package)/debian.README
	gzip -9qf debian/tmp-image/usr/doc/$(i_package)/debian.README
	$(install_file) $(DEBDIR)/copyright.image \
		debian/tmp-image/usr/doc/$(i_package)/copyright
	echo "This was produced by kernel-package version $(kpkg_version)." > \
	         debian/tmp-image/usr/doc/$(i_package)/Buildinfo
ifdef MODULES_ENABLED
	cp modules/* debian/tmp-image/lib/modules/$(version)/misc
	chmod 644 debian/tmp-image/lib/modules/$(version)/misc/*
	(if [ -f modules/BLOCK_MODULES ] ; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./BLOCK_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../block; fi; \
              done; \
              mv ./BLOCK_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/CDROM_MODULES ] ; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./CDROM_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../cdrom; fi; \
              done; \
              mv ./CDROM_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/FS_MODULES ]; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./FS_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../fs; fi; \
              done; \
              mv ./FS_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/NLS_MODULES ]; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./NLS_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../fs; fi; \
              done; \
              mv ./NLS_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/NET_MODULES ]; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./NET_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../net; fi; \
              done; \
              mv ./NET_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/HAM_MODULES ]; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./HAM_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../net; fi; \
              done; \
              mv ./HAM_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/IPV4_MODULES ]; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./IPV4_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../ipv4; fi; \
              done; \
              mv ./IPV4_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/IPV6_MODULES ]; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./IPV6_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../ipv6; fi; \
              done; \
              mv ./IPV6_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/SCSI_MODULES ]; then \
              cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./SCSI_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../scsi; fi; \
              done; \
              mv ./SCSI_MODULES ../../../../usr/doc/$(i_package)/; fi)
	(if [ -f modules/NET_MISC_MODULES ]; then \
	      cd debian/tmp-image/lib/modules/$(version)/misc; \
	      for mod in `cat ./NET_MISC_MODULES` ; do \
                  if [ -f $$mod ]; then mv $$mod ../net; fi; \
              done; \
	      mv ./NET_MISC_MODULES ../../../../usr/doc/$(i_package)/; fi)
endif
	$(install_file) .config \
	         debian/tmp-image/boot/config-$(version)
# SILO can boot gzip'ed kernel; m68k has vmlinuz, just the kernel makefiles
# don't seem to realise it.
ifneq ($(strip $(architecture)),m68k)
	cp arch/$(architecture)/boot/$(kimage) \
                   debian/tmp-image/boot/vmlinuz-$(version)
	chmod 644 debian/tmp-image/boot/vmlinuz-$(version)
else
ifeq ($(strip $(architecture)),powerpc)
	cp arch/$(KERNEL_ARCH)/$(SUBARCH)/boot/$(kimage) \
                   debian/tmp-image/boot/vmlinuz-$(version)
	chmod 644 debian/tmp-image/boot/vmlinuz-$(version)
else
	gzip -9vc vmlinux > debian/tmp-image/boot/vmlinuz-$(version)
	chmod 644 debian/tmp-image/boot/vmlinuz-$(version)
endif
endif
	cp System.map debian/tmp-image/boot/System.map-$(version)
	chmod 644 debian/tmp-image/boot/System.map-$(version)
	dpkg-gencontrol -p$(i_package) -Pdebian/tmp-image/
	chown -R root.root debian/tmp-image
	chmod -R og=rX debian/tmp-image
	dpkg --build debian/tmp-image $(DEB_DEST)
	rm -f -r debian/tmp-image
ifeq ($(strip $(do_clean)),YES)
	$(MAKE) $(FLAV_ARG) ARCH=$(KERNEL_ARCH) clean
endif
	rm -f stamp-build
	touch stamp-image

# This endif is for HAVE_VALID_PACKAGE_VERSION
endif

#This  endif is for IN_KERNEL_DIR
endif


# only generate module image packages
modules-image modules_image: configure
ifndef MODULES_ENABLED
	@echo Modules not configured, so not making modules_image
else
	-for module in $(MODULE_LOC)/* ; do \
	    (cd $$module; \
              ./debian/rules KVERS="$(version)" KSRC="$(SRCTOP)" \
                             KMAINT="$(pgp)" kdist_image; ); \
        done
endif

# generate the modules packages and sign them with dchanges
modules: configure
ifndef MODULES_ENABLED
	@echo Modules not configured, so not making modules
else
	-for module in $(MODULE_LOC)/* ; do \
	    (cd $$module; \
              ./debian/rules KVERS="$(version)" KSRC="$(SRCTOP)" \
                             KMAINT="$(pgp)" kdist; ); \
        done
endif

modules-config modules_config: configure
ifndef MODULES_ENABLED
	@echo Modules not configured, so not making modules_config
else
	-for module in $(MODULE_LOC)/* ; do \
	    (cd $$module; \
              ./debian/rules KVERS="$(version)" KSRC="$(SRCTOP)" \
                             KMAINT="$(pgp)" kdist_configure; ); \
        done
endif


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


.PHONY: binary binary-arch binary-indep clean modules modules_image
