#!/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}g

_debian_policy_ = 1
-include debian/policy

#

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}.so.${version}	
	# ==== symlink for shared libs to work ====
	ln -s ${libname}.so.${version}	${d_lib}/${libname}.so.${version_major}
	#
	# ==== docs ====
	# particular documents that must be present with exactly this name
	cd debian && ${install_data}	changelog \
				${d_doc}/changelog.Debian
	cd debian && ${install_data}	copyright \
				${d_doc}/copyright
	#
	# 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
	#
	# ==== 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}.so.${version}	${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} ${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}-dbg
	# now compress only large documents
	cd ${d_doc}-dbg && ${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}	changelog \
			${d_doc}-perl/changelog.Debian
	cd debian && ${install_data}	copyright \
			${d_doc}-perl/copyright
	#
	# now compress only large documents
	cd ${d_doc}-perl && ${compress_docs}
	#
	# ==== make install ====
	cd LockDev && $(MAKE) pure_perl_install manifypods \
		INST_MAN3DIR=${d_man}/man3 \
		PREFIX=${d_usr} 
	-find ${d_build} -type f -name .packlist | xargs rm -f
	gzip -r9 ${d_man}
	#
	# ==== clean dirs ====
	-${clean_dirs}
	# ==== md5sums ====
	cd ${d_build} && ${install_md5sums}
	# ==== process control file ====
	dpkg-gencontrol -p${libname}${version_major}-perl
	chown -R root.root ${d_build}
	chmod -R 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`"

