#!/usr/bin/make -f

#export DH_VERBOSE=1

ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)
MULTIARCH=$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
CFLAGS="-O2  -g -Wall -D__COPYRIGHT\(x\)= -D__RCSID\(x\)= \
	-I. -DMACHINE=\\\"debian\\\" -DMACHINE_ARCH=\\\"${ARCH}\\\" \
	-DMACHINE_MULTIARCH=\\\"${MULTIARCH}\\\" \
	-DHAVE_SETENV -DHAVE_STRERROR -DHAVE_STRDUP -DHAVE_STRFTIME \
	-DHAVE_VSNPRINTF \
	-D_GNU_SOURCE -Wno-unused"

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CROSS :=
else
CROSS := CC=$(DEB_HOST_GNU_TYPE)-gcc
endif

build: build-stamp
build-stamp:
	dh_testdir
	$(MAKE) -f Makefile.boot CFLAGS=$(CFLAGS) $(CROSS)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	$(MAKE) -f Makefile.boot clean
	rm -f pmake pmake.1
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs usr/bin usr/share/mk usr/share/doc/pmake
	cp bmake pmake
	cp make.1 pmake.1
	dh_install pmake mkdep usr/bin
	dh_install mk/* usr/share/mk/
	groff -Tascii -ms PSD.doc/tutorial.ms > \
	      debian/pmake/usr/share/doc/pmake/tutorial.asc
	groff -Tps -ms PSD.doc/tutorial.ms > \
	      debian/pmake/usr/share/doc/pmake/tutorial.ps
	gzip -9v debian/pmake/usr/share/doc/pmake/*

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_install
	dh_installman pmake.1 mkdep.1
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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