#!/usr/bin/make -f

SHELL = /bin/bash
INSTALL_PROGRAM = install

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif


build:
	./configure  --host=$(DEB_HOST_GNU_TYPE)      \
			   --build=$(DEB_BUILD_GNU_TYPE)    \
			   --prefix=/usr                    \
			   --mandir=\$${prefix}/share/man   \
			   --infodir=\$${prefix}/share/info \
			   --sysconfdir=/etc                \
			   --disable-epoll
	make
	touch build

clean:
	rm -f build
	test -f Makefile && make -i distclean || true
	rm -rf *~ debian/tmp* debian/*~ debian/files* debian/substvars

checkroot:
	$(checkdir)
	test $$(id -u) = 0

binary: binary-indep binary-arch

binary-indep:

binary-arch: checkroot build
	install -d debian/tmp
	make install INSTALL_PROGRAM=$(INSTALL_PROGRAM) CFLAGS=$(CFLAGS) DESTDIR=`pwd`/debian/tmp
	chmod -R -x debian/tmp/usr/lib/{,cherokee/}*.{so*,la}

# cherokee
	install -d debian/tmp_bin/{DEBIAN,usr/bin}
	install -d debian/tmp_bin/etc/{init.d,logrotate.d,cherokee}
	install -d debian/tmp_bin/var/{www,log}
	install -d debian/tmp_bin/usr/share/{doc/cherokee,cherokee/icons,man/man1}

	strip -R .comment -R .note debian/tmp/usr/bin/cherokee
	strip -R .comment -R .note --strip-unneeded debian/tmp/usr/lib/{,cherokee/}*so*

	mv debian/tmp/usr/bin/cherokee                   debian/tmp_bin/usr/bin/
	mv debian/tmp/usr/share/cherokee/*.txt           debian/tmp_bin/usr/share/cherokee/
	mv debian/tmp/usr/share/cherokee/icons/*         debian/tmp_bin/usr/share/cherokee/icons/

	install -m 644 debian/conffiles                  debian/tmp_bin/DEBIAN
	install -m 755 debian/{prerm,postinst,postrm}    debian/tmp_bin/DEBIAN
	install -m 755 debian/cherokee.rc                debian/tmp_bin/etc/init.d/cherokee
	install -m 644 debian/cherokee.logrotate         debian/tmp_bin/etc/logrotate.d/cherokee
	install -m 644 debian/tmp/etc/cherokee/*conf     debian/tmp_bin/etc/cherokee/

	gzip -9c debian/tmp/usr/share/man/man1/cherokee.1 >debian/tmp_bin/usr/share/man/man1/cherokee.1.gz
	gzip -9c README                                   >debian/tmp_bin/usr/share/doc/cherokee/README.gz

	dpkg-shlibdeps -pcherokee debian/tmp_bin/usr/bin/*
	dpkg-gencontrol -Pdebian/tmp_bin -pcherokee -isp


# libcherokee0 and libcherokee0-dev
	install -d debian/tmp_{lib,dev}/DEBIAN
	install -d debian/tmp_lib/usr/lib/{pkgconfig,cherokee}
	install -d debian/tmp_lib/usr/share/{cherokee,aclocal,doc/libcherokee0}
	install -d debian/tmp_dev/usr/{bin,lib/pkgconfig,include/cherokee}
	install -d debian/tmp_dev/usr/share/{doc/libcherokee0-dev,aclocal}

	mv debian/tmp/usr/lib/libcherokee.{a,so}         debian/tmp_dev/usr/lib/
	mv debian/tmp/usr/include/cherokee/*             debian/tmp_dev/usr/include/cherokee/
	mv debian/tmp/usr/bin/cherokee-config            debian/tmp_dev/usr/bin
	mv debian/tmp/usr/lib/pkgconfig/*                debian/tmp_dev/usr/lib/pkgconfig/
	mv debian/tmp/usr/share/aclocal/*                debian/tmp_dev/usr/share/aclocal/

	mv debian/tmp/usr/lib/libcherokee*.so*           debian/tmp_lib/usr/lib/
	mv debian/tmp/usr/lib/cherokee/libcherokee_*.so* debian/tmp_lib/usr/lib/cherokee/

	install -m 644 debian/shlibs                     debian/tmp_lib/DEBIAN

	dpkg-shlibdeps -plibcherokee0 debian/tmp_lib/usr/lib/libcherokee.so*
	dpkg-shlibdeps -plibcherokee0 debian/tmp_lib/usr/lib/cherokee/libcherokee_*.so

	dpkg-gencontrol -Pdebian/tmp_lib -plibcherokee0 -isp
	dpkg-gencontrol -Pdebian/tmp_dev -plibcherokee0-dev -isp


# Common documentation files
	install -m 644 debian/copyright AUTHORS debian/tmp_bin/usr/share/doc/cherokee
	install -m 644 debian/copyright AUTHORS debian/tmp_lib/usr/share/doc/libcherokee0
	install -m 644 debian/copyright AUTHORS debian/tmp_dev/usr/share/doc/libcherokee0-dev

	gzip -9c ChangeLog        > debian/tmp/changelog.gz 
	gzip -9c debian/changelog > debian/tmp/changelog.Debian.gz 

	install -m 644 debian/tmp/changelog.* debian/tmp_bin/usr/share/doc/cherokee/
	install -m 644 debian/tmp/changelog.* debian/tmp_lib/usr/share/doc/libcherokee0/
	install -m 644 debian/tmp/changelog.* debian/tmp_dev/usr/share/doc/libcherokee0-dev/


# Generate de packages
	chown -R root.root debian/tmp*
	chmod -R g-ws debian/tmp*

	dpkg --build debian/tmp_bin ..
	dpkg --build debian/tmp_lib ..
	dpkg --build debian/tmp_dev ..



define checkdir
	test -f debian/rules -a -f configure.in
endef

.PHONY: clean binary-indep binary-arch binary
