#!/bin/sh
#
# Copyright (C) 2012, 2016, 2019, 2020, 2021, 2025  Etersoft
# Copyright (C) 2012, 2016, 2019, 2020, 2021, 2025  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 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 <http://www.gnu.org/licenses/>.
#

load_helper epm-sh-altlinux
load_helper epm-sh-repo

epm_repolist_help()
{
    message 'epm repo list - list package repositories
Usage: epm repo list [options] [pattern]'
    echo ''
    echog 'Options:'
    get_help HELPOPT $SHAREDIR/epm-repolist
}

# Output apt sources lines from one file
# Usage: __cat_apt_sources_list grep_pattern file
__cat_apt_sources_list()
{
    local pattern="$1"
    local file="$2"
    test -r "$file" || return
    if echo "$file" | grep -q '\.sources$' ; then
        __convert_deb822_to_oneline "$file" | grep -- "$pattern" | grep -v -- "^ *\$"
    else
        grep -- "$pattern" "$file" | grep -v -- "^ *\$"
    fi
}

__print_apt_sources_list()
{
    local i
    for i in "$@" ; do
        __cat_apt_sources_list "^[^#]*$PKGFORMAT" "$i"
    done
}

__print_apt_sources_list_full()
{
    local i
    for i in "$@" ; do
        __cat_apt_sources_list "^[[:space:]]*#*[[:space:]]*$PKGFORMAT" "$i"
    done
}

__print_apt_sources_list_disabled()
{
    local i
    for i in "$@" ; do
        __cat_apt_sources_list "^[[:space:]]*#[[:space:]]*$PKGFORMAT" "$i"
    done
}

__print_file_header()
{
    set_boldcolor $CYAN
    echo "$1:" >&2
    restore_color
}

# repo_filter_patterns is set by print_apt_sources_list
__print_apt_sources_list_verbose()
{
    local i content found=''
    for i in "$@" ; do
        content="$(__cat_apt_sources_list "^[^#]*$PKGFORMAT" "$i" | __filter_repos_list $repo_filter_patterns)"
        [ -n "$content" ] || continue
        found=1
        __print_file_header "$i"
        echo "$content" | sed -e 's|^|    |'
    done
    [ -n "$found" ]
}

__print_apt_sources_list_verbose_full()
{
    local i content found=''
    for i in "$@" ; do
        content="$(__cat_apt_sources_list "^[[:space:]]*#*[[:space:]]*$PKGFORMAT" "$i" | __filter_repos_list $repo_filter_patterns)"
        [ -n "$content" ] || continue
        found=1
        __print_file_header "$i"
        # highlight commented lines
        echo "$content" | sed -e 's|^|    |' -e "s|^\(    #.*\)|$(set_color $WHITE)\1$(restore_color)|"
    done
    [ -n "$found" ]
}

__print_apt_sources_list_verbose_disabled()
{
    local i content found=''
    for i in "$@" ; do
        content="$(__cat_apt_sources_list "^[[:space:]]*#[[:space:]]*$PKGFORMAT" "$i" | __filter_repos_list $repo_filter_patterns)"
        [ -n "$content" ] || continue
        found=1
        __print_file_header "$i"
        echo "$content" | sed -e 's|^|    |'
    done
    [ -n "$found" ]
}

print_apt_sources_list()
{
    local LISTS="$APT_ALL_SOURCES_LIST"
    # flagall, flagdisabled are set by epm_repolist()

    if [ -n "$quiet" ] ; then
        if [ -n "$flagall" ] ; then
            __print_apt_sources_list_full $LISTS | __filter_repos_list "$@"
        elif [ -n "$flagdisabled" ] ; then
            __print_apt_sources_list_disabled $LISTS | __filter_repos_list "$@"
        else
            __print_apt_sources_list $LISTS | __filter_repos_list "$@"
        fi
    else
        repo_filter_patterns="$*"
        if [ -n "$flagall" ] ; then
            __print_apt_sources_list_verbose_full $LISTS
        elif [ -n "$flagdisabled" ] ; then
            __print_apt_sources_list_verbose_disabled $LISTS
        else
            __print_apt_sources_list_verbose $LISTS
        fi
    fi
}


epm_repolist()
{

flagall=''
flagdisabled=''

while [ -n "$1" ] ; do
    case "$1" in
        -h|--help)            # HELPOPT: show this help
            epm_repolist_help
            return
            ;;
        -a|--all)             # HELPOPT: show all repos (including disabled/commented)
            flagall=1
            ;;
        -d|--disabled)        # HELPOPT: show only disabled (commented) repos
            flagdisabled=1
            ;;
        --regex|--regexp)     # HELPOPT: use pattern as raw regex (no glob conversion)
            # handled globally in epm via $regexp variable
            ;;
        *)
            break
            ;;
    esac
    shift
done

# TODO
[ -z "$*" ] || [ "$PMTYPE" = "apt-rpm" ] || [ "$PMTYPE" = "apm-rpm" ] || [ "$PMTYPE" = "apt-dpkg" ] || fatal "No arguments are allowed here"

case $PMTYPE in
    apt-rpm|apm-rpm)
        print_apt_sources_list "$@"
        ;;
    deepsolver-rpm)
        docmd ds-conf
        ;;
    apt-dpkg|aptitude-dpkg)
        print_apt_sources_list "$@"
        ;;
    yum-rpm)
        docmd yum repolist $verbose
        [ -n "$verbose" ] || info "Use --verbose if you need detail information."
        ;;
    dnf-rpm|dnf5-rpm)
        docmd dnf repolist $verbose
        [ -n "$verbose" ] || info "Use --verbose if you need detail information."
        ;;
    urpm-rpm)
        docmd urpmq --list-media active --list-url
        ;;
    apk)
        cat /etc/apk/repositories
        ;;
    zypper-rpm)
        docmd zypper sl -d
        ;;
    packagekit)
        docmd pkcon repo-list
        ;;
    emerge)
        docmd eselect profile list
        docmd layman -L
        ;;
    xbps)
        docmd xbps-query -L
        ;;
    winget)
        docmd winget source list
        ;;
    eoget)
        docmd eoget list-repo
        ;;
    pisi)
        docmd pisi list-repo
        ;;
    pacman)
        if [ -f /etc/pacman.d/mirrorlist ] ; then
            docmd grep -v -- "^#\|^$" /etc/pacman.d/mirrorlist | grep "^Server =" | sed -e 's|^Server = ||'
        else
            docmd grep -v -- "^#\|^$" /etc/pacman.conf
        fi
        ;;
    slackpkg)
        docmd grep -v -- "^#\|^$" /etc/slackpkg/mirrors
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE in epm_repolist()'
        ;;
esac

}
