#!/bin/bash
#
# 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; version 2 of the License.
#
# 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.

version=0.15.15dev

[[ -r /usr/lib/manjaro-tools/util-msg.sh ]] && source /usr/lib/manjaro-tools/util-msg.sh

match=$1

if [[ -z $match ]]; then
    echo 'Usage: finddeps <depname>'
    echo ''
    echo 'Find packages that depend on a given depname.'
    echo 'Run this script from the top-level directory of your ABS tree.'
    echo ''
    exit 1
fi

find $PWD -type d | while read d; do
    if [[ -f "$d/PKGBUILD" ]]; then
        unset pkgname depends makedepends optdepends
        . "$d/PKGBUILD"
        for dep in "${depends[@]}"; do
            # lose the version comparator, if any
            depname=${dep%%[<>=]*}
            [[ $depname = $match ]] && msg "$d (depends)"
        done
        for dep in "${makedepends[@]}"; do
            # lose the version comparator, if any
            depname=${dep%%[<>=]*}
            [[ $depname = $match ]] && msg "$d (makedepends)"
        done
        for dep in "${optdepends[@]/:*}"; do
            # lose the version comaparator, if any
            depname=${dep%%[<>=]*}
            [[ $depname = $match ]] && msg "$d (optdepends)"
        done
    fi
done
