#!/usr/bin/make -f
############################ -*- Mode: Makefile -*- ###########################
## rules ---
## Author           : Marcus Brinkmann <Marcus.Brinkmann@ruhr-uni-bochum.de
## Created On       : Wed, 11 Mar 1998 15:05:54 +0100
## Created On Node  : localhost
## Last Modified By : Marcus Brinkmann
## Last Modified On : Wed, 11 Mar 1998 15:05:54 +0100
## Last Machine Used: localhost
## Update Count     : 0
## Status           : Unknown, Use with caution!
## HISTORY          :
## Description      :
##
###############################################################################

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

# Configuration variables (these should be pretty generic)
#CC = cc
CC = gcc
CFLAGS = `dpkg-buildflags --get CFLAGS` \
	`dpkg-buildflags --get LDFLAGS` \
	`pkg-config --cflags --libs alsa` \
	-O2 -g -pipe -Wall -Wno-pointer-sign
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	DONTSTRIP = true
	LDFLAGS = $(LDFLAGS) -s
endif
PREFIX = /usr
BINDIR = $(PREFIX)/games
MANDIR = $(PREFIX)/share/man
MAN6DIR = $(MANDIR)/man6
DOCDIR = $(PREFIX)/share/doc/$(package)
#PIXMAPSDIR = $(PREFIX)/X11R6/include/X11/pixmaps
PIXMAPSDIR = $(PREFIX)/share/pixmaps

#  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
STAMPS_TO_CLEAN = stamp-build stamp-binary stamp-xmkmf

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

all build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

# Compatibility with archaic build rules
build-stamp: stamp-build

xmkmf: stamp-xmkmf
stamp-xmkmf:
	-cd src && cp ../debian/Imakefile.debian Imakefile && xmkmf && touch ../stamp-xmkmf

stamp-build:
	$(checkdir)
	# build package
	test -f stamp-xmkmf || $(MAKE) -f debian/rules xmkmf
	cd src && $(MAKE) CCOPTIONS="$(CFLAGS)"
	touch stamp-build

clean:
	$(checkdir)
	-cd src && test -f Makefile && $(MAKE) clean
	-rm -f $(FILES_TO_CLEAN) $(STAMPS_TO_CLEAN)
	-rm -rf $(DIRS_TO_CLEAN)
	-cd src && mv xscavenger.6 scavenger.6
	-cd src && cp ../debian/Imakefile.default Imakefile
	-rm src/Makefile*
	-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
	-rm -f debian/substvars

binary:		binary-indep binary-arch

binary-indep:

binary-arch: stamp-binary
stamp-binary: build
	$(checkroot)
	$(checkdir)
	-rm -rf			debian/tmp

	$(make_directory) debian/tmp/DEBIAN
	$(make_directory) debian/tmp$(BINDIR)
	$(make_directory) debian/tmp/usr/lib/games/$(package)
	$(make_directory) debian/tmp$(DOCDIR)
	$(make_directory) debian/tmp$(MAN6DIR)
	$(make_directory) debian/tmp$(PIXMAPSDIR)

	cd src && $(MAKE) install DESTDIR=../debian/tmp
	[ "$(DONTSTRIP)" != true ] || \
		strip -s -R.note -R.comment	debian/tmp$(BINDIR)/xscavenger

	$(install_file)		src/scavenger.6 debian/tmp$(MAN6DIR)/xscavenger.6
	gzip -n -9frq		debian/tmp$(MANDIR)/.

	# menu file
	$(install_script)	debian/xscavenger.postinst debian/tmp/DEBIAN/postinst
	$(install_script)	debian/xscavenger.postrm debian/tmp/DEBIAN/postrm
	$(install_script)	debian/xscavenger.prerm debian/tmp/DEBIAN/prerm
	$(install_file)		debian/xscavenger-icon.xpm debian/tmp$(PIXMAPSDIR)/xscavenger-icon.xpm

	# copy doc files but not INSTALL, it won't be needed
	# compress all but copyright
	$(install_file)		README debian/tmp$(DOCDIR)/README
	$(install_file)		CREDITS debian/tmp$(DOCDIR)/CREDITS
	$(install_file)		DOC debian/tmp$(DOCDIR)/DOC
#	$(install_file)		TODO debian/tmp$(DOCDIR)/TODO
	$(install_file)		debian/README.Debian debian/tmp$(DOCDIR)/README.Debian
	$(install_file)		debian/changelog debian/tmp$(DOCDIR)/changelog.Debian
	$(install_file)		changelog debian/tmp$(DOCDIR)/changelog
	gzip -n -9frq		debian/tmp$(DOCDIR)/.
	$(install_file)		debian/copyright debian/tmp$(DOCDIR)/copyright
	$(make_directory)	debian/tmp/usr/share/applications
	$(install_file)		debian/xscavenger.desktop debian/tmp/usr/share/applications/xscavenger.desktop

	dpkg-shlibdeps		debian/tmp$(BINDIR)/xscavenger
	dpkg-gencontrol         -p$(package) -Pdebian/tmp
	chown -R root.root      debian/tmp
	dpkg --build            debian/tmp ..

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