#!/usr/bin/make -f

package = recode

CC = gcc
CFLAGS = -g -Wall
INSTALL_PROGRAM = install

STRIP = true
ATRIP = true

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
 CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
 INSTALL_PROGRAM += -s
 STRIP=strip --strip-unneeded --remove-section=.comment --remove-section=.note
 ATRIP=strip --strip-debug
endif

build:
	touch aclocal.m4
	touch configure
	touch stamp-h.in
	touch config.h.in
	touch Makefile.in
	touch m4/Makefile.in
	touch doc/Makefile.in
	touch lib/Makefile.in
	touch libiconv/Makefile.in
	touch i18n/Makefile.in
	touch src/Makefile.in
	touch tests/Makefile.in
	touch contrib/Makefile.in
	./configure --prefix=/usr --without-included-gettext
	$(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)"
	$(MAKE) install DESTDIR=`pwd`/debian/tmp \
		INSTALL_PROGRAM="$(INSTALL_PROGRAM)"
	touch build

clean:
	rm -f build
	rm -f i18n/*.mo
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f `find . -name "*~"`
	rm -rf debian/tmp debian/files* core debian/substvars
	cd debian && rm -rf recode librecode0 librecode-dev recode-doc

binary-indep: build
	debian/rules recode-doc

binary-arch: build
	debian/rules recode
	debian/rules librecode0
	debian/rules librecode-dev

recode-doc: build
	rm -rf debian/$@
	install -d debian/$@/DEBIAN debian/$@/usr/share/doc/$@
	install -d debian/$@/usr/share/doc-base
	install -m 644 debian/$@.doc-base debian/$@/usr/share/doc-base/$@
	cd doc && texi2html -split_chapter $(package).texi
	mv doc/*.html debian/$@/usr/share/doc/$@
	ln -s $(package)_toc.html debian/$@/usr/share/doc/$@/index.html
	cp -p debian/copyright debian/$@/usr/share/doc/$@
	cp -p ChangeLog debian/$@/usr/share/doc/$@/changelog
	cp -p debian/changelog debian/$@/usr/share/doc/$@/changelog.Debian
	cd debian/$@/usr/share/doc/$@ && gzip -9 changelog changelog.Debian
	dpkg-gencontrol -p$@ -Pdebian/$@
	cd debian/$@ && \
		md5sum `find * -type f ! -regex "DEBIAN/.*"` > DEBIAN/md5sums
	chown -R 0:0 debian/$@
	chmod -R go=rX debian/$@
	dpkg --build debian/$@ ..

recode: build
	rm -rf debian/$@
	install -d debian/$@/DEBIAN debian/$@/usr/share/doc/$@
	cp -a debian/tmp/usr/share/locale debian/$@/usr/share
	cp -a debian/tmp/usr/info         debian/$@/usr/share
	cp -a debian/tmp/usr/man          debian/$@/usr/share
	cp -a debian/tmp/usr/bin          debian/$@/usr
	cp -p TODO NEWS THANKS doc/File-Latin1 debian/copyright \
		debian/$@/usr/share/doc/$@
	cp -p ChangeLog debian/$@/usr/share/doc/$@/changelog
	cp -p debian/changelog debian/$@/usr/share/doc/$@/changelog.Debian
	cd debian/$@/usr/share/doc/$@ && gzip -9 changelog changelog.Debian
	gzip -9 debian/$@/usr/share/info/*
	gzip -r9 debian/$@/usr/share/man
	LD_LIBRARY_PATH=`pwd`/debian/tmp/usr/lib:$$LD_LIBRARY_PATH \
		dpkg-shlibdeps debian/tmp/usr/bin/recode
	dpkg-gencontrol -p$(package) -Pdebian/$@
	cd debian/$@ && \
		md5sum `find * -type f ! -regex "DEBIAN/.*"` > DEBIAN/md5sums
	chown -R 0:0 debian/$@
	chmod -R go=rX debian/$@
	dpkg --build debian/$@ ..

librecode0: build
	rm -rf debian/$@
	install -d debian/$@/DEBIAN debian/$@/usr/share/doc/$@
	install -d debian/$@/usr/lib
	cp -a debian/tmp/usr/lib/librecode.so.0* debian/$@/usr/lib
	install -m 755 debian/$@.postinst debian/$@/DEBIAN/postinst
	install -m 755 debian/$@.postrm   debian/$@/DEBIAN/postrm
	install -m 644 debian/shlibs      debian/$@/DEBIAN
	cp -p debian/copyright debian/$@/usr/share/doc/$@
	cp -p ChangeLog debian/$@/usr/share/doc/$@/changelog
	cp -p debian/changelog debian/$@/usr/share/doc/$@/changelog.Debian
	cd debian/$@/usr/share/doc/$@ && gzip -9 changelog changelog.Debian
	$(STRIP) debian/$@/usr/lib/*.so.0.0.0
	chmod -x debian/$@/usr/lib/*
	dpkg-shlibdeps debian/$@/usr/lib/librecode.so.0
	dpkg-gencontrol -p$@ -Pdebian/$@
	cd debian/$@ && \
		md5sum `find * -type f ! -regex "DEBIAN/.*"` > DEBIAN/md5sums
	chown -R 0:0 debian/$@
	chmod -R go=rX debian/$@
	dpkg --build debian/$@ ..

librecode-dev: build
	rm -rf debian/$@
	install -d debian/$@/DEBIAN debian/$@/usr/share/doc
	install -d debian/$@/usr/lib
	ln -s librecode0 debian/$@/usr/share/doc/$@
	cp -a debian/tmp/usr/lib/*.so debian/$@/usr/lib
	cp -p debian/tmp/usr/lib/*.a  debian/$@/usr/lib
	cp -a debian/tmp/usr/include  debian/$@/usr
	$(ATRIP) debian/$@/usr/lib/*.a
	dpkg-gencontrol -p$@ -Pdebian/$@
	cd debian/$@ && \
		md5sum `find * -type f ! -regex "DEBIAN/.*"` > DEBIAN/md5sums
	chown -R 0:0 debian/$@
	chmod -R go=rX debian/$@
	dpkg --build debian/$@ ..

binary: binary-indep binary-arch

build-arch: build

build-indep: build

.PHONY: binary binary-arch binary-indep build-arch build-indep clean
.PHONY: recode librecode0 librecode-dev recode-doc
