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

__epm_changelog_apt()
{
    local i
    for i in $@ ; do
        docmd apt-cache show $i | grep -A 1000 "^Changelog:"
    done
}

__epm_changelog_dpkg()
{
    local i
    tmpdir="$(mktemp -d)" || fatal
    remove_on_exit "$tmpdir"

    for i in $@ ; do
        docmd dpkg-deb -x $i $tmpdir/extract || fatal
        docmd zcat $tmpdir/extract/usr/share/doc/*/changelog*.gz
        rm -rf $tmpdir/extract
    done
}

__epm_changelog_files()
{
    [ -z "$*" ] && return

    # TODO: detect every file
    case $(get_package_type $1) in
        rpm)
            assure_exists rpm
            docmd_foreach "rpm -q -p --changelog" $@
            ;;
        deb)
            __epm_changelog_dpkg "$@"
            ;;
        *)
            fatal 'Have no suitable command for $1 in epm_changelog()'
            ;;
    esac
}

__epm_changelog_local_names()
{
    [ -z "$*" ] && return

    case $PMTYPE in
        *-rpm)
            docmd_foreach "rpm -q --changelog" $@
            ;;
        apt-dpkg|aptitude-dpkg)
            docmd zcat /usr/share/doc/$1/changelog.Debian.gz
            ;;
        emerge)
            assure_exists equery
            docmd equery changes -f $1
            ;;
        pacman)
            docmd pacman -Qc $1
            ;;
        *)
            fatal 'Have no suitable command for $PMTYPE in epm_changelog()'
            ;;
    esac
}

__epm_changelog_unlocal_names()
{
    [ -z "$*" ] && return

    case $PMTYPE in
        apt-rpm)
            __epm_changelog_apt "$1"
            ;;
        apt-dpkg)
            #assure_exist apt
            #docmd apt changelog "$1"
            docmd apt-get changelog "$1"
            ;;
        yum-rpm)
            assure_exist yum-plugin-changelog
            docmd yum changelog "$1"
            ;;
        dnf-rpm|dnf5-rpm)
            #assure_exist yum-changelog
            docmd dnf changelog "$1"
            ;;
        urpm-rpm)
            docmd urpmq --changelog "$1"
            ;;
        #zypper-rpm)
        #    sudocmd zypper clean
        #    ;;
        emerge)
            assure_exists equery
            docmd equery changes -f "$1"
            ;;
        *)
            fatal 'Have no suitable command for $PMTYPE in epm_changelog(). Try install the package firstly.'
            ;;
    esac

}


epm_changelog()
{
    [ -n "$pkg_filenames" ] || fatal "Changelog: Missing package(s) name"

    __epm_changelog_files $pkg_files

    # TODO: add less or bat
    local pkg
    for pkg in $pkg_names ; do
        if is_installed $pkg ; then
            __epm_changelog_local_names $pkg
        else
            __epm_changelog_unlocal_names $pkg
        fi
    done
}
