#!/usr/bin/make -f

DEBDIR = debian/tmp/DEBIAN
BINDIR = debian/tmp/usr/bin
MANDIR = debian/tmp/usr/share/man
DOCDIR = debian/tmp/usr/share/doc/mpack

ifeq (,$(findstring noopt,${DEB_BUILD_OPTIONS}))
	OPT = -O2
endif

STRIP = strip --remove-section=.comment --remove-section=.note

export DEB_HOST  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

CFLAGS += ${OPT} -g -Wall

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	test -f debian/rules
	./configure --prefix=/usr --build ${DEB_BUILD} --host ${DEB_BUILD}
	make CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
	touch build

clean:
	test -f debian/rules
	-rm -f build
	[ ! -f Makefile ] || $(MAKE) clean
	-rm -f debian/*~ core
	-rm -f config.log config.cache config.status Makefile
	-rm -f mpack.1 munpack.1
	-rm -rf debian/tmp debian/files* debian/substvars

binary-indep:

binary-arch: build
	test root = "`whoami`"
	test -f debian/rules
	-rm -rf debian/tmp
# programs
	install -d ${BINDIR}
	${STRIP} mpack
	${STRIP} munpack
	install -m 755 mpack ${BINDIR}
	install -m 755 munpack ${BINDIR}
# manpages
	install -d ${MANDIR}/man1
	install -p -m 644 unixpk.man ${MANDIR}/man1/mpack.1
	install -p -m 644 unixunpk.man ${MANDIR}/man1/munpack.1
	gzip -9n ${MANDIR}/man1/*.1
# documentation
	install -d ${DOCDIR}
	install -p -m 644 README.unix ${DOCDIR}
	install -p -m 644 debian/changelog ${DOCDIR}/changelog.Debian
	gzip -9n ${DOCDIR}/*
	install -p -m 644 debian/copyright ${DOCDIR}
# control files
	install -d ${DEBDIR}
	dpkg-shlibdeps ${BINDIR}/*
	dpkg-gencontrol -isp
	dpkg-deb --build debian/tmp ..

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
