#
# Package: Reference Standard M
# File:    rsm/etc/rsm
# Summary: Bash completion script for rsm executable
#
# David Wicksell <dlw@linux.com>
# Copyright © 2023-2024 Fourth Watch Software LC
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License (AGPL) as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.
#
# SPDX-FileCopyrightText:  © 2023 David Wicksell <dlw@linux.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
#
#
# If you want to install this for a single user, copy this file to
# $HOME/.local/share/bash-completion/completions/.
#
# If you want to install this for every user, copy this file to
# /usr/local/share/bash-completion/completions/.

__rsm_option()
{
    local IFS=$'\n'
    local rsm_args=(-V -h -i -k -v -b -s -m -e -j -g -r -x -R)
    local rsm_words=("${COMP_WORDS[@]}") rsm_cword="${COMP_CWORD}"
    local rsm_current="${rsm_words[${rsm_cword}]}" rsm_word=""
    local rsm_cflag="" rsm_mflag="" rsm_bflag="" rsm_jflag=""

    case ${rsm_words[${rsm_cword}-1]} in
    *rsm)
        COMPREPLY=($(compgen -W "${rsm_args[*]}" -- "${rsm_current}"))
        COMPREPLY+=($(compgen -f -- "${rsm_current}" | sort || true))
        return
        ;;

    -V|-h)
        COMPREPLY=()
        return
        ;;

    -i|-k)
        COMPREPLY+=($(compgen -f -- "${rsm_current}" | sort || true))
        return
        ;;

    -v)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W 'volume-name(1-32_alpha)')")
        return
        ;;

    -b)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W 'block-size(1-256_KiB)')")
        return
        ;;

    -s)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W 'database-size(100-2147483647_blocks)')")
        return
        ;;

    -m)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W '[map-size(0-262147_KiB)]')")
        return
        ;;

    -e)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W '[environment-name(1-32_alpha)]')")
        return
        ;;

    -j)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W 'max-jobs(1-1024_jobs)')")
        return
        ;;

    -g)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W '[global-buffers(1-131072_MiB)]')")
        return
        ;;

    -r)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W '[routine-buffers(1-4095_MiB)]')")
        return
        ;;

    -x)
        [[ -z ${rsm_current} ]] && COMPREPLY=("$(compgen -W '[M-commands]')")
        return
        ;;

    -R)
        rsm_args=("${rsm_args[@]/-V}")
        rsm_args=("${rsm_args[@]/-h}")
        rsm_args=("${rsm_args[@]/-i}")
        rsm_args=("${rsm_args[@]/-k}")
        rsm_args=("${rsm_args[@]/-v}")
        rsm_args=("${rsm_args[@]/-b}")
        rsm_args=("${rsm_args[@]/-s}")
        rsm_args=("${rsm_args[@]/-m}")
        rsm_args=("${rsm_args[@]/-j}")
        rsm_args=("${rsm_args[@]/-g}")
        rsm_args=("${rsm_args[@]/-r}")
        rsm_args=("${rsm_args[@]/-R}")
        ;;

    *)
        rsm_args=("${rsm_args[@]/-V}")
        rsm_args=("${rsm_args[@]/-h}")
        rsm_args=("${rsm_args[@]/-i}")
        rsm_args=("${rsm_args[@]/-k}")

        case ${rsm_words[${rsm_cword}-2]} in
        -v|-b|-s|-m)
            rsm_args=("${rsm_args[@]/-j}")
            rsm_args=("${rsm_args[@]/-g}")
            rsm_args=("${rsm_args[@]/-r}")
            rsm_args=("${rsm_args[@]/-x}")
            rsm_args=("${rsm_args[@]/-R}")
            ;;

        -j|-g|-r)
            rsm_args=("${rsm_args[@]/-v}")
            rsm_args=("${rsm_args[@]/-b}")
            rsm_args=("${rsm_args[@]/-s}")
            rsm_args=("${rsm_args[@]/-m}")
            rsm_args=("${rsm_args[@]/-e}")
            rsm_args=("${rsm_args[@]/-x}")
            rsm_args=("${rsm_args[@]/-R}")
            ;;

        -e)
            rsm_args=("${rsm_args[@]/-j}")
            rsm_args=("${rsm_args[@]/-g}")
            rsm_args=("${rsm_args[@]/-r}")

            for rsm_word in "${rsm_words[@]}"
            do
                case ${rsm_word} in
                -v|-b|-s|-m)
                    rsm_args=("${rsm_args[@]/-x}")
                    rsm_args=("${rsm_args[@]/-R}")
                    ;;

                -x|-R)
                    rsm_args=("${rsm_args[@]/-v}")
                    rsm_args=("${rsm_args[@]/-b}")
                    rsm_args=("${rsm_args[@]/-s}")
                    rsm_args=("${rsm_args[@]/-m}")
                    ;;

                *)
                    # do nothing
                    ;;

                esac
            done
            ;;

        -x)
            rsm_args=("${rsm_args[@]/-v}")
            rsm_args=("${rsm_args[@]/-b}")
            rsm_args=("${rsm_args[@]/-s}")
            rsm_args=("${rsm_args[@]/-m}")
            rsm_args=("${rsm_args[@]/-j}")
            rsm_args=("${rsm_args[@]/-g}")
            rsm_args=("${rsm_args[@]/-r}")
            ;;

        *)
            COMPREPLY=()
            return
            ;;

        esac
        ;;

    esac

    for rsm_word in "${rsm_words[@]:1:${#rsm_words[@]}-2}"
    do
        rsm_args=("${rsm_args[@]/${rsm_word}}")
    done

    for rsm_word in "${rsm_words[@]}"
    do
        case ${rsm_word} in
        -v|-b|-s)
            rsm_cflag=$((rsm_cflag + 1))
            ;;

        -m)
            rsm_mflag=$((rsm_mflag + 1))
            ;;

        -j)
            rsm_jflag=$((rsm_jflag + 1))
            ;;

        -g|-r)
            rsm_bflag=$((rsm_bflag + 1))
            ;;

        *)
            # do nothing
            ;;

        esac
    done

    COMPREPLY=($(compgen -W "${rsm_args[*]}" -- "${rsm_current}"))

    if [[ -n "${rsm_args[*]}" ]]
    then
        if [[ ${rsm_mflag} == 1 ]]
        then
            [[ ${rsm_cflag} == 3 ]] && COMPREPLY+=($(compgen -f -- "${rsm_current}" | sort || true))
        elif [[ ${rsm_bflag} -gt 0 ]]
        then
            [[ ${rsm_jflag} == 1 ]] && COMPREPLY+=($(compgen -f -- "${rsm_current}" | sort || true))
        else
            [[ ${rsm_cflag:-3} == 3 ]] && COMPREPLY+=($(compgen -f -- "${rsm_current}" | sort || true))
        fi
    else
        COMPREPLY+=($(compgen -f -- "${rsm_current}" | sort || true))
    fi
}

complete -o filenames -o nosort -F __rsm_option rsm

# ex: filetype=bash
