#!/usr/bin/make -f

SHELL=/bin/bash

subdirs_build_arch=	cprogs
subdirs_nobuild=backup sync-accounts scripts
package=	chiark-utils
packages_indep=	chiark-backup chiark-scripts
packages_arch=	chiark-rwbuffer chiark-really chiark-utils-bin
packages=	$(packages_indep) $(packages_arch)

cwd=	$(shell pwd)
d=	$(cwd)/debian
t=	$d/tmp

makebuildargs := OPTIMISE= DEBUG= \
 CMDLINE_CFLAGS="$(shell dpkg-buildflags --get CFLAGS)" \
 CMDLINE_CPPFLAGS="$(shell dpkg-buildflags --get CPPFLAGS)" \
 CMDLINE_LDFLAGS="$(shell dpkg-buildflags --get LDFLAGS)"

build:
	$(checkdir)
	set -e; for s in $(subdirs_build_arch); do \
		$(MAKE) -C $$s all $(makebuildargs); \
	done
	touch build

build-indep: build
build-arch: build

clean:
	$(checkdir)
	rm -f build
	set -e; for s in $(subdirs_build_arch); do \
		$(MAKE) -C $$s -i distclean || \
		$(MAKE) -C $$s -f Makefile.in distclean; \
	done
	rm -rf *~ debian/tmp debian/*~ debian/files* debian/substvars*
	rm -rf debian/sv-* debian/*.debhelper.log

binary-prep:
	$(checkdir)
	rm -rf debian/tmp*
	#
	set -e; for s in $(subdirs_build_arch) $(subdirs_nobuild); do \
		$(MAKE) -C $$s install install-docs install-examples \
			prefix=$t/$$s/usr \
			etcdir=$t/$$s/etc \
			varlib=$t/$$s/var/lib \
			mandir=$t/$$s/usr/share/man; \
	done
	#
	mv $t/cprogs $t/chiark-utils-bin
	#
	cp -a debian/tmp/sync-accounts/* debian/tmp/scripts/.
	rm -r debian/tmp/sync-accounts
	mv debian/tmp/scripts debian/tmp/chiark-scripts
	mv debian/tmp/backup debian/tmp/chiark-backup
	#
	set -e; for p in $(packages); do \
		install -d $t/$$p/DEBIAN $t/$$p/usr/share/doc/$$p; \
		cp debian/copyright debian/changelog \
			$t/$$p/usr/share/doc/$$p/; \
		ln -s changelog.gz \
			$t/$$p/usr/share/doc/$$p/changelog.Debian.gz; \
		gzip -9v $t/$$p/usr/share/doc/$$p/changelog; \
		done
	#
	install -d $t/chiark-rwbuffer/usr/bin
	install -d $t/chiark-rwbuffer/usr/share/man/man1
	cd $t/chiark-utils-bin/usr/bin && \
		mv readbuffer writebuffer $t/chiark-rwbuffer/usr/bin/
	cd $t/chiark-utils-bin/usr/share/man/man1 && \
		mv readbuffer.1 writebuffer.1 $t/chiark-rwbuffer/usr/share/man/man1/
	#
	install -d $t/chiark-backup/usr/share/man/man1
	cp backup/man/*.1 $t/chiark-backup/usr/share/man/man1/
	cd $t/chiark-backup/usr/share/man/man1 && \
		for m in *.1; do \
			mv "$$m" backup-"$$m"; \
		done
	cp \
 $t/chiark-backup/usr/share/doc/chiark-backup/examples/chiark/settings.sh \
 $t/chiark-backup/etc/chiark-backup/settings.sh
	#
	install -d $t/chiark-really/usr/sbin
	install -d $t/chiark-really/usr/share/man/man8
	cd $t/chiark-utils-bin/usr/sbin && \
		mv really $t/chiark-really/usr/sbin/
	cd $t/chiark-utils-bin/usr/share/man/man8 && \
		mv really.8 $t/chiark-really/usr/share/man/man8/
	rm	$t/chiark-utils-bin/usr/sbin/trivsoundd \
		$t/chiark-utils-bin/usr/share/man/man8/trivsoundd.8
	rmdir	$t/chiark-utils-bin/usr/sbin \
		$t/chiark-utils-bin/usr/share/man/man8
	#
	gzip -9f $t/*/usr/share/man/man*/*

binary-hook-chiark-backup:
binary-hook-chiark-rwbuffer:
binary-hook-sync-accounts:
binary-hook-chiark-scripts:
binary-hook-chiark-really:
binary-hook-chiark-utils-bin:

binary-one:
	set -e; for f in preinst postinst prerm postrm conffiles; do \
		test -f debian/$p/$$f || continue; \
		cp debian/$p/$$f $t/$p/DEBIAN/$$f; \
		chmod u=rwX,go=rX $t/$p/DEBIAN/$$f; \
	done
	dh_link -p$p -Pdebian/tmp/$p
	dpkg-gencontrol -isp -p$p -P$t/$p -Tdebian/sv-$p
	chown -R root.root debian/tmp
	chmod -R g-ws debian/tmp
	debian/rules binary-hook-$p
	dpkg --build $t/$p ..

binary-indep:	checkroot build binary-prep
	set -e; for p in $(packages_indep); do \
		debian/rules binary-one p=$$p; done

binary-arch:	checkroot build binary-prep
	$(checkdir)
	set -ex; for p in chiark-really chiark-utils-bin chiark-rwbuffer; do \
		dh_strip -p$$p -Pdebian/tmp/$$p; done
	dpkg-shlibdeps -Tdebian/sv-chiark-rwbuffer \
		$t/chiark-rwbuffer/usr/bin/*
	dpkg-shlibdeps -Tdebian/sv-chiark-really \
		$t/chiark-really/usr/sbin/*
	set -e; for f in $t/chiark-utils-bin/usr/bin/*; do \
		case "$$f" in \
		*/xbatmon-simple|*/xduplic-copier) \
					d=Suggests	;; \
		*/watershed|*/summer)	d=Recommends	;; \
		*)			d=Depends	;; \
		esac; \
		a="$$a -d$$d $$f"; \
	    done; set -x; \
	    dpkg-shlibdeps -Tdebian/sv-chiark-utils-bin $$a
	perl -i~ -pe '						'\
	 -e'	next unless m/^shlibs:/;			'\
	 -e'	s/$$/,/; s/=/=, /;				'\
	 -e'	s/, libgmp3(?:c2)?,/, libgmp3 | libgmp3c2,/; 	'\
	 -e'	s/=, /=/; s/,$$//;				'\
			debian/sv-*[!~]
	set -e; for p in $(packages_arch); \
		do debian/rules binary-one p=$$p; done

define checkdir
	test -f cprogs/writebuffer.c
endef

# Below here is fairly generic really

binary:		binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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

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