#!/usr/bin/make -f
# Sample debian.rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified
#
# Modified to be a prototype for debmake by Christoph Lameter <clameter@debian.org>
SHELL=/bin/bash
export DH_COMPAT=3

include /usr/share/dpatch/dpatch.make

package=openssl

# For generating the manpages
export VERSION=$(shell dpkg-parsechangelog | grep '^Version:' | sed -e 's/^.*://' -e 's/-.*//')

# for debug symbol package
CFLAGS += -g

# Use soft-float and thumb mode if it enabled.
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif
export CFLAGS

# The binary architeture
DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)

CONFARGS  = --prefix=/usr --openssldir=/usr/lib/ssl no-idea no-mdc2 no-rc5 no-err
OPT_alpha = ev4 ev5
OPT_i386  = i486 i586 i686/cmov
OPT_sparc = v8 v9
ARCHOPTS  = OPT_$(DEB_HOST_ARCH)
OPTS      = $($(ARCHOPTS))
WANTED_LIBC_VERSION = 2.3.1-10

build: patch-stamp
	dh_testdir
	perl util/perlpath.pl /usr/bin
#	perl util/ssldir.pl /usr/lib/ssl
#	chmod +x debian/libtool
	./Configure no-shared $(CONFARGS) debian-$(DEB_HOST_ARCH)
	make -C crypto links
	make -f Makefile DIRS="crypto ssl" all
	mv libcrypto.a libcrypto.static
	mv libssl.a libssl.static
	make -f Makefile DIRS="crypto ssl" clean
	test -z "$(OPTS)" || for opt in $(OPTS); \
	do \
		set -xe; \
		./Configure shared $(CONFARGS) debian-$(DEB_HOST_ARCH)-$$opt; \
		make -f Makefile DIRS="crypto ssl" all; \
		mkdir -p $$opt; \
		mv libcrypto.so* libssl.so* $$opt/; \
		make -f Makefile DIRS="crypto ssl" clean; \
	done
	./Configure shared $(CONFARGS) debian-$(DEB_HOST_ARCH)
	#make -f Makefile depend
	ln -sf apps/openssl.pod crypto/crypto.pod ssl/ssl.pod doc/
#	make -f Makefile linux-shared
	make -f Makefile all
#	strip apps/openssl
#	make -f Makefile clean DIRS="crypto ssl"
#	./Configure --prefix=/usr --openssldir=/usr/lib/ssl no-idea no-mdc2 no-rc5 debian-$(DEB_HOST_ARCH)
#	make -f Makefile all DIRS="crypto ssl"
	touch build

clean: unpatch
	dh_testdir
	dh_testroot
	-rm -f build
	perl util/perlpath.pl /usr/bin
	./Configure $(CONFARGS) debian-$(DEB_HOST_ARCH)
	-make -f Makefile  clean clean-shared
	#-make -f Makefile  dclean
	perl util/perlpath.pl /usr/bin
#	perl util/ssldir.pl /usr/local/ssl
	-rm -f test/.rnd test/testkey.pem test/testreq.pem test/certCA.srl
	-rm -f util/mk1mf.bak Makefile.bak `find . -name Makefile.save` 
	-rm -f crypto/pem/ctx_size
	-rm -f `find . -name "*~"`
	-rm -f `find . -name "*.orig" -o -name "*.rej"`
	-rm -f certs/*.0 certs/*.1
#	-rm -rf debian/tmp debian/files* core `find debian/* -type d`
	-rm -rf core $(OPTS)
	-rm -f doc/*.pod
	-rm -f libcrypto.* libssl.*
	dh_clean

binary-indep:	build
	dh_testdir
	dh_testroot
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch:	build
	dh_testdir
	dh_testroot
	dh_clean
#	-rm -rf debian/tmp `find debian/* -type d`
	install -d debian/tmp debian/libssl0.9.7 debian/libssl-dev debian/ssleay/usr/share/doc
#	cd debian/tmp && install -d `cat ../dirs`
#	cd debian/libssl09 && install -d `cat ../libssl09.dirs`
#	cd debian/libssl09-dev && install -d `cat ../libssl09-dev.dirs`
	dh_installdirs
#openssl install
	make -f Makefile  install INSTALL_PREFIX=`pwd`/debian/tmp
	rm debian/tmp/usr/share/man/man1/openssl.1
	rm debian/tmp/usr/share/man/man3/crypto.3
	rm debian/tmp/usr/share/man/man3/ssl.3
#	rm debian/tmp/usr/lib/libcrypto.a
#	rm debian/tmp/usr/lib/libssl.a
	mv debian/tmp/usr/lib/libcrypto.a debian/tmp/usr/lib/libcrypto_pic.a
	mv debian/tmp/usr/lib/libssl.a debian/tmp/usr/lib/libssl_pic.a
	cp -pf libcrypto.static debian/tmp/usr/lib/libcrypto.a
	cp -pf libssl.static debian/tmp/usr/lib/libssl.a
#	mv debian/tmp/usr/lib/ssl/bin debian/tmp/usr/bin/ssl
#	(cd debian/tmp/usr/lib/ssl; ln -s /usr/bin/ssl bin)
#	mv debian/tmp/usr/lib/ssl/include debian/tmp/usr/include/ssl
#	(cd debian/tmp/usr/lib/ssl; ln -s /usr/include/ssl include)
#	chmod -x debian/tmp/usr/lib/*.so.*
#	mv debian/tmp/usr/lib/*.a debian/libssl09-dev/usr/lib/
#	mv debian/tmp/usr/lib/*.so debian/libssl09-dev/usr/lib/
#	mv debian/tmp/usr/lib/*.so.*.*.* debian/libssl09/usr/lib/
#	mv debian/tmp/usr/lib/*.la debian/libssl09-dev/usr/lib/
#	mv debian/tmp/usr/include debian/libssl09-dev/usr/
	mkdir -p debian/tmp/etc/ssl
	mv debian/tmp/usr/lib/ssl/{certs,openssl.cnf,private} debian/tmp/etc/ssl/
	ln -s /etc/ssl/{certs,openssl.cnf,private} debian/tmp/usr/lib/ssl/
	cp -pf debian/tmp/usr/lib/libcrypto.so.* debian/libcrypto0.9.7-udeb/usr/lib/
#ssleay install
	#ln -s openssl debian/ssleay/usr/share/doc/ssleay
#libssl install
#	install -m 644 libcrypto.so.08.1 libssl.so.08.1 debian/libssl08/usr/lib/
#	ln -s libssl.so.08 debian/libssl08-dev/usr/lib/libssl.so
#	ln -s libcrypto.so.08 debian/libssl08-dev/usr/lib/libcrypto.so
#	debian/libtool install -m 644 crypto/libcrypto.la
#	debian/libtool install -m 644 ssl/libssl.la
	cp -auv lib*.so* debian/tmp/usr/lib/
#	cp -auv lib*.a debian/tmp/usr/lib/
	for opt in $(OPTS); do set -xe; mkdir -p debian/tmp/usr/lib/$$opt; cp -auv $$opt/lib*.so* debian/tmp/usr/lib/$$opt/; done
	install debian/copyright debian/libssl0.9.7/usr/share/doc/libssl0.9.7/
#	install debian/changelog debian/libssl0.9.7/usr/share/doc/libssl0.9.7/changelog.Debian
	install debian/copyright debian/libssl-dev/usr/share/doc/libssl-dev/
	install debian/changelog debian/libssl-dev/usr/share/doc/libssl-dev/changelog.Debian
#	(cd debian/tmp/usr/doc/openssl/doc; for f in *.doc*; do mv "$$f" "$$(echo $$f | sed -e 's/doc/txt/')";done)
#	(cd doc; for f in *; do install "$$f" ../debian/tmp/usr/share/doc/openssl/doc/"$$(echo $$f | sed -e 's/doc/txt/')";done)
#	debstd -u CHANGES* LICENSE README NEWS
	dh_installdocs -plibssl-dev CHANGES.SSLeay LICENSE README NEWS debian/README.optimization
#	dh_installexamples
	dh_installchangelogs -plibssl-dev CHANGES
#	dh_installmenu
#	dh_installcron
	dh_installmanpages -popenssl
	dh_installdebconf
#	dh_undocumented c_rehash.1
	dh_movefiles
#	rmdir debian/tmp/usr/lib/ssl/lib
#	rmdir debian/tmp/usr/include/openssl
#	rmdir debian/tmp/usr/include
#	for opt in $(OPTS); do set -xe; rm -fr debian/tmp/usr/lib/$$opt; done
	dh_compress
	chmod 700 debian/openssl/etc/ssl/private
	dh_fixperms -X etc/ssl/private
	dh_strip --dbg-package=libssl0.9.7
	dh_perl -d
#	dh_suidregister
	dh_shlibdeps -l`pwd` -Xlibssl.so
	# Hack, to depend on version of libc6 which supports the i686 directory
	if [ "${DEB_HOST_ARCH}" == "i386" ]; \
	then \
	  SHLIB=`cat debian/libssl0.9.7.substvars`; \
	  LIBC_VERSION=`echo $$SHLIB | sed s'/.*libc6 (>= \(.*\))/\1/'`; \
	  if dpkg --compare-versions ${WANTED_LIBC_VERSION} gt $$LIBC_VERSION; \
	  then \
	    echo $$SHLIB | sed s'/libc6 (>= \(.*\))/libc6 (>= ${WANTED_LIBC_VERSION})/' > debian/libssl0.9.7.substvars; \
	  fi \
	fi
	dh_gencontrol
	dh_makeshlibs -m 0.9.7
	dh_installdeb
	dh_md5sums
	dh_builddeb
	echo -en "\a"

# Below here is fairly generic really

binary:		binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

.PHONY: binary binary-arch binary-indep clean
