#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

VERSION = 5.4.0
SONAME = 0
BUILD_SSL = yes
# NB "wwwssl" is missing from this list, it's special-cased below
LIBNAMES = wwwzip xmlparse wwwcore md5 pics wwwgopher wwwftp wwwmime \
	wwwstream wwwhtml wwwutils wwwdir wwwcache wwwhttp wwwxml xmltok \
	wwwapp wwwinit wwwmux wwwfile wwwtrans wwwnews wwwtelnet

# Nonstandard target: Re-libtoolize and bootstrap sources as described
# in the HTML docs.
bootstrap:
	perl config/winConfigure.pl
	rm -f configure.in acconfig.h
	libtoolize -c -f
	aclocal-1.9
	autoheader2.50
	automake-1.9 -a -c
	autoconf2.50

# The libwww 5.4.0 source tarball seems to contain files with "wrong"
# timestamps, causing builds to fail in not-really-reproducible ways. 
# We touch everything to fix this.
# "Library/src/.libs/.libs" is a kludge for libtool 1.5-2.
configure: configure-stamp
configure-stamp:
	dh_testdir
	rm -f configure.in acconfig.h
	find -type f | sort | xargs touch
	touch aclocal.m4 configure.in configure
	find -name Makefile.in | sort | xargs touch
	mkdir build build-ssl
	cd build && "$(CURDIR)/configure" --prefix=/usr --with-ssl=no \
	    --with-zlib --with-expat --with-gnu-ld --enable-shared
	mkdir -p build/Library/src/.libs
	ln -s . build/Library/src/.libs/.libs
ifeq ($(BUILD_SSL),yes)
	cd build-ssl && "$(CURDIR)/configure" --prefix=/usr --with-ssl=yes \
	    --with-zlib --with-expat --with-gnu-ld --enable-shared
	mkdir -p build-ssl/Library/src/.libs
	ln -s . build-ssl/Library/src/.libs/.libs
endif
	touch configure-stamp

build:	configure-stamp build-stamp
build-stamp:
	dh_testdir
	$(MAKE) -C build
ifeq ($(BUILD_SSL),yes)
	$(MAKE) -C build-ssl
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f conftest.o config.cache config.log config.status
	rm -f html.tar html.tar.bz2
	rm -rf debian/libwww0 debian/libwww0.tmp debian/libwww-dev \
	    debian/libwww-ssl0 debian/libwww-ssl-dev
	rm -rf build build-ssl
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	debian/rules install-sub SSL=""     PKG="libwww$(SONAME)"
ifeq ($(BUILD_SSL),yes)
	debian/rules install-sub SSL="-ssl" PKG="libwww-ssl$(SONAME)"
endif

# The symlink /usr/share/libwww/examples -> ../doc/libwww[-ssl]-dev/examples
# is used by libwww-doc to symlink to the examples.
.PHONY:	install-sub
install-sub: html.tar
	$(MAKE) -C build$(SSL) install DESTDIR="$(CURDIR)/debian/$(PKG)"
	mv debian/$(PKG)/usr/include debian/libwww$(SSL)-dev/usr
	mv debian/$(PKG)/usr/lib/*.a debian/$(PKG)/usr/lib/*.la \
	    debian/$(PKG)/usr/lib/*.so debian/libwww$(SSL)-dev/usr/lib
	mv debian/$(PKG)/usr/bin/libwww-config \
	    debian/libwww$(SSL)-dev/usr/bin
	if test ! -f debian/$(PKG)/usr/lib/libwwwapp.so.0.1.0; then \
	    echo " *** libwwwapp failed to build ***"; \
	    exit 1; \
	fi
	docs="debian/libwww$(SSL)-dev/usr/share/doc/libwww$(SSL)-dev"; \
	    cp -a Library/Examples/*.c $$docs/examples; \
	    cp -a debian/examples-Makefile $$docs/examples/Makefile; \
	for prog in w3c webbot www; do \
	    sed "s/@PACKAGE@/$(PKG)/g" <debian/$$prog.1 \
	        >debian/$(PKG)/usr/share/man/man1/$$prog.1; \
	done
	tar -C debian/$(PKG)/usr/share/doc/$(PKG) -xvf "$(CURDIR)/html.tar"
	ln -s ../doc/libwww$(SSL)-dev/examples \
	    debian/libwww$(SSL)-dev/usr/share/libwww/examples

html.tar: html.tar.bz2
	bzip2 -d -k html.tar.bz2
	touch $@
html.tar.bz2: debian/html.tar.bz2.mime
	munpack debian/html.tar.bz2.mime

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
# The particular order of the *shlib* calls is necessary to avoid
# lintian package-relation-with-self warnings.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
#	dh_installman
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress -X /examples/ -X /html/
	dh_fixperms
	dh_makeshlibs -plibwww-ssl$(SONAME) \
	    -V'libwww-ssl$(SONAME) (>= $(VERSION))'
	dh_shlibdeps -plibwww-ssl$(SONAME)
	dh_makeshlibs -plibwww$(SONAME) \
	    -V'libwww$(SONAME) (>= $(VERSION)) | libwww-ssl$(SONAME) (>= $(VERSION))'
	dh_shlibdeps -plibwww$(SONAME)
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
