#!/bin/bash
#
#   rpm_install -   optionally installs all missing packages require by the
#                   specified package via recursive calls
#
#   SYNOPSIS
#       rpm_install install_pkg
#
#       Where install_pkg is the filename of the RPM package file to install
#

PATH="$PATH:/cooker"                    # Add path to rpm_install script
RPM_DIRs="/cooker/cooker/Mandrake/RPMS" # List of RPM directories
RPM_DB="/cooker/rpm_db"                 # A database of RPM --provides

echo $2 "Installing: $(basename $1)"

install_pkg=$1              # package to install
indent="===$2"              # level of recursion indication
shift; shift;
pkgs="$install_pkg $*"

if [ ! -f ${RPM_DB}.pkg ]   # check for database
then
    echo "ERROR: RPM 'provides' database not found"
    echo "       ${RPM_DB}.pkg"
    exit 1          # return error to parent
fi

function find_missing_package()
{
    need=$(
        rpm -i --test $install_pkg 2>&1 | grep " is needed by " | \
            awk 'END {print $1}'
    )
}

function find_package()
{
    pkg=$(
        (
        grep '[[:space:]]'"$need"'  $' ${RPM_DB}.pkg | awk '{print $1}'
        grep '[[:space:]]'"$need"' = ' ${RPM_DB}.pkg | awk '{print $1}'
        ) | sort | uniq
    )
    if [ $(echo $pkg | wc -w) -gt 1 ]   # check if more that 1 package
    then
        echo $indent "WARNING: more than one package found which provides"
        echo $indent "         $need"
        count=0
        for name in $pkg
        do
            count=$(($count + 1))
            echo $indent "$count: $name"
        done
        echo -n $indent "Select which RPM to install (by index number): "
        read index
        if [ -z "$index" -o "$(echo $index|sed 's/[0-9]//g')" ]
        then
            echo $indent "ERROR: bad input for index number: $index"
            exit 1      # return error to parent
        elif [ "$index" -lt 1 -o "$index" -gt $count ]
        then
            echo $indent "ERROR: bad input for index number: $index"
            exit 1      # return error to parent
        fi
        pkg=$(echo $pkg | awk -v i=$index '{print $i}')
    fi
    if [ -z "$pkg" ]    # check that RPM package file was found
    then
        echo $indent "ERROR: $need not found in available RPM package files"
        exit 1          # return error to parent
    fi
    for old_pkg in $pkgs        # check that RPM package file is not repeated
    do
        if [ "$pkg" = "$old_pkg" ]
        then
            echo $indent "ERROR: bad dependency between RPM package files;"
            echo $indent "       $install_pkg"
            echo $indent "       $old_pkg"
            exit 1          # return error to parent
        fi
    done
}

if [ ! -f $install_pkg ]    # check that RPM package file exists
then
    echo $indent "ERROR: $install_pkg RPM package file not found"
    exit 1          # return error to parent
fi

need=junk               # initialize loop
while [ -n "$need" ]    # loop while installing missing RPM package files
do
    find_missing_package
    if [ -n "$need" ]   # check if missing anything
    then
        find_package
        rpm_install $pkg $indent $pkgs      # RECURSIVE CALL
        rtn=$?
        if [ $rtn -ne 0 ]   # check for problems installing missing package
        then
            exit 1          # return error to parent
        fi
    fi
done

rpm -i --test $install_pkg 2>&1 # test for any remaining probems, output to user
rtn=$?
if [ $rtn -ne 0 ]   # check if rpm test returned problems with package install
then
    echo $indent "ERROR: installing $install_pkg"
    exit 1          # return error to parent
fi

# user option to install
echo -n $indent "Install $(basename $install_pkg) ? ([y]/n): "
read ans
if [ "$ans" = N -o "$ans" = n ]
then
    exit 1          # return error to parent
fi

rpm -i $install_pkg 2>&1    # finally install package
rtn=$?
sleep 1             # rpm bug ... need to wait a second for cleanup

if [ $rtn -ne 0 ]   # check that package installed without errors
then
    exit 1          # return error to parent
fi

exit 0              # return OK to continue to parent
