#! /usr/bin/make -f

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

b = $(shell pwd)/debian/build
STRIP=/usr/bin/strip --remove-section=.note --remove-section=.comment --strip-all

ifeq ($(DEB_HOST_ARCH),arm)
MULTYPE=''
else
MULTYPE='-DUSE_FLOAT_MUL'
endif

build: checkdir
	$(MAKE) CCFLAGS='-c -g -O2 -fPIC -Wall -D_GNU_SOURCE -D_REENTRANT -DNeedFunctionPrototypes=1 -DWAV49' MULHACK=$(MULTYPE) all


clean: checkdir
	rm -rf debian/build debian/files debian/substvars
	find . -name \*~ -print0 | xargs -0 rm -f
	$(MAKE) clean

binary:	binary-indep binary-arch

binary-indep: build checkroot

binary-arch: checkdir build checkroot

	rm -rf debian/build debian/files debian/substvars

	install -d $(b)/lib/DEBIAN
	install -d $(b)/dev/DEBIAN
	install -d $(b)/tools/DEBIAN

	install -d $(b)/lib/usr/lib
	mv lib/*.so.1.* $(b)/lib/usr/lib/
	mv lib/*.so.1 $(b)/lib/usr/lib/

	install -d $(b)/tools/usr/bin
	install -d $(b)/tools/usr/lib/mime/packages
	install -d $(b)/tools/usr/share/man/man1
	install -d $(b)/tools/usr/share/doc

	install -d $(b)/dev/usr/lib
	install -d $(b)/dev/usr/include/gsm
	install -d $(b)/dev/usr/share/man/man3
	install -d $(b)/dev/usr/share/doc

	mv bin/* $(b)/tools/usr/bin/
	$(STRIP) $(b)/tools/usr/bin/toast
	install -c man/toast.1 $(b)/tools/usr/share/man/man1/
	gzip --best $(b)/tools/usr/share/man/man1/*
	ln -sf toast.1.gz $(b)/tools/usr/share/man/man1/tcat.1.gz
	ln -sf toast.1.gz $(b)/tools/usr/share/man/man1/untoast.1.gz
	install -d $(b)/tools/usr/share/doc/libgsm-tools
	install debian/changelog $(b)/tools/usr/share/doc/libgsm-tools/changelog.Debian
	install -c ChangeLog $(b)/tools/usr/share/doc/libgsm-tools
	gzip --best $(b)/tools/usr/share/doc/libgsm-tools/*
	( cd $(b)/tools/usr/share/doc/libgsm-tools && ln -s ChangeLog.gz changelog.gz )
	install debian/copyright $(b)/tools/usr/share/doc/libgsm-tools/copyright

	# At least one program (vat) includes one of those headers
	install -c inc/*.h $(b)/dev/usr/include/gsm/
	# This makes us upstream compatible
	ln -s gsm/gsm.h $(b)/dev/usr/include/gsm.h
	mv lib/*.so $(b)/dev/usr/lib/
	mv lib/*.a $(b)/dev/usr/lib/
	install -c man/*.3 $(b)/dev/usr/share/man/man3/
	gzip --best $(b)/dev/usr/share/man/man3/*
	ln -sf gsm.3.gz $(b)/dev/usr/share/man/man3/gsm_create.3.gz
	ln -sf gsm.3.gz $(b)/dev/usr/share/man/man3/gsm_destroy.3.gz
	ln -sf gsm.3.gz $(b)/dev/usr/share/man/man3/gsm_decode.3.gz
	ln -sf gsm.3.gz $(b)/dev/usr/share/man/man3/gsm_encode.3.gz
	ln -sf gsm_explode.3.gz $(b)/dev/usr/share/man/man3/gsm_implode.3.gz
	cd $(b)/dev/usr/share/doc && ln -s libgsm1 libgsm1-dev

	install -d $(b)/lib/usr/share/doc/libgsm1
	install -c ChangeLog $(b)/lib/usr/share/doc/libgsm1
	install -c MACHINES README $(b)/lib/usr/share/doc/libgsm1/
	install debian/changelog $(b)/lib/usr/share/doc/libgsm1/changelog.Debian
	gzip --best $(b)/lib/usr/share/doc/libgsm1/*
	( cd $(b)/lib/usr/share/doc/libgsm1 && ln -s ChangeLog.gz changelog.gz )

	install debian/copyright $(b)/lib/usr/share/doc/libgsm1/copyright
	
	install -m 644 debian/shlibs $(b)/lib/DEBIAN/shlibs
	install -m 755 debian/postinst $(b)/lib/DEBIAN/postinst
	install -m 755 debian/postinst-tools $(b)/tools/DEBIAN/postinst
	install -m 755 debian/postrm-tools $(b)/tools/DEBIAN/postrm
	install -m 644 debian/mime $(b)/tools/usr/lib/mime/packages/libgsm-tools

	-chown -Rf root.root $(b)
	find $(b) -type f -print0 | xargs -0 chmod 644
	find $(b) -type d -print0 | xargs -0 chmod 755
	chmod 0755 $(b)/tools/usr/bin/*
	chmod 0644 $(b)/lib/usr/lib/*.so*
	$(STRIP) $(b)/lib/usr/lib/*.so*
	dpkg-shlibdeps $(b)/lib/usr/lib/*.so*
	chmod 755 $(b)/tools/DEBIAN/postrm
	chmod 755 $(b)/tools/DEBIAN/postinst
	chmod 755 $(b)/lib/DEBIAN/postinst

	dpkg-gencontrol -isp -plibgsm1 -P$(b)/lib
	dpkg --build $(b)/lib ..

	dpkg-gencontrol -isp -plibgsm1-dev -P$(b)/dev
	dpkg --build $(b)/dev ..

	dpkg-shlibdeps $(b)/tools/usr/bin/*
	dpkg-gencontrol -isp -plibgsm-tools -P$(b)/tools
	dpkg --build $(b)/tools ..

checkdir:
	test -d tls

checkroot:
	test "`whoami`" = "root"
