#!/usr/bin/make -f
# makefile to build a shared library as a debian binary package
# modifyed to handle alternate libc

libname=liblockdev
package=${libname}${version_major}

_debian_policy_ = 1
-include debian/policy

ifndef PERL
PERL = /usr/bin/perl 
endif

archlib = `$(PERL) -MConfig -e 'print $$Config{installarchlib}'`
config  = INSTALLDIRS=perl
config += INSTALLMAN1DIR=${d_man}/man1
config += INSTALLMAN3DIR=${d_man}/man3
config += INSTALLPRIVLIB=${d_ulib}/perl5
config += INSTALLARCHLIB=${d_build}$(archlib)
PERLVERSION=`$(PERL) -e 'printf("%.3f", $$])'`

#

build:
	${checkdir}
	-${MAKE} mostyclean
	${MAKE} shared CFLAGS="-O2 -fPIC -fomit-frame-pointer"
	${MAKE} perl-lib
	touch build

.PHONY: clean 
clean:	
	${checkdir}
	-rm -f out build build-* _SRCDIR_
	-${MAKE} distclean
	${update_standards_version} debian/control
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp `find debian/* -type d ! -name CVS`
	-rm -f debian/*substvars debian/files* core


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

binary-run:	checkroot build
	${checkdir}
	-rm -rf ${d_build}
	#
	# need to create dirs by hand
	${install_dir}	${d_dirs} 
	#
	# ==== DEBIAN control files ====
	cd debian && ${install_DEBIAN_scripts}
	# shlibs dependency
	echo -e "${libname}\t${version_major}\t${package}" \
		> ${d_deb}/shlibs
	#
	# ==== make install ====
	${MAKE} install_run basedir=${d_usr}
	# move libs to /lib
	mv ${d_ulib}/* ${d_lib}
	strip --strip-unneeded 	${d_lib}/${libname}.${version}.so	
	# ==== symlink for shared libs to work; must be after shlib!  ====
	ln -s ${libname}.${version}.so	${d_lib}/${libname}.so.${version_major}
	#
	# ==== docs ====
	# particular documents that must be present with exactly this name
	cd debian && ${install_data}	changelog copyright  ${d_doc}
	#
	# now compress only large documents
	cd ${d_doc} && ${compress_docs}
	#
	# ==== clean dirs ====
	-${clean_dirs}
	# ==== md5sums ====
	cd ${d_build} && ${install_md5sums}
	# ==== finds dependencies ====
	dpkg-shlibdeps ${d_lib}/*
	# ==== process control file ====
	dpkg-gencontrol -p${package}
	chown -R root.root ${d_build}
	chmod -R go=rX ${d_build}
	dpkg --build ${d_build} ..

binary-dev:	checkroot
	# now add static libs, include file and manpage
	${checkdir}
	-rm -rf ${d_build}
	#
	# need to create dirs by hand
	${install_dir}	${d_dirs} 
	ln -s ${package} ${d_doc}-dev
	#
	# ==== DEBIAN control files ====
	prefix="dev" && cd debian && ${install_DEBIAN_scripts}
	#
	# ==== make install ====
	-${MAKE} mostyclean
	${MAKE} static CFLAGS="-g3 -O2"
	${MAKE} install_dev basedir=${d_usr}
	strip --strip-debug 	${d_ulib}/${libname}.a
	# ==== symlink for shared libs to work ====
	ln -s ${libname}.${version}.so	${d_lib}/${libname}.so
	#
	# ==== manpages ====
	${MAKE} install_doc basedir=${d_usr}
	gzip -r9 ${d_man}
	#
	# ==== clean dirs ====
	-${clean_dirs}
	# ==== md5sums ====
	cd ${d_build} && ${install_md5sums}
	# ==== process control file ====
	dpkg-gencontrol -p${package}-dev
	chown -R root.root ${d_build}
	chmod -R go=rX ${d_build}
	dpkg --build ${d_build} ..

binary-dbg:	checkroot
	# now add static libs with debug and profiling enabled
	${checkdir}
	-rm -rf ${d_build}
	#
	# need to create dirs by hand
	${install_dir}	${d_dirs}
	ln -s ${package} ${d_doc}-dbg
	#
	# ==== DEBIAN control files ====
	prefix="dbg" && cd debian && ${install_DEBIAN_scripts}
	#
	# ==== make install ====
	-${MAKE} mostyclean
	${MAKE} shared CFLAGS="-g3 -O2 -fPIC -DDEBUG" create_debug_lib=1
	${MAKE} install_debug basedir=${d_usr}
	-${MAKE} mostyclean
	${MAKE} shared CFLAGS="-g3 -pg -fPIC"
	${MAKE} install_profile basedir=${d_usr}
	#
	# ==== sources ====
	${install_sources}
	#
	# ==== docs ====
	cd debian && ${install_data}	README.debug	${d_doc}
	# now compress only large documents
	cd ${d_doc} && ${compress_docs}
	#
	# ==== clean dirs ====
	-${clean_dirs}
	# ==== md5sums ====
	cd ${d_build} && ${install_md5sums}
	# ==== process control file ====
	dpkg-gencontrol -p${package}-dbg
	chown -R root.root ${d_build}
	chmod -R go=rX ${d_build}
	dpkg --build ${d_build} ..

binary-perl:	checkroot build
	${checkdir}
	-rm -rf ${d_build}
	#
	# need to create dirs by hand
	${install_dir}	${d_dirs} ${d_doc}-perl ${d_man}/man3
	#
	# ==== docs ====
	# particular documents that must be present with exactly this name
	cd debian && ${install_data}	copyright changelog ${d_doc}-perl
	#
	# now compress only large documents
	cd ${d_doc}-perl && ${compress_docs}
	#
	# ==== make install ====
	cd LockDev && $(MAKE) pure_install $(config)
	-find ${d_build} -type f -name .packlist | xargs rm -f
	echo "perl:Depends=perl-$(PERLVERSION)" >>debian/substvars
	strip --strip-unneeded `find ${d_build} -name '*.so'`
	gzip -r9 ${d_man}
	#
	# ==== DEBIAN control files ====
	prefix="perl" && cd debian && ${install_DEBIAN_scripts}
	#
	# ==== clean dirs ====
	-${clean_dirs}
	# ==== md5sums ====
	cd ${d_build} && ${install_md5sums}
	# ==== process control file ====
	dpkg-gencontrol -p${package}-perl
	chown -R root.root ${d_build}
	chmod -R u=rwX,go=rX ${d_build}
	dpkg --build ${d_build} ..


binary-arch:	binary-run binary-dev binary-dbg binary-perl


.PHONY: binary binary-indep binary-arch binary-run binary-dev 
binary:		binary-indep binary-arch

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

