summaryrefslogtreecommitdiff
path: root/install-completions
blob: a39c78780fd94f8b21cd462d2ab7bc1a38d2fe17 (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
#!/bin/sh
# install relevant completions, by sourcing every scriptlet
# found in a given directory, and symlinking those matching
# an installed command to another directory

have()
{
    PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null
}

usage()
{
    echo "usage: $0 <completion_dir> [installation_dir]"
}

while getopts ":h" flag; do
    case $flag in
	h) usage; exit 0;;
    esac
done

shift $((OPTIND - 1))

completion_dir=$1
if [ -z "$completion_dir" ]; then
    echo "missing completion directory"
    usage
    exit 1
fi
if [ ! -d $completion_dir ]; then
    echo "invalid directory $completion_dir"
    usage
    exit 1
fi

installation_dir=$2
if [ ! -d $installation_dir ]; then
    echo "invalid directory $installation_dir"
    usage
    exit 1
fi

# many scripts require this
shopt -s extglob

# and some require this also
UNAME=$(uname -s)
UNAME=${UNAME/CYGWIN_*/Cygwin}

case $UNAME in
    Linux|GNU|GNU/*) USERLAND=GNU ;;
    *) USERLAND=$UNAME ;;
esac

for script in $completion_dir/*; do
    # reset completion
    complete -r

    # source script
    source $script 2>/dev/null

    # check completion output
    output=$(complete -p)

    if [ -z "$output" ]; then
	continue
    fi

    if [ -n "$installation_dir" ]; then
	ln -sf $script $installation_dir/$(basename $script)
    else
	echo "$script OK"
    fi

done