summaryrefslogtreecommitdiffstats
path: root/sogrep.in
blob: 3be2672d22d2d0f1b57380f3bf4008dc287836dc (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
# License: Unspecified

: ${SOLINKS_MIRROR:="https://mirror.pkgbuild.com"}
: ${SOCACHE_DIR:="${XDG_CACHE_HOME:-${HOME}/.cache}/sogrep"}
repos=('staging' 'testing' 'core' 'extra'
       'community-staging' 'community-testing' 'community'
       'multilib-staging' 'multilib-testing' 'multilib'
       'gnome-unstable' 'kde-unstable')
arches=('x86_64')

source /usr/share/makepkg/util/util.sh

recache() {
    local repo arch

    for repo in "${repos[@]}"; do
        for arch in "${arches[@]}"; do
            rm -rf "${SOCACHE_DIR}/${arch}/${repo}"
            mkdir -p "${SOCACHE_DIR}/${arch}/${repo}"
            curl "${SOLINKS_MIRROR}/${repo}/os/${arch}/${repo}.links.tar.gz" | bsdtar -xf - -C "${SOCACHE_DIR}/${arch}/${repo}"
        done
    done
}

search() {
    local repo=$1 arch lib=$2

    if [[ $repo != all ]]; then
        if ! in_array "$repo" "${repos[@]}"; then
            echo "${BASH_SOURCE[0]##*/}: unrecognized repo '$repo'"
            echo "Try '${BASH_SOURCE[0]##*/} --help' for more information."
            exit 1
        fi
        local repos=("${repo}")
    fi

    if [[ ! -d ${SOCACHE_DIR} ]]; then
        recache
    fi

    for arch in "${arches[@]}"; do
        for repo in "${repos[@]}"; do
            db=${SOCACHE_DIR}/${arch}/${repo}/
            if [[ -d ${db} ]]; then
                while read -rd '' pkg; do
                    pkg=${pkg#${db}}
                    printf '%s/%s\n' "${repo}" "${pkg%-*-*/links}"
                done < <(grep -rlZ "${lib}" "${db}")
            fi
        done
     done | sort -u
}

usage() {
    cat <<- _EOF_
		Usage: ${BASH_SOURCE[0]##*/} [OPTIONS] REPO LIBNAME

		Check the soname links database for pacman repositories containing
		packages linked to a given shared library. If the repository specified
		is "all", then all repositories will be searched, otherwise only the
		named repository will be searched.

		If the links database does not exist, it will be downloaded first.

		OPTIONS
		    -r, --refresh   Refresh the links databases
		    -h, --help      Show this help text
_EOF_
}

if (( $# == 0 )); then
    echo "error: No arguments passed."
    echo "Try '${BASH_SOURCE[0]##*/} --help' for more information."
    exit 1
fi

while (( $# )); do
    case $1 in
        -r|--refresh)
            recache
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            if (( $# < 2 )); then
                echo "error: Not enough arguments passed."
                echo "Try '${BASH_SOURCE[0]##*/} --help' for more information."
                exit 1
            fi
            search "$@"
            exit $?
            ;;
    esac
    shift
done