#!/bin/bash
# This helper scripts copies datafiles from installed packages
# into the root of a source package. It uses dh_install configuration
# files (install | <pkg>.install) to determine which files to include
# in the source package.
# If the required binary packages are not installed on the system an attempt
# is made to call an executable named 'update-data' and located in the root
# of the source package. This executable is thought to populate the source
# package directory with datafiles from upstream sources.
#
# Copyright 2007 by Michael Hanke <michael.hanke@gmail.com> licensed under the
# terms of the GNU General Public License (v2 or later)

shopt -s extglob

# determine whether up-to-date binary package(s) is(are) available
# or whether the upstream sources have to be used
dpkg-checkbuilddeps
if [ $? == 0 ]; then
	echo "Using local binary package data to populate the source package"
	# use information from the debhelper file to determine where the package
	# content is located
	lcl_data=$(cat debian/*install | awk -v OFS='/' '{ print $2,$1 }')

	# copy every item into the source package root
	for src in $lcl_data; do
		# use eval to enable brace expansion
		eval cp --target-directory=. -vrf /$src
	done
else
	# in case any build-deps are missing, assume that the binary data
	# packages are missing and call the 'update-data' script to fetch
	# them from upstream sources
	echo "No local package data available. Invoking 'update-data'"
	./update-data $@
fi
