#!/bin/sh
#
# Copyright (C) 2013, 2016, 2018  Etersoft
# Copyright (C) 2013, 2016, 2018  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-print

epm_whatprovides_help()
{
    message 'epm whatprovides - print packages that provide the given target
Usage: epm whatprovides [options] <target>
'
    get_help HELPOPT $SHAREDIR/epm-whatprovides
}

__epm_whatprovides_installed()
{
    local pkg="$*"

    case $PMTYPE in
        *-rpm)
            if [ -n "$short" ] ; then
                docmd rpm -q --whatprovides --queryformat '%{name}\n' $pkg
            else
                docmd rpm -q --whatprovides $pkg
            fi
            ;;
        *-dpkg)
            # get providers list from apt-cache and filter to installed
            local providers
            providers="$(apt-cache showpkg $pkg 2>/dev/null | sed -n '/^Reverse Provides:/,$ {/^Reverse Provides:/d; p}' | awk '{print $1}' | sort -u)"
            [ -n "$providers" ] || return 1
            local p
            for p in $providers ; do
                dpkg -l "$p" 2>/dev/null | grep -q '^ii' || continue
                if [ -n "$short" ] ; then
                    echo "$p"
                else
                    dpkg-query -W "--showformat=\${Package}-\${Version}\n" "$p" 2>/dev/null
                fi
            done
            ;;
        pacman)
            if [ -n "$short" ] ; then
                docmd pacman -Q $pkg | sed -e 's| .*||'
            else
                docmd pacman -Q $pkg
            fi
            ;;
        *)
            fatal 'Have no suitable command for $PMTYPE in __epm_whatprovides_installed()'
            ;;
    esac
}

epm_whatprovides()
{
    local CMD
    [ -n "$pkg_url" ] && fatal "whatprovides does not handle URLs"

    case "$pkg_options" in
        -h|--help)            # HELPOPT: show this help
            epm_whatprovides_help
            return
            ;;
        --installed)          # HELPOPT: search only installed packages
            shift
            ;;
    esac

    [ -n "$pkg_files$pkg_names" ] || fatal "whatprovides: package name is missed"
    local pkg=$(print_name $pkg_files $pkg_names)

    if [ "$pkg_options" = "--installed" ] ; then
        __epm_whatprovides_installed $pkg
        return
    fi

# by package name
case $PMTYPE in
    conary)
        CMD="conary repquery --what-provides"
        ;;
    apt-rpm|apt-dpkg|aptitude-dpkg)
        LC_ALL=C docmd apt-get install --print-uris $pkg | grep "^Selecting" | cut -f2 -d" "
        return
        ;;
    yum-rpm)
        CMD="yum whatprovides"
        ;;
    urpm-rpm)
        CMD="urpmq --whatprovides"
        ;;
    dnf-rpm|dnf5-rpm)
        CMD="dnf repoquery --whatprovides"
        ;;
    zypper-rpm)
        CMD="zypper what-provides"
        ;;
    opkg)
        CMD="opkg whatprovides"
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE in epm_whatprovides()'
        ;;
esac

docmd $CMD $pkg

}
