#! /bin/bash
# $Id: resolve-builddeps,v 1.2 2003/12/05 02:17:47 ensc Exp $

# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# This program 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; version 2 of the License.
#  
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Usage: resolve-deps <aptlist-dir> <spec-file> <rootdir> <rpm-options>*


unset DJINNI_VSERVERNAME
. /etc/sysconfig/fedora.us-build

aptlistdir=$1
specfile=$2
rootdir=$3

style=apt

shift
shift
shift

args=()
while test "$1"; do
    case "$1" in
	--nodeps|--force)	break;;
	--target=*)
	    args=( "${args[@]}" "$1" );
	    ;;
	--define|--with*|--target)
	    args=( "${args[@]}" "$1" "$2" );
	    shift
	    ;;
	*)
	    echo "resolve-deps: unknown option '$1'; assuming it as a single option" >&2
    esac
    shift
done

tmp0=$(mktemp /tmp/resolve-deps.XXXXXX)
tmp1=$(mktemp /tmp/resolve-deps.XXXXXX)
tmp2=$(mktemp /tmp/resolve-deps.XXXXXX)
trap "rm -f $tmp{0,1,2}" EXIT

$RPM_GETBUILDDEPS --chroot "$rootdir" --uid 500 --gid 500 "$specfile" "${args[@]}" >$tmp0 || exit 1


function handleDepSet
{
    local flag
    
    case "$3" in
	/*)		echo "$@";;
	rpmlib\(*\))	;;
	*)
	    flag=
	    test    $[ $2 & 8 ] -eq 0 || flag='='
	    if test $[ $2 & 4 ] -ne 0; then
		flag=">$flag"
	    elif test $[ $2 & 2 ] -ne 0; then
		flag="<$flag"
	    fi

	    case "$style" in
		apt)	test -z "$flag" || flag="$flag$4";;
		*)	;;
	    esac

	    echo $3$flag >&3
	    ;;
    esac
}

while read type flags n evr; do
    test "$type" || continue
    
    case "$type" in
	-)	;; # ignore BuildConflicts
	+)	handleDepSet "$type" 0x"$flags" "$n" "$evr";;
	*)	echo "Unexpected type '$type'"; exit 1;;
    esac
done <$tmp0 3>$tmp2 | $APTLIST_RESOLVEDEPS "$aptlistdir"/*

cat $tmp2
