#!/bin/sh
#
# Copyright (C) 2012, 2014, 2016, 2019  Etersoft
# Copyright (C) 2012, 2014, 2016, 2019  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-query
load_helper epm-query_file

epm_info_help()
{
    message 'epm info - print package information
Usage: epm info [options] <package>'
    echo ''
    echog 'Options:'
    get_help HELPOPT $SHAREDIR/epm-info
    echo ''
    echog 'Without options prints general package information.'
    echog 'If a file path is given, shows info for the package owning that file.'
}

__epm_info_rpm_low()
{
    if [ -n "$pkg_files" ] ; then
        docmd rpm -qip $pkg_files
    fi
    [ -z "$pkg_names" ] && return
    is_installed $pkg_names && docmd rpm -qi $pkg_names && return
}

__epm_info_by_pkgtype()
{
    [ -n "$pkg_files" ] || return 1

    case $(get_package_type $pkg_files) in
        rpm)
            __epm_info_rpm_low && return
            ;;
        deb)
            docmd dpkg -I $pkg_files
            ;;
        *)
            return 1
            ;;
    esac
}

__epm_info_by_pmtype()
{
case $PMTYPE in
    apt-dpkg)
        if [ -n "$pkg_files" ] ; then
            docmd dpkg -I $pkg_files
        fi
        [ -z "$pkg_names" ] && return
        is_installed $pkg_names && docmd dpkg -p $pkg_names && return
        docmd apt-cache show $pkg_names
        ;;
    apm-rpm)
        if [ -n "$direct" ] ; then
            __epm_info_rpm_low && return
        fi
        docmd apm system info $full $pkg_names
        ;;
    aptitude-dpkg)
        if [ -n "$pkg_files" ] ; then
            docmd dpkg -I $pkg_files
        fi
        [ -z "$pkg_names" ] && return
        docmd aptitude show $pkg_names
        ;;
    *-rpm)
        __epm_info_rpm_low && return
        case $PMTYPE in
            apt-rpm)
                docmd apt-cache show $pkg_names | awk 'BEGIN{desk=1}{if(/^Changelog:$/){desk=0} else if (desk==1) {print}}'
                ;;
            packagekit)
                docmd pkcon get-details $pkg_names
                ;;
            yum-rpm)
                docmd yum info $pkg_names
                ;;
            urpmi-rpm)
                docmd urpmq -i $pkg_names
                ;;
            dnf-rpm|dnf5-rpm)
                docmd dnf info $pkg_names
                ;;
            zypper-rpm)
                docmd zypper info $pkg_names
                ;;
            *)
                warning "Unknown command for $PMTYPE"
                ;;
        esac
        ;;
    packagekit)
        # TODO: get-details-local
        docmd pkcon get-details $pkg_names
        ;;
    pacman)
        is_installed $pkg_names && docmd pacman -Qi $pkg_names && return
        docmd pacman -Si $pkg_names
        ;;
    aura)
        is_installed $pkg_names && docmd pacman -Qi $pkg_names && return
        docmd aura -Ai $pkg_names
        ;;
    npackd)
        # FIXME: --version=
        docmd npackdcl info --package=$pkg_names
        ;;
    conary)
        is_installed $pkg_names && docmd conary query $pkg_names --info && return
        docmd conary repquery $pkg_names --info
        ;;
    emerge)
        assure_exists equery
        docmd equery meta $pkg_names
        docmd equery which $pkg_names
        docmd equery uses $pkg_names
        docmd equery size $pkg_names
        ;;
    slackpkg)
        docmd /usr/sbin/slackpkg info $pkg_names
        ;;
    opkg)
        docmd opkg info $pkg_names
        ;;
    apk)
        docmd apk info $pkg_names
        ;;
    pkgng)
        docmd pkg info $pkg_names
        ;;
    xbps)
        docmd xbps-query --show $pkg_names
        ;;
    homebrew)
        docmd brew info $pkg_names
        ;;
    aptcyg)
        docmd apt-cyg show $pkg_names
        ;;
    eopkg)
        docmd eopkg info $pkg_files $pkg_names
        ;;
    stplr)
        docmd stplr info $pkg_names
        ;;
    pisi)
        docmd pisi info $pkg_files $pkg_names
        ;;
    appget)
        docmd appget view $pkg_names
        ;;
    winget)
        docmd winget show $pkg_names
        ;;
    termux-pkg)
        docmd pkg show $pkg_names
        ;;
    *)
        fatal 'Have no suitable command for $PMTYPE in epm_info()'
        ;;
esac
}


__epm_info_convert_to_pkgnames()
{
    local f owner
    pkg_files=
    pkg_names=
    for f in $1 ; do
        if is_package_file "$f" ; then
            [ -n "$pkg_files" ] && pkg_files="$pkg_files $f" || pkg_files="$f"
        elif [ -e "$f" ] ; then
            owner="$(__do_query_real_file "$f")"
            if [ -n "$owner" ] ; then
                info 'File $f belongs to package $owner'
                [ -n "$pkg_names" ] && pkg_names="$pkg_names $owner" || pkg_names="$owner"
            else
                warning 'Cannot find package owning $f'
                return 1
            fi
        else
            [ -n "$pkg_names" ] && pkg_names="$pkg_names $f" || pkg_names="$f"
        fi
    done
    pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")
}

# TODO: separate to _files and _names parts
# implement _files part per package, not by PMTYPE (see filelist)
epm_info()
{
    case "$pkg_options" in
        -h|--help)            # HELPOPT: show this help
            epm_info_help
            return
            ;;
        --requires)           # HELPOPT: print package requires (dependencies)
            load_helper epm-requires
            epm_requires
            return
            ;;
        --provides)           # HELPOPT: print package provides
            load_helper epm-provides
            epm_provides
            return
            ;;
        --conflicts)          # HELPOPT: print package conflicts
            load_helper epm-conflicts
            epm_conflicts
            return
            ;;
        --obsoletes)          # HELPOPT: print packages obsoleted by this package
            load_helper epm-info_obsoletes
            epm_info_obsoletes
            return
            ;;
        --recommends)         # HELPOPT: print recommended packages
            load_helper epm-info_recommends
            epm_info_recommends
            return
            ;;
        --suggests)           # HELPOPT: print suggested packages
            load_helper epm-info_suggests
            epm_info_suggests
            return
            ;;
        --changelog)          # HELPOPT: print package changelog
            load_helper epm-changelog
            epm_changelog
            return
            ;;
        --whatdepends)        # HELPOPT: print packages that depend on this
            load_helper epm-whatdepends
            epm_whatdepends
            return
            ;;
        --whatprovides)       # HELPOPT: print packages that provide the target
            load_helper epm-whatprovides
            epm_whatprovides
            return
            ;;
    esac

    # if possible, it will put pkg_urls into pkg_files or pkg_names
    if [ -n "$pkg_urls" ] ; then
        load_helper epm-download
        __handle_pkg_urls_to_checking
    fi

    # convert files/directories to package names
    __epm_info_convert_to_pkgnames "$pkg_filenames" || exit

    if [ -z "$pkg_filenames" ] ; then
        epm_info_help >&2
        exit 1
    fi

    __epm_info_by_pkgtype || __epm_info_by_pmtype

    return $?
}
