#!/bin/sh
#
# Copyright (C) 2012  Etersoft
# Copyright (C) 2012  Vitaly Lipatov <lav@etersoft.ru>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

# TODO: use when run install with epm --skip-installed install
filter_out_installed_packages()
{
	[ -z "$skip_installed" ] && cat && return

	# TODO: use this more effectively way
	#for i in $(cat) ; do
	#	rpm -q $i >/dev/null && continue
	#	echo $i
	#done | 
	case $PKGFORMAT in
		"rpm")
			LANG=C LC_ALL=C xargs -n1 rpm -q 2>&1 | grep 'is not installed' |
				sed -e 's|^.*package \(.*\) is not installed.*|\1|g'
			;;
		"deb")
			LANG=C LC_ALL=C xargs -n1 dpkg -L 2>&1 | grep 'is not installed.' |
				sed -e 's|^Package .\(.*\). is not installed.*|\1|g'
			;;
		*)
			cat
			;;
	esac | sed -e "s|rpm-build-altlinux-compat||g" | filter_strip_spaces
}


# copied from etersoft-build-utils/share/eterbuild/functions/rpmpkg
epm_install_names()
{
	[ -z "$1" ] && return
	case $PMTYPE in
		apt-rpm|apt-dpkg)
			docmd $SUDO apt-get install $@
			return ;;
		yum-rpm)
			docmd $SUDO yum install $@
			return ;;
		urpm-rpm)
			docmd $SUDO urpmi $@
			return ;;
		zypper-rpm)
			docmd $SUDO zypper install $@
			return ;;
		pkg_add)
			docmd $SUDO pkg_add -r $@
			return ;;
		*)
			fatal "Do not known install command for $PMTYPE"
			;;
	esac
}

epm_ni_install_names()
{
	[ -z "$1" ] && return
	case $PMTYPE in
		apt-rpm|apt-dpkg)
			docmd $SUDO apt-get -y --force-yes install $@
			return ;;
		yum-rpm)
			docmd $SUDO yum -y install $@
			return ;;
		urpm-rpm)
			docmd $SUDO urpmi --auto --no-verify-rpm $@
			return ;;
		zypper-rpm)
			yes | docmd $SUDO zypper --non-interactive install $@
			return ;;
		pkg_add)
			docmd $SUDO pkg_add -r $@
			return ;;
		*)
			fatal "Do not known appropriate install command for $PMTYPE"
			;;
	esac
}

epm_install_files()
{
    [ -z "$1" ] && return

    case $DISTRNAME in
        ALTLinux)
            docmd $SUDO rpm -Uvh --force $@ && return
            docmd $SUDO apt-get install $@
            return ;;
        PCLinux)
            docmd $SUDO rpm -Uvh --force $@
            return ;;
        FreeBSD)
            docmd $SUDO pkg_add $@
            return ;;
        Ubuntu|Debian|Mint)
            docmd $SUDO dpkg -i $@
            docmd $SUDO apt-get -f install
            return ;;
    esac

    # other systems can install file package via ordinary command
    epm_install_names $@
}


epm_install()
{
    [ -n "$pkg_files$pkg_names" ] || fatal "Run install without packages"

    local names=$(echo $pkg_names | filter_out_installed_packages)
    local files=$(echo $pkg_files | filter_out_installed_packages)

    [ -z "$files$names" ] && echo "Skip empty install list" && return 2

    if [ -n "$non_interactive" ] ; then
        epm_ni_install_names $names || return
    else
        epm_install_names $names || return
    fi

    epm_install_files $files
}