#! /bin/bash
#                               -*- Mode: Sh -*- 
# satisfydeps.sh --- 
# Author           : Manoj Srivastava ( srivasta@glaurung.internal.golden-gryphon.com ) 
# Created On       : Sun Oct  1 18:59:05 2006
# Created On Node  : glaurung.internal.golden-gryphon.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Mon Oct  2 00:57:49 2006
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 99
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
# 

set -e

MY_ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)

control=$1
if [ -z "$control" ] || [ ! -e "$control" ]; then
    echo >&2 "Unspecified control file."
    exit 1
fi

packages_to_install=
SATISFIED=

dep_line=$(sed -n 's/^Build-Depends[^:]*://p' "$control" | tr " " "/")

for my_depends in $(echo "$dep_line" | awk 'BEGIN{RS=","} {print}'); do
    if [  -z "$my_depends" ]; then continue; fi
    depends=$(echo "$my_depends" tr "/" " ")
    # Have each depends, version numbers, and alternates here
    alternates_p=$(echo "$depends" | sed -n 's/.*|.*/YES/p')
    : ${alternates_p:="NO"}
    SATISFIED=


    my_dep=$(echo "$depends" | tr " " "/")
    for my_alt in $(echo "$my_dep" | awk 'BEGIN{RS="|"} {print}' ); do
        if [  -z "$my_alt"    ]; then continue; fi
        if [  -n "$SATISFIED" ]; then continue; fi
        alt=$(echo "$my_alt" | tr "/" " ")
        package=$(echo "$alt" | awk '{print $1}')

        # If there are arch specific dependencies, we handle them here.
        NEED_THIS='YES'
        arches=$(echo "$alt" | sed -n 's/.*\[\(.*\)\].*/\1/p')
        if [ -n "$arches" ]; then
            if echo "$arches" | grep "[!]" > /dev/null; then
                # Full ofnegated arches
                if echo "$arches" | grep "[!]$MY_ARCH" > /dev/null; then
                    NEED_THIS='NO'
                    SATISFIED='YES'
                    echo >&2 "Do not need $package for $MY_ARCH"
                    break
                fi
            else
                # List of arches
                if ! echo "$arches" | grep "$MY_ARCH" > /dev/null; then
                    NEED_THIS='NO'
                    SATISFIED='YES'
                    echo >&2 "Do not need $package for $MY_ARCH"
                    break
                fi
            fi
        fi
        

        # If we need this alt, let us see if we have a version that matches
        if [ "$NEED_THIS" = "YES" ]; then 
            # If we have versioned depends, handl them here
            HAVE_VERSION='YES'
            versions=$(echo "$alt" | sed -n 's/.*(\(.*\)).*/\1/p')
            if [ -n "$versions" ]; then 
                compare=$(echo "$versions" | awk '{print $1}')
                number=$(echo "$versions" | perl -ple 's/[ \<\>\=]+//g')
                avail=$(apt-cache show "$package" | \
                    sed -n  's/^Version: \(.*\)$/\1/p' | head -n 1)
                if ! dpkg --compare-versions "$avail"  $compare $number; then
                    HAVE_VERSION='NO'
                fi
            fi
            
            if [ "$HAVE_VERSION" = "YES" ]; then
                # We need this dep, matches the arch, have the proper
                # version available
                if apt-get -s install "$package" >/dev/null 2>&1; then
                    packages_to_install="$package $packages_to_install";
                    SATISFIED='YES'
                    break
                else
                    echo >&2 ERROR
                    apt-get -s install "$package"
                fi
            fi

            # Darn. Try for provides?
            PROVIDED=$(apt-cache showpkg "$package" | awk 'BEGIN{p=0} 
                 /^Reverse Provides:/,/^$/{p=1} 
                 {if(p && ($0 !~ "Reverse Provides:")){PACKAGE="$PACKAGE $1"}}
                 END{print PACKAGE}')
            for provider in $PROVIDED; do
                if apt-get -s install "$provider" >/dev/null 2>&1; then
                    packages_to_install="$provider $packages_to_install";
                    SATISFIED='YES'
                    break 2
                fi
            done
        fi
    done
    if [ -z "$SATISFIED" ]; then
        echo >&2 "Could not satisfy $depends($SATISFIED)"
        exit 2
    fi
done

echo "$packages_to_install"
