# apt-repo(8) completion                                   -*- shell-script -*-
# vi: ft=sh sw=4
# Copyright (c) 2025 Vitaly Chikunov <vt@altlinux.org>
# SPDX-License-Identifier: GPL-2.0-or-later
# shellcheck disable=SC2207

_apt_repo_branches() {
    apt-repo add branch | LC_ALL=C grep -e "${1:-^[sp]}"
}

_apt_repo_rm_branches() {
    apt-repo |
	sed -ne "$(apt-repo rm branch |
		   sed -e '/^[Ssa]/s!.*!s/.*&.*/&/ip!' \
		       -e '/^[^Ssa]/s!.*!s/.*&\\/branch.*/&/p!')" | LC_ALL=C sort -uf
}

_apt_repo_filter() {
    local c=$1 filter; shift
    filter=$(printf '%s\n' "$@" | sed -e "${c}d")
    if [[ -z "$filter" ]]; then
	cat
    else
	grep -Fxv -e "$filter"
    fi
}

_apt_repo()
{
    # shellcheck disable=SC2034
    local cur prev words cword split
    _init_completion -s -- "$@" || return

    local offset=0 i has_cmd=''
    for ((i = 1; i < cword; i++)); do
	if [[ ${words[i]} != -* ]]; then
	    has_cmd=yes
	    offset=$i
	    break
	fi
    done

    if [ -z "$has_cmd" ]; then
	if [[ $prev == --hsh-apt-config ]]; then
	    COMPREPLY=( $(compgen -f -- "$cur") )
	    compopt -o filenames
	elif [[ $cur == -* ]]; then
	    COMPREPLY=( $(compgen -W "-a -h --help --hsh-apt-config= --dry-run -v --version" -- "$cur") )
	    [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
	elif [[ $prev != @(-a|-h|--help|-v|--version) ]]; then
	    COMPREPLY=( $(compgen -W "add clean list rm set test update upgrade" -- "$cur") )
	fi
	return
    fi

    local xword=$((cword - offset))
    (( xword > 0 )) || return
    case "${words[offset]}" in
        list)
	    (( xword == 1 )) &&
		COMPREPLY=( $(compgen -W "-a task" -- "$cur") )
            ;;
        test)
	    # apt-repo test [task] 123456 pkg1 pkg2...
            if (( xword == 1 )); then
                COMPREPLY=( $(compgen -W "task" -- "$cur") )
		return
	    fi
	    local tword=$((offset+1))
	    [[ ${words[tword]} == 'task' ]] && (( tword++ ))
	    local taskid=${words[tword]}
	    (( cword > tword )) && [[ $taskid == +([[:digit:]]) ]] || return
	    if [[ $cur == +* ]]; then
                COMPREPLY=( $(compgen -W "+debuginfo +checkinstall" -- "$cur") )
		return
	    fi
	    COMPREPLY=( $(compgen -W "$(apt-repo list task "$taskid" 2>/dev/null | sed 's/=.*//' |
		_apt_repo_filter "$((cword-tword))" "${words[@]:tword+1}")" -- "$cur") )
            ;;
        set)
	    (( xword == 1 )) &&
		COMPREPLY=( $(compgen -W "$(_apt_repo_branches "$cur")" -- "$cur") )
            ;;
        add)
	    # apt-repo add [branch] <branchname> YYYYMMDD
	    if (( xword == 1 )); then
		if [[ $cur == /* ]]; then
		    COMPREPLY=( $(compgen -d -- "$cur") )
		    compopt -o filenames
		else
		    COMPREPLY=( $(compgen -W "branch task $(_apt_repo_branches "$cur")" -- "$cur") )
		fi
		return
	    fi
	    local bword=$((offset+1))
	    [[ ${words[2]} == 'branch' ]] && (( bword++ ))
	    if (( cword == bword )); then
		COMPREPLY=( $(compgen -W "$(apt-repo add branch)" -- "$cur") )
	    elif (( cword == bword+1 )) && [[ $prev == [psS]* ]]; then
		COMPREPLY=( $(compgen -W "$(LC_ALL=C TZ=UTC date +%Y%m%d -d-1day)" -- "$cur") )
	    fi
	    ;;
        rm)
            if (( xword == 1 )); then
                COMPREPLY=( $(compgen -W "all branch task $(_apt_repo_rm_branches)" -- "$cur") )
	    elif (( xword == 2 )); then
		if [[ $prev == all ]]; then
		    COMPREPLY=( $(compgen -W "cdrom branch task" -- "$cur") )
		elif [[ $prev == branch ]]; then
		    COMPREPLY=( $(compgen -W "$(apt-repo rm branch)" -- "$cur") )
		elif [[ $prev == task ]]; then
		    COMPREPLY=( $(compgen -W "$(apt-repo |
			grep -Po -e 'repo/\K\d+(?=/.* task)|tasks/archive/done/_\d+/\K\d+(?=.build/repo/.* task)' )" -- "$cur") )
		fi
            fi
            ;;
        upgrade)
            if (( xword == 1 )); then
                COMPREPLY=( $(compgen -W "$(_apt_repo_branches "$cur")" -- "$cur") )
            fi
            ;;
    esac
} &&
    complete -F _apt_repo apt-repo
