aboutsummaryrefslogtreecommitdiff
path: root/tools/ejabberdctl.bc
blob: 3a63e29fde328b7c07bab6ed19699f3bbb790ead (plain) (blame)
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
99
#
# bash completion for ejabberdctl
#
get_help()
{
    local COMMANDCACHE=/var/log/ejabberd/bash_completion_$RANDOM
    ejabberdctl $CTLARGS help >$COMMANDCACHE
    if [[ $? == 2 ]] ; then
        ISRUNNING=1
        runningcommands=`cat $COMMANDCACHE | grep "^   [a-z]" | awk '{print $1}' | xargs`
    fi
    rm $COMMANDCACHE
}

_ejabberdctl()
{
    local cur prev
    local ISRUNNING=0
    local runningcommands

    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    local startcoms="start debug live"
    local startpars="--config-dir --config --ctl-config --logs --spool"

    local i=1
    local CTLARGS=""
    while [ $i -lt $COMP_CWORD ] ; do
        local PARAM="${COMP_WORDS[i]}"
        i=$((i+1))
        case $PARAM in
            --*)
                CTLARGS="--node ${COMP_WORDS[i]}"
                i=$((i+1)) ;;
            *) break ;;
        esac
    done

    case "${prev##*/}" in
        ejabberdctl)
            # This clause matches even when calling `/sbin/ejabberdctl` thanks to the ##*/ in the case
            get_help
            COMPREPLY=($(compgen -W "--node ${startpars} ${startcoms} ${runningcommands}" -- $cur))
            return 0
            ;;
        start|live)
            COMPREPLY=($(compgen -W "--node ${startpars}" -- $cur))
            return 0
            ;;
        debug)
            COMPREPLY=($(compgen -W "--node" -- $cur))
            return 0
            ;;
        help)
            get_help
            COMPREPLY=($(compgen -W "${runningcommands}" -- $cur))
            return 0
            ;;
        --node)
            RUNNINGNODES=`epmd -names | grep name | awk '{print $2"@localhost"}' | xargs`
            COMPREPLY=($(compgen -W "$RUNNINGNODES" -- $cur))
            return 0
            ;;
        --config|--ctl-config)
            _filedir '?(u)cfg'
            return 0
            ;;
        --config-dir|--logs|--spool)
            _filedir
            return 0
            ;;
        *)
            prev2="${COMP_WORDS[COMP_CWORD-2]}"
            get_help
            if [[ "$prev2" == --* ]]; then
                COMPREPLY=($(compgen -W "--node ${startpars} ${startcoms} ${runningcommands}" -- $cur))
            else
                if [[ $ISRUNNING == 1 ]]; then
                    echo ""
                    ejabberdctl $CTLARGS help ${PARAM}
                    echo -n "${COMP_LINE}"
                fi
            fi
            return 0
            ;;
    esac
}

complete -F _ejabberdctl ejabberdctl

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh