###
###	This file is a makefile fragment to be included on top of 
###	the debian/rules makefile __and__ on top of the package
###	makefile. It does nothing, as all is conditioned on a couple of
###	variables: _debian_policy_ for the debian's policy variables
###	definitions, and create_debug_lib for the overwriting rule.
###

this_dir=${shell pwd}
abs_src = /usr/src/$(shell basename ${this_dir})

######################## debian policy implementation ########################

ifdef _debian_policy_

# this is the version of the policy to be included in 
# debian/control file through $${update_standards_version} 
std-version     = 3.0.1

version=${shell expr `pwd` : '.*-\([0-9.]*\)'}
version_major=${shell expr `pwd` : '.*-\([0-9]*\).[0-9.]*'}
arch = ${shell dpkg --print-gnu-build-architecture}


# the directories that needs to be created;
# better list them here than in another file.
#
# ATTENTION: some makefile adds ../ in front of $$prefix
# others change curdir as it were shoes ...
# use relative d_build in the first case,
relative_d_build = debian/tmp
# absolute d_build in the second case.
absolute_d_build = ${this_dir}/debian/tmp
#
d_build = ${absolute_d_build}
#
d_deb   = ${d_build}/DEBIAN
d_etc   = ${d_build}/etc
d_conf  = ${d_build}/etc/man
d_lib   = ${d_build}/lib
d_usr   = ${d_build}/usr
d_ulib  = ${d_build}/usr/lib
d_bin   = ${d_build}/usr/bin
d_sbin  = ${d_build}/usr/sbin
d_incl  = ${d_build}/usr/include
d_man   = ${d_build}/usr/share/man
d_mlib  = ${d_build}/usr/lib/${package}
d_menu  = ${d_build}/usr/lib/menu
d_locl  = ${d_build}/usr/share/locale
d_xlib  = ${d_build}/usr/X11R6/lib
d_xbin  = ${d_build}/usr/X11R6/bin
d_xman  = ${d_build}/usr/X11R6/man
d_doc   = ${d_build}/usr/share/doc/${package}
d_doc-base  = ${d_build}/usr/share/doc-base

d_dirs  = ${d_deb} ${d_etc} ${d_lib} ${d_usr} ${d_ulib} ${d_bin} ${d_sbin} ${d_incl} ${d_man} ${d_mlib} ${d_menu} ${d_locl} ${d_xlib} ${d_xbin} ${d_xman} ${d_doc} ${d_conf} ${d_doc-base} 

# simple commands

define checkdir
@echo -e "\n\t########## ${package} rules $@ ##########\n"
test -f debian/rules
endef

define install_dir
install -p -o root -g root -m 755 -d
endef

define install_bin
install -p -o root -g root -m 755
endef

define install_data
install -p -o root -g root -m 644
endef

define compress_docs
find -type f \(	  -name "changelog.Debian" -or -size +4k \) \
		! -name "copyright"	\
		! -name "*.html"	! -name "*.htm"	\
		! -name "*.gif"		! -name "*.css"	\
		! -name "*.gz"		! -iname "*.z"	\
| xargs --no-run-if-empty  gzip -v9
endef

define clean_dirs
cd ${d_build}	\
&& touch ${d_deb}/md5sums \
&& until ( find -type d -empty \
	-exec rmdir {} \; ) ;	\
do :; done 2>&1 \
| sed 's/No such file or directory/	removed/'
endef

define install_md5sums
find . -path './DEBIAN' -prune -o -type f -printf "%P "       \
| xargs --no-run-if-empty  md5sum  >DEBIAN/md5sums
endef

### can set a prefix to install for different binary packages.
### eg.:	prefix="dbg_" && cd debian && ${install_DEBIAN_scripts}
### will install debian/dbg_postinst as tmp/DEBIAN/postinst  ...
define install_DEBIAN_scripts
for script in preinst postinst prerm postrm; \
do	if test -e "$${prefix}$${script}"; \
	then	${install_bin} $${prefix}$${script} ${d_deb}/$${script} ; \
		ls -la ${d_deb}/$${script} ; \
		fi; \
done
endef

# ==== install cron files in /etc ====
define install_cron_files
for cron in `ls cron.*`; \
do	if test -e "$${cron}"; \
	then	${install_dir} ${d_etc}/$${cron}; \
		${install_bin} $${cron} ${d_etc}/$${cron}/${package}; \
		ls -la ${d_etc}/$${cron}/${package}; \
		fi; \
done
endef

define update_standards_version
test -n "${std-version}" \
&& perl -pi -e 's/^(Standards-Version:).*/$$1 ${std-version}/'
endef

define install_sources
for src in `strings -a ${d_ulib}/debug/* | grep '$(abs_src)' | sort -u` ; \
do	for fullname in $${src} ; \
	do	fulldir=`dirname $${fullname}` ; \
		${install_dir}	${d_build}$${fulldir} ; \
		localname=`echo $${fullname} | sed 's,^${abs_src}/,,'` ; \
		ls $${localname} 2>/dev/null \
		&& ${install_data} $${localname} ${d_build}$${fullname}  ; \
done; done;
endef

ifdef list_build_enabled
define list_build_if_enabled
cd ${d_build} && find -ls
endef
else
define list_build_if_enabled
true
endef
endif # list_build_enabled

endif # _debian_policy_

###############################################################################

### this fragment of makefile _must_ be included in the makefile
### and this variable ( create_debug_lib ) must be passed to the 
### sub-make to activate this rule. Do this in the debug building 

ifdef create_debug_lib

_SRCDIR_:
	ln -sf . $@

### How does this work?
### a little explanation:
### the build stage (and all the binary) depends from a file not phony
### named _SRCDIR_ which is created in the current working dir as a 
### symlink to . (dot, the current dir), and removed in the clean stage.
### We overwrite the implicit rule to make objects from .c sources,
### producing the asm source file from the .c source seen through the
### _SRCDIR_ symlink. In the rule we change that strange name with the
### absolute pathname where we will store the sources, so that the
### object (and the libraries) will record that name instead that the 
### actual one.
### The purpose is to have a default link from the libraries to the
### place where the debugging binary package will put the sources, so that
### gdb will diplay source lines without _any_ need of strange declarations.

define compile.c
	${COMPILE.c} -S _SRCDIR_/$< -o $*.ss
endef
define compile.cc
	${COMPILE.cc} -S _SRCDIR_/$< -o $*.ss
endef
define the_rest
	sed 's,^.stabs \"_SRCDIR_\/,.stabs \"${abs_src}/,' $*.ss > $*.s
	${COMPILE.s} -o $@ $*.s
	${RM} $*.ss $*.s
endef

# overwriting implicit rule 
%.o: %.c _SRCDIR_
	@echo "=== overwritten rule .o:.c ($@: $^) ==="
	${compile.c}
	${the_rest}

# overwriting implicit rule 
%.o: %.cc _SRCDIR_
	@echo "=== overwritten rule .o:.cc ($@: $^) ==="
	${compile.cc}
	${the_rest}

endif # create_debug_lib

###############################################################################
