#!/bin/sh

# Update package Control files.
#
# Copyright (C) 2018, 2023 Paul Boddie <paul@boddie.org.uk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301, USA

PROGNAME=$(basename "$0")

if [ "$1" = '--help' ] || [ ! "$1" ] || [ ! -e "$1" ] ; then
    cat 1>&2 <<EOF
Usage: $PROGNAME <package location>

Search for package declarations in the given package, overwriting the Control
file for the package with an updated version.
EOF
    exit 1
fi

PACKAGE="$1"

# Obtain the existing Control file and extract the non-provides fields.

grep -v '^provides: ' "$PACKAGE/Control" > _non_provides.tmp

# Search for package declarations and build provides fields.

grep PC_FILENAME -r "$PACKAGE" | cut -d'=' -f 2 | sort -u | sed 's/^ /provides: /' > _provides.tmp

# Concatenate the files and overwrite the Control file.

cat _provides.tmp _non_provides.tmp > "$PACKAGE/Control"

# Remove the temporary files.

rm _provides.tmp _non_provides.tmp

# vim: tabstop=4 expandtab shiftwidth=4
