#!/bin/sh
#
# Copyright (C) 2012, 2017, 2020, 2021  Etersoft
# Copyright (C) 2012, 2017, 2020, 2021  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


# like apt-add-repository --remove on deb systems
__epm_removerepo_apt()
{
    local repo="$*"
    [ -n "$repo" ] || fatal "empty repo name"

    if [ -n "$dryrun" ] ; then
        echo "$repo"
        return
    fi

    local sc="sudocmd"
    [ -z "$quiet" ] || sc="sudorun"

    local i
    for i in /etc/apt/sources.list /etc/apt/sources.list.d/*.list ; do
        [ -s "$i" ] || continue
        # touch file only when it is needed
        grep -q -F "$repo" $i || continue
        
        $sc sed -i -e "\|$(sed_escape "$repo")|d" $i
    done
}


__epm_grep_repo_list()
{
    while [ -n "$1" ] ; do
        epm --quiet repo list "$1"
        shift
    done
}

# remove grepped lines
__epm_removerepo_alt_grepremove()
{
    local rl="$*"

    if [ "$rl" = "all" ] ; then
        rl="*"
    fi

    # ^rpm means full string
    if ! rhas "$rl" "^rpm" ; then
        rl="$(__epm_grep_repo_list $rl 2>/dev/null)"
        if [ -z "$rl" ] ; then
            [ -n "$verbose" ] && warning 'Can'\''t find '$*' in the repos (see # epm repolist output)'
            return 1
        fi
    fi

    echo "$rl" | while read rp ; do
        __epm_removerepo_apt "$rp"
    done
}

__epm_removerepo_alt()
{
    local repo="$*"
    [ -n "$repo" ] || fatal "No such repo or task. Use epm repo remove <regexp|autoimports|archive|tasks|TASKNUMBER>"

    if tasknumber "$repo" >/dev/null ; then
        local tn
        for tn in $(tasknumber "$repo") ; do
            __epm_removerepo_alt_grepremove " repo/$tn/" "/tasks/$tn " "/$tn[ /]build/repo"
        done
        return
    fi

    local branch="$(echo "$DISTRVERSION" | tr "[:upper:]" "[:lower:]")"

    case "$1" in
        autoimports)
            info "removing autoimports repo"
            [ -n "$DISTRVERSION" ] || fatal "Empty DISTRVERSION"
            repo="autoimports.$branch"
            __epm_removerepo_alt_grepremove "$repo/"
             ;;
        archive)
            info "removing archive repos"
            __epm_removerepo_alt_grepremove "archive/"
            ;;
        korinf)
            info "removing korinf repo"
            __epm_removerepo_alt_grepremove "Korinf/"
            ;;
        cdroms)
            info "removing cdroms entries"
            __epm_removerepo_alt_grepremove "/^[[:space:]]*rpm[[:space:]]+cdrom:/" "/^cdrom(s)?$/"
            ;;
        tasks)
            info "removing tasks' repos"
            __epm_removerepo_alt_grepremove " repo/[0-9]+/" "/tasks/[0-9]+ " "/[0-9]+[ /]build/repo"
            ;;
        task)
            shift
            __epm_removerepo_alt_grepremove " repo/$1/" "/tasks/$1 " "/$1[ /]build/repo"
            ;;
        -*)
            fatal "epm removerepo: no options are supported"
            ;;
        *)
            if echo "$*" | grep -q "^rpm" ; then
                __epm_removerepo_apt "$*"
            else
                info "removing with grep by '$*'"
                __epm_removerepo_alt_grepremove "$*"
            fi
            ;;
    esac

}

epm_removerepo()
{

case $BASEDISTRNAME in
    "alt")
        __epm_removerepo_alt "$@"
        return
        ;;
    "astra")
        echo "Use workaround for AstraLinux"
        # aptsources.distro.NoDistroTemplateException: Error: could not find a distribution template for AstraLinuxCE/orel
        __epm_removerepo_apt "$@"
        return
        ;;
esac;

case $PMTYPE in
    apt-dpkg)
        assure_exists apt-add-repository software-properties-common
        # FIXME: it is possible there is troubles to pass the args
        sudocmd apt-add-repository --remove "$*"
        info "Check file /etc/apt/sources.list if needed"
        ;;
    aptitude-dpkg)
        info "You need remove repo from /etc/apt/sources.list"
        ;;
    yum-rpm)
        assure_exists yum-utils
        sudocmd yum-config-manager --disable "$@"
        ;;
    dnf-rpm)
        repo_file_name=$(env LC_ALL=C dnf repoinfo "$@" 2>/dev/null | sed -n 's/^Repo-filename\s*:\s*//p')
        sudocmd rm "$repo_file_name"
        ;;
    dnf5-rpm)
        repo_file_name=$(env LC_ALL=C dnf repoinfo "$@" 2>/dev/null | sed -n 's/^Config file\s*:\s*//p')
        sudocmd rm "$repo_file_name"
        ;;
    urpm-rpm)
        if [ "$1" = "all" ] ; then
            sudocmd urpmi.removemedia -av
            return
        fi
        sudocmd urpmi.removemedia "$@"
        ;;
    zypper-rpm)
        sudocmd zypper removerepo "$@"
        ;;
    emerge)
        sudocmd layman "-d$1"
        ;;
    pacman)
        info "You need remove repo from /etc/pacman.conf"
        ;;
    npackd)
        sudocmd npackdcl remove-repo --url="$*"
        ;;
    winget)
        sudocmd winget source remove "$@"
        ;;
    eopkg)
        sudocmd eopkg remove-repo "$@"
        ;;
    pisi)
        sudocmd pisi remove-repo "$@"
        ;;
    slackpkg)
        info "You need remove repo from /etc/slackpkg/mirrors"
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE'
        ;;
esac

}