#!/usr/bin/make -f
# Sample debian/rules file; from GNU Hello,  Copyright 1994,1995 by Ian Jackson.

package = debdelta

D = debian/debdelta

BUILD_DATE := $(shell dpkg-parsechangelog --show-field Date)

docdir = $(D)/usr/share/doc/$(package)
mandir = $(D)/usr/share/man/man1/

D2 = debian/debdelta-doc

docdir2 = $(D2)/usr/share/doc/$(package)
docdir2doc = $(D2)/usr/share/doc/$(package)-doc

PO_FILES := $(wildcard po/*.po)
MO_FILES := $(patsubst %.po,%.mo,$(PO_FILES))
LANGS := $(patsubst po/%.po,%,$(PO_FILES))

include /usr/share/dpkg/architecture.mk
-include /usr/share/dpkg/buildtools.mk
STRIP = $(DEB_HOST_GNU_TYPE)-strip
INSTALL_PROGRAM = install --strip-program=$(STRIP)

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

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

minibzip2: minigzip.c
	$(CC) -DBZIP $(CFLAGS) $(CPPFLAGS) minigzip.c -o minibzip2  $(LDFLAGS) -lbz2

minigzip: minigzip.c
	$(CC) $(CFLAGS) $(CPPFLAGS) minigzip.c -o minigzip $(LDFLAGS) -lz

build-arch: minibzip2 minigzip
	$(checkdir)
	touch build

build-indep:

build: build-arch build-indep

clean:
	$(checkdir)
	rm -f build *~ */*~ debian/files* debian/substvars
	rm -f minigzip minibzip2
	rm -rf $(D) $(D2)

binary-indep:	build
	$(checkdir)
	rm -rf $(D2)
	install -d $(D2)/DEBIAN  $(docdir2)  $(docdir2doc)
	cp -p doc/debdelta_suite.pdf debian/copyright debian/changelog $(docdir2doc)
	chmod -c 0644 $(docdir2doc)/*
	cd $(docdir2doc) && gzip -9n changelog
	cp -rp doc/html  $(docdir2doc)
	ln -s ../debdelta-doc/debdelta_suite.pdf  $(docdir2)
	ln -s ../debdelta-doc/html  $(docdir2)
	# build package
	dpkg-gencontrol -Pdebian/debdelta-doc -pdebdelta-doc
	find debian/debdelta-doc -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	# Ensure correct mode for files for reproducible builds
	chmod -c 0755 $(docdir2doc)/html/
	chmod -c 0644 $(docdir2doc)/html/*
	dpkg-deb --root-owner-group --build $(D2) ..

binary-arch:	build
	$(checkdir)
	rm -rf $(D)
	# dirs
	install -d $(D)/DEBIAN $(D)/usr/bin $(docdir) $(mandir) $(D)/usr/lib/debdelta $(D)/usr/share/debdelta $(D)/usr/share/keyrings
	set -e ; for L in $(LANGS) ; do install -d $(D)/usr/share/locale/$${L}/LC_MESSAGES ; done
	# control
	install -m 755 debian/postrm $(D)/DEBIAN/
	install -m 755 debian/postinst $(D)/DEBIAN/
	# /usr/share
	set -e ; for L in $(LANGS) ; do cp -p po/$${L}.mo $(D)/usr/share/locale/$${L}/LC_MESSAGES/debdelta.mo ; chmod -c 0644 $(D)/usr/share/locale/$${L}/LC_MESSAGES/debdelta.mo ; done
	install debpatch.sh $(D)/usr/share/debdelta
	install contrib/debmirror-delta-security $(D)/usr/share/debdelta/
	install contrib/debmirror-marshal-deltas $(D)/usr/share/debdelta/
	install contrib/debmarshal_list_useless_debs $(D)/usr/share/debdelta/
	install contrib/dpkg-sig $(D)/usr/share/debdelta
	cp -p contrib/debmirror*trash_option.patch  $(D)/usr/share/debdelta
	ln -s ../../bin/debdelta  $(D)/usr/share/debdelta/debpatch-url
	cp -p keyrings/pubring.gpg $(D)/usr/share/keyrings/debian-debdelta-archive-keyring.gpg
	chmod 0644 -c $(D)/usr/share/keyrings/debian-debdelta-archive-keyring.gpg
	# /usr/lib
	$(INSTALL_PROGRAM)  minigzip $(D)/usr/lib/debdelta/minigzip
	$(INSTALL_PROGRAM)  minibzip2 $(D)/usr/lib/debdelta/minibzip2
	# /etc
	install -d $(D)/etc/debdelta
	cp -p etc/sources.conf $(D)/etc/debdelta/sources.conf
	chmod -c 0644  $(D)/etc/debdelta/sources.conf
	echo /etc/debdelta/sources.conf >> $(D)/DEBIAN/conffiles
	# /usr/bin
	install debdelta $(D)/usr/bin/debdelta
	ln -s debdelta  $(D)/usr/bin/debpatch
	ln -s debdelta  $(D)/usr/bin/debdeltas
	ln -s debdelta  $(D)/usr/bin/debdelta-upgrade
	# /usr/share/doc
	cp -p FAQ README.features debian/copyright debian/changelog $(docdir)
	chmod -c 0644 $(docdir)/*
	cd $(docdir) && gzip -9n changelog FAQ
	# man
	cp -p *.1 $(mandir)
	chmod -c 0644 $(mandir)*.1
	gzip -9n $(mandir)/*.1 
	# build package
	dpkg-shlibdeps $(D)/usr/lib/debdelta/minigzip  $(D)/usr/lib/debdelta/minibzip2
	dpkg-gencontrol -Pdebian/debdelta -pdebdelta
	find debian/debdelta -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	# Ensure correct mode for files for reproducible builds
	chmod -c 0644 $(D)/usr/share/debdelta/*.patch
	dpkg-deb --root-owner-group --build $(D) ..

define checkdir
	test -f debdelta -a -f debian/rules
endef

binary: binary-indep binary-arch

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