# vim: filetype=sh
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2018 Mateusz Piotrowski <0mp@FreeBSD.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright notice,
#        this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# ---
# Version: 0.2.1

_service_complete_service() {
    COMPREPLY+=($(compgen -W "$(service -l)" -- "$cur"))
}

_service_complete_command() {
    # Do not try to complete commands if the service is invalid.
    service -l | grep --quiet --only-matching "$prev" || return 1

    # Not every command is listed here for convenience.
    local commands=(start stop restart status $(service "$prev" oneextracommands 2>/dev/null))

    # Prepend 'one' to the available commands if the service is not enabled.
    if ! service "$prev" enabled >/dev/null 2>&1; then
        commands=($(compgen -P 'one' -W "${commands[*]}"))
    fi

    COMPREPLY+=($(compgen -W "${commands[*]}" -- "$cur"))
}

_service() {
    local cur prev words cword
    _init_completion || return

    local flags=(-e -j -l -r -R -v)

    case $prev in
        -j)
            # XXX: I am not sure if it gets the information we want.
            COMPREPLY+=($(compgen -W "$(jls | awk 'NR>1{print $1, $3}')" -- "$cur"))
            ;;
        -*)
            if [[ $cur == -* ]]; then
                COMPREPLY+=($(compgen -W "${flags[*]}" -- "$cur"))
            else
                _service_complete_service
            fi
            ;;
        *)
            if [[ $cur == -* ]]; then
                COMPREPLY+=($(compgen -W "${flags[*]}" -- "$cur"))
            fi

            case $cword in
                1)
                    _service_complete_service
                    ;;
                2)
                    _service_complete_command
                    ;;
                *)
                    # Check if the word before the previous one is "-j".
                    # For example ("_" is a cursor):
                    #
                    #     # service -v -j 1 postg_
                    #
                    if [[ ${words[((cwords - 3))]} = '-j' ]]; then
                        _service_complete_service
                    else
                        _service_complete_command
                    fi
                    ;;
            esac
            ;;
    esac
} &&
complete -F _service service
