#!/usr/bin/make -f
# -*- makefile -*-

export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog --count 1 -SDate)" +%s)

# Package name
PACKAGE_NAME = supercat

# Install program
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


# The installation directory
PACKAGE_DIR = debian/$(PACKAGE_NAME)

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

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

configure:
	cp -f configure.ac configure.ac.bak
	autoupdate configure.ac
	autoreconf --force --install --symlink --warnings=all
	./configure $(CROSS) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"

config.status: configure
	$(checkdir)
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif


build: build-stamp

build-stamp: config.status
	$(checkdir)
	$(MAKE)
	touch $@

clean: checkroot
	$(checkdir)
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.sub config.guess config.log
	rm -rf $(PACKAGE_DIR) debian/files debian/substvars
	test -e configure.ac.bak && mv -f configure.ac.bak configure.ac || true
	rm -f Makefile.in */Makefile.in aclocal.m4 config.h.in configure
	rm -f compile install-sh missing depcomp
	rm -rf autom4te.cache/

install: checkroot build
	$(checkdir)
	$(MAKE) DESTDIR=$(CURDIR)/$(PACKAGE_DIR) install

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

# Build architecture-dependent files here.
binary-arch: checkroot build install
	$(checkdir)
	$(INSTALL_DIR) $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/
	for i in doc/spc.txt debian/README.Debian;do \
		$(INSTALL_FILE) -m 644 $$i $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/; \
	done;
	$(INSTALL_FILE) -m 644 ChangeLog $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/changelog
	$(INSTALL_FILE) -m 644 debian/changelog $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/changelog.Debian
	for i in $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/changelog*;do \
		gzip -9nv $$i; \
	done;
	$(INSTALL_FILE) -m 644 debian/copyright $(PACKAGE_DIR)/usr/share/doc/$(PACKAGE_NAME)/copyright
	$(INSTALL_DIR) $(PACKAGE_DIR)/etc/$(PACKAGE_NAME)
	$(INSTALL_DIR) $(PACKAGE_DIR)/usr/share/man/man1
	$(INSTALL_FILE) -m 644 doc/spc.1 $(PACKAGE_DIR)/usr/share/man/man1
	gzip -9nv $(PACKAGE_DIR)/usr/share/man/*/*
# Strip binaries (including hack by policy wonks)
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip -R.note -R.comment $(PACKAGE_DIR)/usr/bin/*
endif
	dpkg-shlibdeps $(PACKAGE_DIR)/usr/bin/spc
	$(INSTALL_DIR) $(PACKAGE_DIR)/DEBIAN
	cd $(PACKAGE_DIR) && ls etc/$(PACKAGE_NAME)/*|sed 's|^|/|' > DEBIAN/conffiles
	cd $(PACKAGE_DIR) && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
	dpkg-gencontrol -p$(PACKAGE_NAME) -P$(PACKAGE_DIR)
	find $(PACKAGE_DIR) -newermt "@$$SOURCE_DATE_EPOCH" -print0 | \
		xargs -0r touch --no-dereference --date="@$$SOURCE_DATE_EPOCH"
	dpkg-deb --build $(PACKAGE_DIR) ..

binary: binary-indep binary-arch

define checkdir
	test -f src/spc.h && test -f debian/rules
endef

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


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