#!/usr/bin/perl
# -w
#
# GaMaLib_diff - script which compares two archives of gamalib
#
# usage: GaMaLib_diff new_version  old_version
#
# made by Jan Pytel<pytel@gama.fsv.cvut.cz>
#
#
# The start of a script must be executed from the directory which contains
# archives of different versions of GaMaLib (in format tgz or tar.gz)
#
# $Id: gamalib_diff,v 1.1 2001/12/07 11:45:44 cepek Exp $


# From pytel@Panda.fsv.cvut.cz Tue Jul 13 19:37:33 1999
# Date: Mon, 12 Jul 1999 23:43:14 +0200 (CEST)
# From: Jan Pytel <pytel@Panda.fsv.cvut.cz>
# To: gamalib@Panda.fsv.cvut.cz
# Subject: [GaMaLib] Skript pro porovnani versi GaMa
#
# Ahoj,
#  tady je ten skriptik na porovnavani zmen v archivech gama jak chtel
#  Ales.  Skript se spousti z adresare kde se nachazi archivy
#  gama. Spousti se se dvema parametry, kde kazdy parametr =
#  cislo_verse. Napriklad:
#       ./Porovnej_ver_gama 0.0.9 0.0.8 > zmeny_9_8.zdiff
#
#  Skript vypisuje zmeny souboru mezi verzemi a navic informuje
#  uzivatele o souborech ktere byly smazany nebo naopak pridany do
#  novejsi verze.  Popis skriptu: Skript vytvori v adresari tmp
#  adresar "Cislo"gama, kde "Cislo" = PID (Napad AC). Pote v tomto
#  adresari vytvori dalsi dva adresare a provede rozbaleni a porovnani
#  (viz. skript). Nakonec vse smaze.  Tento skr. jsem moc netestoval,
#  neb mam doma pouze tri archivy gama. Pokud ho tedy nekdo otestuje
#  na vice archivech a na neco prijde (to je ale velmi nepravdepodobne
#  ;-)) necht se mi ozve.(Alesi omlouvam se ze mi to trvalo tak
#  dlouho, ale nebyl jsem doma.)
#
# Zdravim,
#     Honza P.

# _______________________________________________
# Gamalib maillist  -  Gamalib@GaMa.fsv.cvut.cz
# http://GaMa.fsv.cvut.cz/mailman/listinfo/gamalib


# 29.7. 1999 I added the conditions about the existence entered versions
# in directory and the controll of watching the same versions.

# 3.9. 1999 Now you are able to use direct the name of archive instead of
# using number of an archive. If the script recognizes that the file of
# an entered name exists, then extracts from its name the number of a
# version

# 20.3.2001 I corrected the error caused by different sturcture of archives
# of new gamalib versions.

# 23.3.2001 I completly rewrited most of this script code. I translated the
# script output messages from czech to english. I added some 'useless ;-)'
# comments

# GaMaLib_diff:
# ##################
#


if (not $ARGV[1])
    {
    print "*" x 75 , "\n";
    print " " x 31 , "GaMaLib_diff\n";
    print "*" x 75 , "\n";
    die "Attention, script must have two parameters: \n" .
          "       $0 new_version  old_version \n"
    }

#
# Constants
#
$program_name     = "gamalib-";
$extension_name   = ".tgz";
$gamalib_length   = length($program_name);
$extension_lenght = length($extension_name);


$ver1 = $ARGV[0];
$ver2 = $ARGV[1];

# Were the files direct entered? If they were, extract a version.
if(-e $ver1) { $ver1 = substr($ver1, $gamalib_length,
                         length($ver1) - ($gamalib_length +
$extension_lenght))
      }

if(-e $ver2) { $ver2 = substr($ver2, $gamalib_length,
                         length($ver2) - ($gamalib_length +
$extension_lenght))
      }

if ( $ver1 eq $ver2 )
                 { die "Attention, you compare the same versions ! \n"}
unless (-e $program_name . $ver1 . $extension_name)
                 { die "GaMaLib version $ver1 doesn't exist. \n"}
unless (-e $program_name . $ver2 . $extension_name)
                 { die "GaMaLib version $ver2 doesn't exist. \n"}

$path = "/tmp/" . $$ . "gama/";

$archive1 = $path . $ver1 . "/" . $program_name .
           $ver1 . "/" . $program_name . $ver1 .".md5sum";
$archive2 = $path . $ver2 . "/" . $program_name .
           $ver2 . "/" . $program_name . $ver2 .".md5sum";

mkdir $path, 999;
mkdir "$path$ver1", 999;
mkdir "$path$ver2", 999;

system("cp $program_name$ver1$extension_name $path$ver1;" .
       "cp $program_name$ver2$extension_name $path$ver2;" .
       "cd $path$ver1; tar xzf $program_name$ver1$extension_name;" .
       "cd $path$ver2; tar xzf $program_name$ver2$extension_name;");

open (ARCHIVE1, "$archive1") or die "I can't open the file $archive1 \n";
while ($line = <ARCHIVE1>) {
    chop($line); # I must remove lining, I don't know why
                 # I can not write directly split(" ", chop($line));
    ($md5sum1, $file1) = split("  ", $line);
    $s1{$file1} .= $md5sum1;
}

open (ARCHIVE2, "$archive2") or die "I can't open the file $archive2 \n";
while ($line = <ARCHIVE2>) {
    chop($line); # I must remove lining ('n')
    ($md5sum2, $file2) = split("  ", $line);
    $s2{$file2} .= $md5sum2;
}

foreach $file1 ( sort keys %s1) {
       $path2_change_ver =
            "gamalib-". $ver2 . substr($file1, index($file1, "/") );
       if (not $s2{$path2_change_ver})
           { print "\nThe file $file1 was added. \n";}
       elsif ($s1{$file1} cmp $s2{$path2_change_ver})
           { system("diff -c $path$ver1/$file1 $path$ver2/$path2_change_ver"); }
      }

foreach $file2 ( sort keys %s2) {
       $path1_change_ver =
            "gamalib-". $ver1 . substr($file2, index($file2, "/") );
       if (not $s1{$path1_change_ver})
           { print "\nThe file $file2 was deleted. \n";}
      }

system("rm -R $path");










