#!/usr/bin/make -f

INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

package = mktemp

CFLAGS = -Wall -g

define checkdir
        test -f debian/rules
endef

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

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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

CONFIGFLAGS = --prefix=/usr --mandir=\$${prefix}/share/man --bindir=/bin

config.status: configure
	$(checkdir)
	CFLAGS="$(CFLAGS)" ./configure $(CONFIGFLAGS)

build: build-stamp

build-stamp:  config.status
	$(checkdir)

	$(MAKE)
	touch build-stamp

clean: checkroot
	rm -f build-stamp

	-$(MAKE) distclean
	rm -f debian/mktemp.substvars
	rm -rf debian/mktemp

install: checkroot build
	rm -f debian/mktemp.substvars
	rm -rf debian/mktemp

	# Add here commands to install the package into debian/mktemp.
	$(MAKE) install prefix=$(CURDIR)/debian/mktemp/usr \
		bindir=$(CURDIR)/debian/mktemp/bin


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install checkroot
	$(INSTALL_DIR) debian/mktemp/DEBIAN debian/mktemp/usr/share/doc/mktemp
	$(INSTALL_FILE) debian/changelog debian/mktemp/usr/share/doc/mktemp/changelog.Debian
	$(INSTALL_FILE) README debian/copyright debian/mktemp/usr/share/doc/mktemp/
	strip --remove-section=.comment --remove-section=.note debian/mktemp/bin/mktemp
	gzip -9f debian/mktemp/usr/share/man/man1/mktemp.1 \
	         debian/mktemp/usr/share/doc/mktemp/changelog.Debian
	dpkg-shlibdeps -Tdebian/mktemp.substvars debian/mktemp/bin/mktemp
	dpkg-gencontrol -ldebian/changelog -isp -Tdebian/mktemp.substvars -Pdebian/mktemp

	cd debian/mktemp && find . -type f ! -regex '.*/DEBIAN/.*' | xargs -r md5sum > DEBIAN/md5sums

	chown -R root:root debian/mktemp/*
	chmod -R u+w,a+rX debian/mktemp/*
	
	dpkg-deb --build debian/mktemp ..

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

checkroot:
	$(checkdir)
	test root = "`whoami`"
