#!/bin/sh

ftp_prefix=/mnt/hdd/opt
hasher_prefix=~/hasher

gen_list() {

master_dirs="${ftp_prefix}/ALTLinux/Master/2.4/SRPMS.main \
	     ${ftp_prefix}/ALTLinux/Master/2.4/SRPMS.contrib \
	     ${ftp_prefix}/ALTLinux/Master/2.4/SRPMS.nonfree"

updates_dirs="${ftp_prefix}/ALTLinux/updates/Master/2.4/SRPMS.updates"

backports_dirs="${ftp_prefix}/ALTLinux/backports/Master/2.4/SRPMS.backports"

#for dir in ${master_dirs} ${updates_dirs} ${backports_dirs}; do
for dir in ${master_dirs} ${updates_dirs} ${backports_dirs}; do
	for file in ${dir}/*.rpm; do
	    rpmquery --requires -p ${file} | grep -q "^python\b" && echo ${file}
	done
done
}

rpm_compare() {

master_dirs="${ftp_prefix}/ALTLinux/Master/2.4/ALTLinux/RPMS.main \
	     ${ftp_prefix}/ALTLinux/Master/2.4/ALTLinux/RPMS.contrib \
	     ${ftp_prefix}/ALTLinux/Master/2.4/ALTLinux/RPMS.nonfree"

updates_dirs="${ftp_prefix}/ALTLinux/updates/Master/2.4/i586/RPMS.updates"

backports_dirs="${ftp_prefix}/ALTLinux/backports/Master/2.4/i586/RPMS.backports"


pushd ${hasher_prefix}/repo/i586/RPMS.hasher > /dev/null
for i in *.rpm; do
	echo "${i}" | grep -q "^rpm\b" && continue
	echo "${i}" | grep -q "^librpm\b" && continue
	echo "${i}" | grep -q "^librpmbuild\b" && continue

	rpmquery --requires -p ${i} > ${i}.req-new.log
	rpmquery --provides -p ${i} > ${i}.prov-new.log
	rpmquery --list -p ${i} > ${i}.list-new.log

    for dir in ${master_dirs} ${updates_dirs} ${backports_dirs}; do
	test -f $dir/$i || continue

	rpmquery --requires -p $dir/$i > $i.req-old.log
	rpmquery --provides -p $dir/$i > $i.prov-old.log
	rpmquery --list -p $dir/$i > $i.list-old.log
	break
    done

    for check in req prov list; do
	if ! test -f $i.$check-new.log -a -f $i.$check-old.log; then
	    echo "# $i $check log is missing!"
	    rm -f $i.$check-old.log $i.$check-new.log
	    continue
	fi
	diff -u $i.$check-old.log $i.$check-new.log > $i.$check.diff
	if ! test -s $i.$check.diff; then
	    rm -f $i.$check-old.log $i.$check-new.log $i.$check.diff
	    echo "# $i $check log matches"
	    continue
	fi
	echo "# $i $check log differs!"
	cat $i.$check.diff
	rm -f $i.$check-old.log $i.$check-new.log $i.$check.diff
    done
    rm -f $i
done
popd > /dev/null
}


rm -f /tmp/list /tmp/hasher.log /tmp/rpm-compare.diff
gen_list >> /tmp/list

for i in `cat /tmp/list`; do
	echo >> /tmp/hasher.log
	echo $i >> /tmp/hasher.log
	echo >> /tmp/hasher.log
	hsh --target i586 ~/hasher $i >> /tmp/hasher.log 2>&1
	rpm_compare >> /tmp/rpm-compare.diff
	rm -f ~/hasher/repo/SRPMS.hasher/`basename $i`
done
