#DEFAULT=SL
#DEFAULT_DIR=../..
#DEBUGINFO_DIR=../../../archive/debuginfo/
#OBSOLETE_DIR=$OBSOLETE_DIR
#SRPMS_DIR=../../../SRPMS/vendor/
if [ -s locations.include ] ; then
  . locations.include
else
  echo "Need locations.include to continue"
  exit 1
fi
echo `date` >> errata.log
echo `date` >> new.log
mv *debuginfo*.rpm $DEBUGINFO_DIR
/bin/mv -f *src.rpm $SRPMS_DIR
for i in *.rpm
do
	if [ -f $DEFAULT_DIR/$DEFAULT/$i ] ; then
		echo "Already have $i in $DEFAULT, so deleting it."
		rm $i
		continue
	fi
	if [ -f $OBSOLETE_DIR$i ] ; then
		echo "Already have $i in obsolete, so deleting it."
		rm $i
		continue
	fi
	NEWNAME=`rpm -qp $i --qf "%{name}"`
	NEWARCH=`rpm -qp $i --qf "%{arch}"`
	NEWRPMVERSION=`rpm -qp --queryformat "%{epoch}:%{version}-%{release}" $i `
	#echo "Looking for Name: $NEWNAME  Arch: $NEWARCH"
	OLDRPM=""
	cd $DEFAULT_DIR/$DEFAULT/ > /dev/null
	LIST=`ls -1 ${NEWNAME}* 2> /dev/null`
	for line in $LIST
	do
		OLDNAME=`rpm -qp $line --qf "%{name}"`
		if [ "${OLDNAME}" == "${NEWNAME}" ] ; then
			OLDARCH=`rpm -qp $line --qf "%{arch}"`
			if [ "${OLDARCH}" == "${NEWARCH}" ] ; then
				OLDRPM="$line"
				OLDRPMVERSION=`rpm -qp --queryformat "%{epoch}:%{version}-%{release}" $OLDRPM `
				#echo "Found a match: $line"
			fi
		fi
	done
	cd - > /dev/null
	if [ "$OLDRPM" == "" ] ; then
		echo ""	
		echo "new: $i"
		echo "I could not find a match for this.  Should I just move it in?"
		echo "Enter skip if I should skip this one"
		read new
		if [ -n "$new" ] ; then
			if [ $new = "skip" ] ; then
				echo "... Skipping"
	        	fi	
		else
			echo "Moving it in"
			mv -v $i $DEFAULT_DIR/$DEFAULT/
			echo -e "\t$i" >> new.log  
		fi
	else
		echo ""
		NEWERISNEWER=0
		echo "old: $OLDRPM"
		echo "new: $i"
		RESULT=`./rpmdev-vercmp $OLDRPMVERSION $NEWRPMVERSION`
                EXITCODE=$?
                if [ $EXITCODE -eq 12 ] ; then  # right side was newer
                   echo "I think that the NEW rpm is newer , should I go ahead?"
                   NEWERISNEWER=1
                else
                    if [ $EXITCODE -eq 11 ] ; then  # left side was newer
                      echo "I think that the OLD rpm is newer , should I leave it?"
                      NEWERISNEWER=0
                    else
                      echo "I do not know which version is newer"
                      echo "RPM Comparision is $RESULT"
                      break
                    fi
                fi
		echo "Enter skip if I should skip this one"
		read new
 		if [ -n "$new" ] ; then
			if [ $new = "skip" ] ; then
				echo "... Skipping"
	                fi	
		else
			if [ $NEWERISNEWER -eq 1 ] ; then
                           echo "Replacing old with new"
			   mv -v $DEFAULT_DIR/$DEFAULT/$OLDRPM $OBSOLETE_DIR
			   mv -v $i $DEFAULT_DIR/$DEFAULT/
                           echo -e "\t$i" >> errata.log
			else
                           echo "Leaving old"
			fi
		fi
	fi
done
