#!/bin/sh
#
# Copyright (C) 2025  Etersoft
# Copyright (C) 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/>.
#

# APT sources paths with test isolation support (EPM_APT_SOURCES_ROOT)
APT_SOURCES_LIST="${EPM_APT_SOURCES_ROOT%/}/etc/apt/sources.list"
APT_SOURCES_LIST_D="${EPM_APT_SOURCES_ROOT%/}/etc/apt/sources.list.d"
APT_ALL_SOURCES_LIST="$APT_SOURCES_LIST_D/*.list $APT_SOURCES_LIST"

# Filter stdin by patterns (AND logic, case insensitive)
# Supports glob (* ?) and exclusion (~pattern)
# Usage: echo "$lines" | __filter_repos_list pattern1 pattern2 ~exclude
__filter_repos_list()
{
    local i
    [ -z "$*" ] && cat && return

    # Check if this is a full repo line (rpm or deb) - use literal matching for the entire string
    if echo "$1" | grep -q '^rpm\|^deb' && [ $# -eq 1 ] ; then
        grep -F -i -- "$1"
        return
    fi

    local list=""
    local listN=""
    for i in "$@" ; do
        case "$i" in
            ~*)
                listN="$listN ${i#\~}"
                ;;
            *)
                list="$list $i"
                ;;
        esac
    done

    # convert glob to regexp
    list=$(__convert_glob__to_regexp "$list")
    listN=$(__convert_glob__to_regexp "$listN")

    listN=$(strip_spaces $listN | sed -e 's/ /|/g')

    local result
    result="$(cat)"

    # exclude patterns
    [ -n "$listN" ] && result="$(echo "$result" | grep -E -i -v -- "$listN")"

    # include patterns (AND)
    for i in $list ; do
        result="$(echo "$result" | grep -E -i -- "$i")"
    done

    [ -n "$result" ] && echo "$result"
}
