# bash completion for projectx(1)
#
# Options come from the argument parsing code of
# net.sourceforge.dvb.projectx.common.Start; -set keys from the user
# configuration file.
#
# Written by Claude Opus 5.5 <noreply@anthropic.com>, reviewed by Nicolas HOUDELOT.

_comp_projectx_keys()
{
    local conf=${XDG_CONFIG_HOME:-$HOME/.config}/project-x/projectx.conf
    [[ -r $conf ]] || conf=$HOME/.projectx/projectx.conf
    [[ -r $conf ]] && sed -n 's/^\([A-Za-z][^=#]*\)=.*/\1=/p' -- "$conf"
}

_projectx()
{
    local cur prev words cword
    if declare -F _comp_initialize >/dev/null; then
        _comp_initialize -n := -- "$@" || return
    else
        _init_completion -n := || return
    fi

    case $prev in
        -ini | -cut | -chp)
            _filedir
            return
            ;;
        -out)
            _filedir -d
            return
            ;;
        -set)
            COMPREPLY=($(compgen -W "$(_comp_projectx_keys)" -- "$cur"))
            local trim=${cur%"${cur##*[:=]}"}
            [[ -n $trim ]] && COMPREPLY=("${COMPREPLY[@]#"$trim"}")
            [[ ${#COMPREPLY[@]} -eq 1 && ${COMPREPLY[0]} == *= ]] &&
                compopt -o nospace
            return
            ;;
        -name | -id | -split)
            return
            ;;
    esac

    if [[ $cur == -* ]]; then
        # mapfile: "-?" must not be expanded as a glob pattern
        mapfile -t COMPREPLY < <(compgen -W '-\? -gui -ini -saveini -set
            -log -webif -demux -tovdr -tom2p -topva -tots -filter -out -name
            -id -cut -chp -split -dvx1 -dvx2 -dvx3 -dvx4' -- "$cur")
        return
    fi

    _filedir
} &&
    complete -F _projectx projectx

# ex: filetype=sh
