#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# URPMI Setup
# 
# Copyright (C) 2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
# 
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; If not, see
# <https://www.gnu.org/licenses/>.
# 
#-----------------------------------------------------------------------------------------------------------------------------------
# -*- mode: shell; sh-basic-offset: 4; indent-tabs-mode: nil; -*-



_urpmi_setup() {
    local cur prev opts distrib_opts mirror_opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Supported distributions (customize as needed)
    distrib_opts="9 10 cauldron"

    # Example mirror lists (customize as needed)
    mirror_opts_mga="https://mirror.rx3.net/mageia/distrib/${DISTRIB}/x86_64 ftp://ftp.proxad.net/mirrors/mageia.org/distrib/${DISTRIB}/x86_64"
    mirror_opts_rx3="http://mirror.xor.rx3/rx3/distrib/${DISTRIB}/x86_64 https://mirror.rx3.net/rx3/distrib/${DISTRIB}/x86_64"

    # Check if --help or --version is already used
    local has_help_or_version=false
    local has_dmr=false  # -d, -m, or -r

    for arg in "${COMP_WORDS[@]}"; do
        case "$arg" in
            --help|-h|--version|-V)
                has_help_or_version=true
            ;;
            
            --distrib|-d|--mirror-mga|-m|--mirror-rx3|-r)
                has_dmr=true
            ;;
        esac
    done

    # If --help or --version is used, only suggest -v, -T, and -u
    if $has_help_or_version; then
        opts="--test -T --update -u --verbose -v"
    # If -d, -m, or -r is used, exclude --help and --version
    elif $has_dmr; then
        opts="--test -T --update -u --verbose -v --distrib -d --mirror-mga -m --mirror-rx3 -r"
    # Otherwise, show all options
    else
        opts="--help -h --version -V --distrib -d --mirror-mga -m --mirror-rx3 -r --test -T --update -u --verbose -v"
    fi

    case "${prev}" in
        --distrib|-d)
            COMPREPLY=( $(compgen -W "${distrib_opts}" -- "${cur}") )
            return 0
        ;;
        
        --mirror-mga|-m)
            COMPREPLY=( $(compgen -W "${mirror_opts_mga}" -- "${cur}") )
            return 0
        ;;

        --mirror-rx3|-r)
            COMPREPLY=( $(compgen -W "${mirror_opts_rx3}" -- "${cur}") )
            return 0
        ;;

        --mirror-mga|-m|--mirror-rx3|-r)
            COMPREPLY=( $(compgen -W "${mirror_opts}" -- "${cur}") )
            return 0
        ;;

        *)
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            return 0
        ;;
    esac
}



complete -F _urpmi_setup urpmi-setup
