#!/bin/bash

#converts glibc-2.3.5-i486-5 to glibc
completetoname(){
        NUMFIELDS=`echo "$1" | awk -F - '{print NF}'`
        if test $NUMFIELDS -ge 4
        then
                echo ${1%-*-*-*}
        fi
}

if test $# -ne 1
then
	echo "usage: compare.sh /path/to/PACKAGES.TXT"
	exit 1
fi

case $1 in
/*);;
*)echo "the first argument must be an absolute path"; exit;;
esac

cd /var/log/packages
for i in *
do
	name="`completetoname $i`"
	deps=`cat $1 | sed /"PACKAGE NAME:  $name-"/,/"PACKAGE NAME"/\!d | grep "PACKAGE REQUIRED:"`
	if test -z "$deps"
	then
		continue
	fi
	deps=${deps#PACKAGE REQUIRED:}
	num="`echo $deps | awk -F "," '{print NF}'`"
	j=1
	while test $j -le $num
	do
		dep="`echo $deps | cut -d "," -f $j`"
		req="`ls $dep-* 2> /dev/null`"
		F=0
		for z in $req
		do
			if test "`completetoname $z`" != "$dep"
			then
				continue
			else
				F=1
				break
			fi
		done
		if test $F -eq 0
		then
			echo "Missing dependency of $name: $dep"
		fi
		j=$(($j + 1))
	done
done
