# bash completion for pixilang(1)
#
# Options come from sundog_main() (lib_sundog/main/main.cpp) and app_init()
# (pixilang3/main/app.cpp). Values for -cfg are the option names of
# pixilang_config.ini. Everything after the program file belongs to the
# Pixilang program and is completed as file names.
#
# Written by Claude Opus 5.5 <noreply@anthropic.com>, reviewed by Nicolas HOUDELOT.

_comp_pixilang_cfg_options='audiodriver= audiodevice= audiodevice_in= frequency=
buffer= jack_nodefin= jack_nodefout= jack_drmin= windowname= width= height=
fullscreen= change_video_mode= maximized= rotate= maxfps= pencontrol=
touchcontrol= ppi= scale= fscale= zoom= show_virt_kbd= hide_virt_kbd=
softrender= nowin= nocursor= nosws= nofrecent= swap_behavior=
no_font_upscale= int_font_scaling= denorm= no_clog= no_flog=
pixi_containers_num= pixi_idir='

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

    # Once the program file is given, the next words are its own arguments.
    local i
    for ((i = 1; i < cword; i++)); do
        case ${words[i]} in
            -cfg | -i | -id)
                ((i++))
                ;;
            -*) ;;
            *)
                _filedir
                return
                ;;
        esac
    done

    case $prev in
        -i)
            _filedir -d
            return
            ;;
        -id)
            return
            ;;
        -cfg)
            # "OPT1=VAL|OPT2=" : complete the option name after the last '|'.
            local head=""
            [[ $cur == *\|* ]] && head=${cur%|*}\|
            local last=${cur##*|}
            if [[ $last != *=* ]]; then
                COMPREPLY=($(compgen -P "$head" \
                    -W "$_comp_pixilang_cfg_options" -- "$last"))
                # readline only replaces the text after the last ':', '='
                # or '|' (COMP_WORDBREAKS): drop that prefix.
                local trim=${cur%"${cur##*[:=|]}"}
                [[ -n $trim ]] && COMPREPLY=("${COMPREPLY[@]#"$trim"}")
                [[ ${#COMPREPLY[@]} -gt 0 ]] && compopt -o nospace
            fi
            return
            ;;
    esac

    if [[ $cur == -* ]]; then
        COMPREPLY=($(compgen -W '-? --help -ncl -nfl -cfg -c -i -id' -- "$cur"))
        return
    fi

    if ((cword == 1)); then
        COMPREPLY=($(compgen -W 'clearall' -- "$cur"))
    fi
    _filedir '@(pixi|txt|pixicode)'
    [[ ${#COMPREPLY[@]} -eq 0 ]] && _filedir
}
complete -F _pixilang pixilang
