#!/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=zomg

CONFIGFLAGS = --prefix=/usr --mandir=\$${prefix}/share/man --libexecdir=\$${prefix}/lib/zomg

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

CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)

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

build: build-arch build-indep

build-indep:

build-arch: build-stamp

build-stamp: config.status
	$(checkdir)

	$(MAKE)
	touch $@

clean: checkroot
	rm -f debian/zomg.substvars
	rm -rf debian/zomg

	test ! -f Makefile || $(MAKE) distclean

	rm -rf autom4te.cache

install: checkroot build
	rm -rf debian/zomg
	$(INSTALL_DIR) debian/zomg/usr/bin \
		debian/zomg/usr/share/doc/zomg

	# Add here commands to install the package into debian/zomg.
	$(MAKE) DESTDIR=$(CURDIR)/debian/zomg install


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

# Build architecture-dependent files here.
binary-arch: checkroot build install
	gzip -9fn debian/zomg/usr/share/man/man1/zomg*.1
	$(INSTALL_FILE) debian/changelog debian/zomg/usr/share/doc/zomg/changelog.Debian
	gzip -9fn debian/zomg/usr/share/doc/zomg/changelog.Debian
	$(INSTALL_FILE) debian/copyright debian/zomg/usr/share/doc/zomg
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip --remove-section=.comment --remove-section=.note debian/zomg/usr/bin/zomghelper
	strip --remove-section=.comment --remove-section=.note debian/zomg/usr/lib/zomg/zomghelper-xspf
endif
	$(INSTALL_DIR) debian/zomg/DEBIAN
	dpkg-shlibdeps -Tdebian/zomg.substvars debian/zomg/usr/bin/zomghelper \
		debian/zomg/usr/lib/zomg/zomghelper-xspf

	dpkg-gencontrol -ldebian/changelog -isp -Tdebian/zomg.substvars -Pdebian/zomg
	cd debian/zomg && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
	chown -R 0:0 debian/zomg
	chmod -R a+rX debian/zomg
	dpkg-deb --build debian/zomg ..

binary: binary-indep binary-arch

define checkdir
	test -f debian/rules
endef

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

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