#!/usr/bin/perl -w

#Point these to a directory of RH and rebuilt (aka WB) RPMS
$WB_RPMS = "/centos-3/build8/i386/RedHat/RPMS";
$RH_RPMS = "/centos-3/build6/i386/RedHat/RPMS";

# Size difference to trigger error, .1 = plus/minus 10%
$SizeError=.10;

# Working directories
# Careful where these point, they get a "rm -rf" cleaning!
$RHTMP="/tmp/RH-$$";
$WBTMP="/tmp/WB-$$";

opendir(DIR,$RH_RPMS);
@allrpms= grep (/rpm$/, readdir(DIR) );
closedir(DIR);

foreach $rpm (sort @allrpms){
    if (! (-e "$WB_RPMS/$rpm") ) {
	print " No equivalent original for $rpm \t Skipping\n";
	next;
    }
    # Initialize
    %allfiles=(); %RHhash=(); %WBhash=();
    $fileproblems=0; $libproblems=0;
    system("rm -rf $RHTMP;mkdir $RHTMP");
    system("rm -rf $WBTMP;mkdir $WBTMP");
    # Extract RPMS
    system("cd $RHTMP/; rpm2cpio $RH_RPMS/$rpm | cpio -id --no-absolute-filenames --quiet");
    system("cd $WBTMP/; rpm2cpio $WB_RPMS/$rpm | cpio -id --no-absolute-filenames --quiet");

    # Parse name/size info
    @RHnamesize=`cd $RHTMP/; find ./ -printf "%p:_:%s:_:%U:_:%G:_:%04m:_:\n"`;
    @WBnamesize=`cd $WBTMP/; find ./ -printf "%p:_:%s:_:%U:_:%G:_:%04m:_:\n"`;
    for ($i=0;$i<@RHnamesize;$i++){
	($file,$size,$uid,$gid,$perms)=split(/:_:/,$RHnamesize[$i]);
	$RHhash{"$file"}=$size;
	$RHuid{"$file"}=$uid;
	$RHgid{"$file"}=$gid;
	$RHperms{"$file"}=$perms;
	$allfiles{"$file"}=1;
    }
    for ($i=0;$i<@WBnamesize;$i++){
	($file,$size,$uid,$gid,$perms)=split(/:_:/,$WBnamesize[$i]);
	$WBhash{"$file"}=$size;
        $WBuid{"$file"}=$uid;
        $WBgid{"$file"}=$gid;
        $WBperms{"$file"}=$perms;
	$allfiles{"$file"}=1;
    }

    
    # Print and tag file errors if appropraite
    printf ("%-50s\t", $rpm);
    foreach $file (keys %allfiles){
	if (!(exists $RHhash{"$file"})){
	    print "\n\tPackage contains extra file:  $file";
	    $fileproblems=1;
	}
	elsif (!(exists $WBhash{"$file"})){
	    print "\n\tPackage is missing file: $file";
	    $fileproblems=1;
	}
	else {
	    if ($RHhash{"$file"}>0){
		$ratio=($WBhash{"$file"})/($RHhash{"$file"})}
	    else{
		$ratio=1-$WBhash{"$file"};  # Negative ratios indicate filesize vs. zero in RedHat
	        }
	    if (($ratio-1)*($ratio-1)>($SizeError*$SizeError)) {
		printf("\n\tSize Ratio: %3.2f on file:%s",$ratio,$file);
		$fileproblems=1;
	        };

            if ($RHuid{"$file"} ne $WBuid{"$file"}) {
		printf("\n\tUid Diff $RHuid{\"$file\"} : $WBuid{\"$file\"} on $file");
		}
            if ($RHgid{"$file"} ne $WBgid{"$file"}) {
                printf("\n\tGid Diff $RHgid{\"$file\"} : $WBgid{\"$file\"} on $file");
                }
            if ($RHperms{"$file"} ne $WBperms{"$file"}) {
                printf("\n\tPerms Diff $RHperms{\"$file\"} : $WBperms{\"$file\"} on $file");
                }
	   }
    }

    
    # Select executable files
    @RHexec=`cd $RHTMP/; find ./ -perm +111 -type f -print`; 

    # Check library dependencies
    foreach $executable (@RHexec) {
	chop $executable;

	#  Move on if no equivalent binary exists in rebuild
	if (! (-e "$WBTMP/$executable") ) {next;}

	# Get shared libs
	@RHlibs=`ldd $RHTMP/$executable`;
	@WBlibs=`ldd $WBTMP/$executable`;

        # Clean up ldd output
	$RHlibline=join('',(sort @RHlibs)); 
	$WBlibline=join('',(sort @WBlibs)); 
	$RHlibline=~ s/=>(.*)(\n)/\n/g; $RHlibline=~ s/(\s+)/ /g; 
	$WBlibline=~ s/=>(.*)(\n)/\n/g; $WBlibline=~ s/(\s+)/ /g;
	@RHlibs=split(/ /,$RHlibline);
	@WBlibs=split(/ /,$WBlibline);

        # Print and tag library errors
	foreach $lib (@RHlibs){
	    $searchpat=$lib;
	    $searchpat =~ s/\+/\\\+/g;
	    $searchpat =~ s/\./\\\./g;
	    unless ($WBlibline =~ (/$searchpat/)){
		print "\n\tMissing link in $executable:\t$lib";$libproblems=1;
	    }
	}
	    
	foreach $lib (@WBlibs){
	    $searchpat=$lib;
	    $searchpat =~ s/\+/\\\+/g;
	    $searchpat =~ s/\./\\\./g;
	    unless ($RHlibline =~ (/$searchpat/)){
		print "\n\tExtra link in $executable:\t$lib";$libproblems=1;
	    }
	}
    }
    if ($fileproblems || $libproblems) { print "\n\n";} else {print "MATCH\n";} 
}

