#!/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.
#

# copied from etersoft-build-utils/share/eterbuild/functions/rpmpkg
epm_install_names()
{
	[ -n "$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
}

# for low level install
#is_alt && FANCYPERCENT=--fancypercent
#docmd $SUDO rpm -Uvh $FANCYPERCENT "$@"

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

    case $DISTRNAME in
        ALTLinux|PCLinux)
            docmd $SUDO apt-get install $@
            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"

    if [ -n "$non_interactive" ] ; then
        epm_ni_install_names $pkg_names
    else
        epm_install_names $pkg_names
    fi

    epm_install_files $pkg_files

}

