aboutsummaryrefslogtreecommitdiff
path: root/contrib/cpt-revdepends
blob: 833dea26c9043ce95130cf4ad641b155845a280e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh -e
# Display packages which depend on the given package

## SYNOPSIS:
## .Nm
## .Op Fl tm
## .Op Ar package
## DESCRIPTION:
## .Nm
## generates reverse dependencies for
## .Ar package .
## If no package name is given,
## .Nm
## uses the name of the current directory for the package.
##
## Following options are available for use::
## .Bl -tag
## .It Fl t , -tree
## Also print indirect reverse dependencies
## .It Fl m , -make
## Include make dependencies
## .El

parser_definition() {
    setup REST help:usage -- "usage: ${0##*/} [options...] [pkg...]"
    flag tree -t --tree   -- "Also print indirect reverse dependencies"
    flag make -m --make   -- "Include make dependencies"
    global_options
}

# shellcheck disable=1091
. cpt-lib; set +f

[ "$1" ] || set -- "${PWD##*/}"

# 'cd' to the database directory as a simple way of
# stripping the path and performing a 'basename'.
#
# $sys_db is defined on cpt-lib
# shellcheck disable=2154
cd "$sys_db"

get_revdep() {
    query="^$1\$"
    # Defined by parser.
    # shellcheck disable=2154
    [ "$make" ] && query="$query\\|^$1 *make\$"
    grep "$query" -- */depends | while read -r pkg _; do
        printf '%s\n' "${pkg%%/*}"
    done
}

# Defined by parser.
# shellcheck disable=2154
if [ "$tree" ]; then
    create_cache nobuild
    :> "$tmp_dir/processed"
    get_revdep "$1" > "$tmp_dir/dep"
    while ! diff -q "$tmp_dir/dep" "$tmp_dir/processed" >/dev/null 2>&1; do
        cp "$tmp_dir/dep" "$tmp_dir/dep.new"
        while read -r line; do
            grep -q "^$line\$" "$tmp_dir/processed" && continue
            get_revdep "$line" >> "$tmp_dir/dep.new"
            printf '%s\n' "$line" >> "$tmp_dir/processed"
        done < "$tmp_dir/dep"
        sort -u -o "$tmp_dir/dep" "$tmp_dir/dep.new"
        sort -u -o "$tmp_dir/processed" "$tmp_dir/processed"
    done
    cat "$tmp_dir/dep"
else
    get_revdep "$1"
fi