#! /usr/bin/make -f
############################ -*- Mode: Makefile -*- ###########################
## rules ---
## Author           : Manoj Srivastava ( srivasta@tiamat.datasync.com )
## Created On       : Tue Mar  4 10:49:39 1997
## Created On Node  : tiamat.datasync.com
## Last Modified By : Manoj Srivastava
## Last Modified On : Mon Sep 11 14:42:14 2000
## Last Machine Used: glaurung.green-gryphon.com
## Update Count     : 53
## Status           : Unknown, Use with caution!
## HISTORY          :
## Description      :
##
###############################################################################

ifdef ARCH
  ha:=-a$(ARCH)
endif

# set the dpkg-architecture vars
export DEB_BUILD_ARCH      := $(shell dpkg-architecture $(ha) \
                                        -qDEB_BUILD_ARCH)
export DEB_BUILD_GNU_CPU   := $(shell dpkg-architecture $(ha) \
                                        -qDEB_BUILD_GNU_CPU)
export DEB_BUILD_GNU_SYSTEM:= $(shell dpkg-architecture $(ha) \
                                        -qDEB_BUILD_GNU_SYSTEM)
export DEB_BUILD_GNU_TYPE  := $(shell dpkg-architecture $(ha) \
                                        -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_ARCH       := $(shell dpkg-architecture $(ha) \
                                        -qDEB_HOST_ARCH)
export DEB_HOST_GNU_CPU    := $(shell dpkg-architecture $(ha) \
                                        -qDEB_HOST_GNU_CPU)
export DEB_HOST_GNU_SYSTEM := $(shell dpkg-architecture $(ha) \
                                        -qDEB_HOST_GNU_SYSTEM)
export DEB_HOST_GNU_TYPE   := $(shell dpkg-architecture $(ha) \
                                        -qDEB_HOST_GNU_TYPE)


#
# VERSION=$(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | \
#                          sed 's/^Version: *//')
#

# The name of the package (for example, `emacs').
package   := make
d_package := make-doc

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

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

FILES_TO_CLEAN  = TAGS tags make.aux make.cp make.cps make.fn \
                  make.fns make.toc debian/files config.cache \
                  make.dvi make.ps
DIRS_TO_CLEAN   = debian/tmp debian/tmp-doc debian/html
STAMPS_TO_CLEAN =  stamp-configure stamp-build stamp-binary stamp-doc \
                   stamp-html
SRCTOP := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
DOC_TOP=$(SRCTOP)/debian/tmp-doc
DOC_DOC=$(DOC_TOP)/usr/share/doc/$(d_package)
DOC_MENU=$(DOC_TOP)/usr/lib/menu
DOC_DBASE=$(DOC_TOP)/usr/share/doc-base

PKG_TOP=$(SRCTOP)/debian/tmp
PKG_DOC=$(PKG_TOP)/usr/share/doc/$(package)
PKG_MENU=$(PKG_TOP)/usr/lib/menu
PKG_DBASE=$(PKG_TOP)/usr/share/doc-base
PKG_INFO=$(PKG_TOP)/usr/share/info
PKG_MANDIR=$(PKG_TOP)/usr/share/man/
PKG_MAN=$(PKG_MANDIR)/man1
PKG_BIN=$(PKG_TOP)/usr/bin

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  STRIP += -s
  LDFLAGS += -s
endif



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

all build: stamp-build
# Builds the binary package.

configure: stamp-configure

stamp-configure:
	./configure --verbose --prefix=$(PREFIX) --mandir=$(MANDIR) \
                    --infodir=$(INFODIR) --sysconfdir=/etc          \
                     && touch stamp-configure


stamp-build:
# Builds the binary package.
	$(checkdir)
	-test -f stamp-configure || $(MAKE) -f debian/rules configure
	$(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
	touch stamp-build

clean:
# Undoes the effect of `$(MAKE) -f debian/rules build'.
	$(checkdir)
	-test -f Makefile && $(MAKE) distclean
	-rm -f  $(FILES_TO_CLEAN) $(STAMPS_TO_CLEAN)
	-rm -rf $(DIRS_TO_CLEAN)
	-rm -f core `find . \( -name '*.orig' -o -name '*.rej' -o -name '*~' \
                -o -name '*.bak' -o -name '#*#' -o -name '.*.orig' \
                -o -name '.*.rej' -o -name '.SUMS' -o -size 0 \) -print` TAGS

binary: binary-indep binary-arch

binary-indep: html stamp-doc

stamp-doc: html
	@test 0 = $$(id | sed -e 's/(.*$$//' -e 's/^uid=//') || (echo need root priviledges; exit 1)
	$(checkdir)
	test -f stamp-configure|| $(MAKE) -f debian/rules configure
	rm -rf               $(DOC_TOP) $(DOC_TOP).deb
	$(make_directory)    $(DOC_TOP)/DEBIAN
	$(make_directory)    $(DOC_DOC)
	$(make_directory)    $(DOC_MENU)
	$(make_directory)    $(DOC_DBASE)
	$(MAKE)              make.ps
	$(install_file)      make.ps          	     $(DOC_DOC)
	$(install_file)      ChangeLog        	     $(DOC_DOC)/changelog
	$(install_file)      NEWS             	     $(DOC_DOC)/NEWS
	$(install_file)      debian/changelog 	     $(DOC_DOC)/changelog.Debian
	gzip -9fqr           $(DOC_DOC)/
# Make sure the copyright file is not compressed
	$(install_file)      debian/copyright        $(DOC_DOC)/copyright
	$(install_file)      debian/html/make_*.html $(DOC_DOC)/
	$(install_file)      debian/menuentry        $(DOC_MENU)/$(d_package)
	$(install_file)      debian/docentry         $(DOC_DBASE)/$(d_package)
	$(install_program)   debian/doc.postinst     $(DOC_TOP)/DEBIAN/postinst
	$(install_program)   debian/doc.postrm       $(DOC_TOP)/DEBIAN/postrm
	$(install_program)   debian/doc.prerm        $(DOC_TOP)/DEBIAN/prerm
	dpkg-gencontrol      -pmake-doc -isp         -P$(DOC_TOP)
	chown -R root.root   $(DOC_TOP)
	dpkg --build         $(DOC_TOP) ..
	touch                stamp-doc

binary-arch: build stamp-binary

stamp-binary:
# Makes a binary package.
	@test root = "`whoami`" || \
		(echo need root priviledges; exit 1)
	$(checkdir)
	test -f stamp-build  || $(MAKE) -f debian/rules build
	rm -rf               $(PKG_TOP) $(PKG_TOP).deb
	rm -f                $(PKG_INFO)/$(package).info*.gz
	$(make_directory)    $(PKG_TOP)
	$(make_directory)    $(PKG_TOP)/DEBIAN
	$(make_directory)    $(PKG_BIN)
	$(make_directory)    $(PKG_INFO)
	$(make_directory)    $(PKG_MAN)
	$(make_directory)    $(PKG_DOC)
	$(MAKE)              install           prefix=$(PKG_TOP)/usr \
	      infodir=$(PKG_INFO) mandir=$(PKG_MANDIR)
	$(install_file)      ChangeLog 	       $(PKG_DOC)/changelog
	$(install_file)      README 	       $(PKG_DOC)/README
	$(install_file)      NEWS 	       $(PKG_DOC)/NEWS
	$(install_file)      debian/changelog  $(PKG_DOC)/changelog.Debian
	gzip -9frq           $(PKG_DOC)/
# Make sure the copyright file is not compressed
	$(install_file)      debian/copyright  $(PKG_DOC)/copyright
	gzip -9fqr           $(PKG_MAN)/
	gzip -9fq            $(PKG_INFO)/$(package).info*
	$(install_program)   debian/postinst  $(PKG_TOP)/DEBIAN/postinst
	$(install_program)   debian/prerm $(PKG_TOP)/DEBIAN/prerm
	dpkg-shlibdeps       $(PKG_BIN)/make
	dpkg-gencontrol      -pmake -isp -P$(PKG_TOP)
	chown -R root.root   $(PKG_TOP)
	dpkg --build         $(PKG_TOP) ..
	touch                stamp-binary

html: stamp-html

stamp-html:
	$(checkdir)
	$(make_directory) debian/html
	(cd debian/html; \
	 texi2html -doctype html2 -expandinfo -glossary -menu \
	           -split_chapter -verbose  ../../make.texinfo)
	touch stamp-html

checkroot:
	@test 0 = $$(id | sed -e 's/(.*$$//' -e 's/^uid=//') || \
		(echo need root priviledges; exit 1)

define checkdir
	test -f  make.h -a -f debian/rules
endef

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


.PHONY: binary binary-arch binary-indep clean

