#!/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=libmsv0
devpkg=libmsv-dev

build: stamp-build
stamp-build: configure
	./configure --prefix=/usr
	$(MAKE)
	touch $@

clean:
	$(checkdir)
	rm -rf debian/$(package) debian/$(devpkg) debian/*.substvars

binary: binary-arch
binary-indep: checkroot

binary-arch: checkroot
	$(INSTALL_DIR) debian/$(package)/usr/share/doc/$(package) \
		debian/$(devpkg)/usr/share/doc/$(devpkg) \
		debian/$(devpkg)/usr/lib \
		debian/$(package)/DEBIAN \
		debian/$(devpkg)/DEBIAN

	$(MAKE) install DESTDIR=$(CURDIR)/debian/$(package)

	$(INSTALL_FILE) debian/copyright debian/$(package)/usr/share/doc/$(package)
	$(INSTALL_FILE) debian/changelog debian/$(package)/usr/share/doc/$(package)/changelog.Debian
	gzip -9f debian/$(package)/usr/share/doc/$(package)/changelog.Debian

	mv debian/$(package)/usr/lib/*.a debian/$(devpkg)/usr/lib
	mv debian/$(package)/usr/lib/*.so debian/$(devpkg)/usr/lib
	mv debian/$(package)/usr/include debian/$(devpkg)/usr
	rm -f debian/$(package)/usr/lib/*.la
	chmod 644 debian/$(package)/usr/lib/*.so.*

	$(INSTALL_FILE) debian/copyright debian/$(devpkg)/usr/share/doc/$(devpkg)
	$(INSTALL_FILE) debian/changelog debian/$(devpkg)/usr/share/doc/$(devpkg)/changelog.Debian
	gzip -9f debian/$(devpkg)/usr/share/doc/$(devpkg)/changelog.Debian

	strip debian/$(package)/usr/lib/*.so.*
	$(INSTALL_SCRIPT) debian/$(package).postinst debian/$(package)/DEBIAN/postinst
	cd debian/$(package) && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
	echo 'libmsv 0 $(package)' >debian/$(package)/DEBIAN/shlibs
	dpkg-shlibdeps -Tdebian/$(package).substvars -dDepends debian/$(package)/usr/lib/*.so.*
	dpkg-gencontrol -ldebian/changelog -isp -p$(package) -Tdebian/$(package).substvars -Pdebian/$(package)
	chown -R root:root debian/$(package)
	chmod -R go=rX debian/$(package)
	dpkg --build debian/$(package) ..

	cd debian/$(devpkg) && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
	dpkg-gencontrol -ldebian/changelog -isp -p$(devpkg) -Tdebian/$(devpkg).substvars -Pdebian/$(devpkg)
	chown -R root:root debian/$(devpkg)
	chmod -R go=rX debian/$(devpkg)
	dpkg --build debian/$(devpkg) ..


define checkdir
	test -f debian/rules
endef

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

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