aboutsummaryrefslogtreecommitdiff
path: root/contrib/cpt-owns
blob: 9bc7d7798fb12094440c0d02b7cf3b2fd038b127 (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
#!/bin/sh -e
# Check which package owns a file

## SYNOPSIS:
## .Nm
## .Op Ar file
## DESCRIPTION:
## .Nm
## searches package manifests to determine which package owns
## .Ar file .
## If the
## .Ar file
## is not a realpath,
## .Nm
## will first check if the file exists as a relative path, and then it will
## check if it exists as an executable
## in
## .Ev PATH .
##
## .Nm
## does not check directories, as they can be used by multiple packages at the
## same time.

# Source the package manager library.
# shellcheck disable=1091
. cpt-lib

case "$1" in ''|--help|-h) out "usage: ${0##*/} [file]"; exit 0; esac

# If full path is not specified, use either the current directory, or look for a
# command.
case "$1" in
    /*) ;;
     *)
        if [ -f "$1" ]; then
            set -- "$PWD/$1"
        else
            set -- "$(command -v "$1")"
        fi
esac

# Strip 'CPT_ROOT' from the file path if passed and follow symlinks.
file="${1#$CPT_ROOT}"
dirname=$(_readlinkf "$CPT_ROOT/${file%/*}")
file="$dirname/${file##*/}"

# Check if the file exists and exit if it is not.
[ -f "$file" ] || {
    [ -d "$file" ] && die "please specify a file instead of a directory"
    die "file '$1' doesn't exist."
}

pkg_owner -lFx "$file" || die "Cannot determine which package owns '$file'"