#! /usr/bin/make -f
# Copyright © 2010-2018 Richard Kettlewell.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

INSTALL=install
STRIPARGS=--strip-unneeded --remove-section=.comment --remove-section=.note

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

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

# Debian apparently wants reproducible builds but won't execute them
# in a consistent environment, so every project has to clean up after
# it.
export LC_ALL=C.UTF-8

build-arch: build
build-indep: build
build:
	[ -e configure ] || autoreconf -si
	./configure --prefix=/usr --mandir=/usr/share/man --disable-silent-rules ${CONFIGURE_EXTRA}
	$(MAKE)

clean-rsbackup:
	rm -rf debian/rsbackup

binary-rsbackup: build
	rm -rf debian/rsbackup
	mkdir -p debian/rsbackup/DEBIAN
	mkdir -p debian/rsbackup/etc/rsbackup/hosts.d
	mkdir -p debian/rsbackup/etc/cron.hourly
	mkdir -p debian/rsbackup/etc/cron.daily
	mkdir -p debian/rsbackup/etc/cron.weekly
	mkdir -p debian/rsbackup/etc/cron.monthly
	mkdir -p debian/rsbackup/usr/share/doc/rsbackup
	mkdir -p debian/rsbackup/var/log/backup
	mkdir -p debian/rsbackup/usr/share/doc-base
	mkdir -p debian/rsbackup/usr/bin
	mkdir -p debian/rsbackup/usr/share/man/man1
	mkdir -p debian/rsbackup/usr/share/man/man5
	cp debian/rsbackup.conffiles debian/rsbackup/DEBIAN/conffiles
	install debian/rsbackup.postinst debian/rsbackup/DEBIAN/postinst
	install debian/rsbackup.postrm debian/rsbackup/DEBIAN/postrm
	install -m 755 tools/rsbackup.hourly debian/rsbackup/etc/cron.hourly/rsbackup
	install -m 755 tools/rsbackup.daily debian/rsbackup/etc/cron.daily/rsbackup
	install -m 755 tools/rsbackup.weekly debian/rsbackup/etc/cron.weekly/rsbackup
	install -m 755 tools/rsbackup.monthly debian/rsbackup/etc/cron.monthly/rsbackup
	cp tools/rsbackup.config debian/rsbackup/etc/rsbackup/config
	cp tools/rsbackup.defaults debian/rsbackup/etc/rsbackup/defaults
	cp tools/rsbackup.devices debian/rsbackup/etc/rsbackup/devices
	cp debian/changelog debian/rsbackup/usr/share/doc/rsbackup/changelog.Debian
	cp debian/NEWS debian/rsbackup/usr/share/doc/rsbackup/NEWS.Debian
	cp debian/doc.rsbackup debian/rsbackup/usr/share/doc-base/rsbackup
	cp README.md debian/rsbackup/usr/share/doc/rsbackup/.
	cp doc/CHANGES.md debian/rsbackup/usr/share/doc/rsbackup/changelog
	gzip -9nv debian/rsbackup/usr/share/doc/rsbackup/*
	cp doc/*.html doc/*.css debian/rsbackup/usr/share/doc/rsbackup/.
	rm -f debian/rsbackup/usr/share/doc/rsbackup/*.prefix.html
	rm -f debian/rsbackup/usr/share/doc/rsbackup/*.in.html
	cp debian/copyright debian/rsbackup/usr/share/doc/rsbackup/.
	$(INSTALL) -m 755 src/rsbackup debian/rsbackup/usr/bin/rsbackup
	$(MAKE) -C tools install DESTDIR=$(shell pwd)/debian/rsbackup
	$(INSTALL) -m 644 doc/rsbackup.1 \
			doc/rsbackup.cron.1 \
			doc/rsbackup-mount.1 \
			doc/rsbackup-snapshot-hook.1 \
		debian/rsbackup/usr/share/man/man1/
	$(INSTALL) -m 644 doc/rsbackup.5 debian/rsbackup/usr/share/man/man5/
	strip $(STRIPARGS) debian/rsbackup/usr/bin/rsbackup
	gzip -9nv debian/rsbackup/usr/share/man/man*/*
	dpkg-shlibdeps -Tdebian/substvars.rsbackup \
		debian/rsbackup/usr/bin/*
	cd debian/rsbackup && \
	  find -name DEBIAN -prune -o -type f -print \
	    | sed 's/^\.\///' \
			| sort | xargs md5sum > DEBIAN/md5sums
	dpkg-gencontrol -isp -prsbackup -Pdebian/rsbackup \
		-Tdebian/substvars.rsbackup
	chown -R root:root debian/rsbackup
	chmod -R g-ws debian/rsbackup
	find debian/rsbackup -newermt "@$$SOURCE_DATE_EPOCH" -print0 | \
		xargs -0r touch --no-dereference --date="@$$SOURCE_DATE_EPOCH"
	dpkg --build debian/rsbackup ..

clean-rsbackup-graph:
	rm -rf debian/rsbackup-graph

binary-rsbackup-graph: build
	rm -rf debian/rsbackup-graph
	mkdir -p debian/rsbackup-graph/DEBIAN
	mkdir -p debian/rsbackup-graph/usr/bin
	mkdir -p debian/rsbackup-graph/usr/share/man/man1
	mkdir -p debian/rsbackup-graph/usr/share/doc
	ln -s rsbackup \
		debian/rsbackup-graph/usr/share/doc/rsbackup-graph
	$(INSTALL) -m 755 src/rsbackup-graph \
		debian/rsbackup-graph/usr/bin/rsbackup-graph
	$(INSTALL) -m 644 doc/rsbackup-graph.1 \
		debian/rsbackup-graph/usr/share/man/man1/
	strip $(STRIPARGS) debian/rsbackup-graph/usr/bin/rsbackup-graph
	gzip -9nv debian/rsbackup-graph/usr/share/man/man*/*
	dpkg-shlibdeps -Tdebian/substvars.rsbackup-graph \
		debian/rsbackup-graph/usr/bin/*
	cd debian/rsbackup-graph && \
	  find -name DEBIAN -prune -o -type f -print \
	    | sed 's/^\.\///' \
			| xargs md5sum > DEBIAN/md5sums
	dpkg-gencontrol -isp -prsbackup-graph -Pdebian/rsbackup-graph \
		-Tdebian/substvars.rsbackup-graph
	chown -R root:root debian/rsbackup-graph
	chmod -R g-ws debian/rsbackup-graph
	find debian/rsbackup-graph -newermt "@$$SOURCE_DATE_EPOCH" -print0 | \
		xargs -0r touch --no-dereference --date="@$$SOURCE_DATE_EPOCH"
	dpkg --build debian/rsbackup-graph ..

binary: binary-arch binary-indep
binary-arch: binary-rsbackup binary-rsbackup-graph
binary-indep:

clean: clean-rsbackup clean-rsbackup-graph
	rm -f debian/files
	rm -f debian/debhelper.log
	[ ! -f Makefile ] || $(MAKE) distclean
