#!/usr/bin/perl

#./compare.pl <opzione> [file1 file2]

$option = shift || die "At least 1 option i required $!";
$filename1 = shift || die "Insert first file to compare $!";
$filename2 = shift || die "Insert second file to compare $!";
die "Too much parameters in input $!" if ($#ARGV >= 0);
die "Option not known $!" if ($option ne "-c" && $option ne "-d" && $option ne "-s");

#opzione -c --> comando comm i file devono essere ordinati
if ($option eq "-c") {

    openFiles();

    @file1 = sort @file1;
    @file2 = sort @file2;
    
    foreach $index (0..$min-1) {
        chomp $file1[$index];
        chomp $file2[$index];
        print "---          ---         $file1[$index]\n" if ($file1[$index] eq $file2[$index]);
        print "$file1[$index]           $file2[$index]          ---\n" if ($file1[$index] ne $file2[$index]);
    }

    foreach $index ($min..$max-1) {
        if ($file1Bigger == 1) {
            chomp $file1[$index];
            print "$file1[$index]           ---          ---\n";
        }
        else {
            chomp $file2[$index];
            print "$file2[$index]           ---          ---\n";
        }
    }
}
elsif ($option eq "-d") {

    openFiles();

    foreach $index (0..$min-1) {
        chomp $file1[$index];
        chomp $file2[$index];

		  $lineNumber = $index+1; # Per far partire la numerazione da 1 invece che da 0
        if ("$file1[$index]" ne "$file2[$index]") {
            print "Differences in line $lineNumber\n $file1[$index]\n----------------\n$file2[$index]\n\n";
        }
    }

    foreach $index ($min..$max-1) {
	     $lineNumber = $index+1; # Per far partire la numerazione da 1 invece che da 0
        if ($file1Bigger == 1) {
            chomp $file1[$index];
            print "Differences in line $lineNumber\n $file1[$index]\n----------------\n<MISSING>\n\n";
        }
        else {
            chomp $file2[$index];
            print "Differences in line $lineNumber\n <MISSING>\n----------------\n$file2[$index]\n\n";
        }

    }

}
elsif ($option eq "-s") {
    openFiles();

    
    foreach (@file1) {
        chomp $_;
        @splitted = split(" ", $_);
        foreach $s (@splitted) {
            chomp $s;
            $hash{$s} = 0;
       }
    }

    @terms = keys %hash;
    foreach (@terms) {
        #print "CHIAVE: <$_>\n";
        foreach $line (@file2) {
            $hash{$_} = $hash{$_} + 1 if ($line =~ m/\b$_\b/);
        }
    }

    foreach $key ( sort{($hash{$b} <=> $hash{$a}) || ($a cmp $b)} keys %hash) {
        print "$key --> $hash{$key}\n";
    }
}


sub openFiles {

    open($f1, "<", $filename1) || die "Cannot open filename1 $filename1 $!";
    @file1 = <$f1>;
    close $f1 || die "Cannot close filename1 $filename1 $!";
    
    open($f2, "<", $filename2) || die "Cannot open filename2 $filename2 $!";
    @file2 = <$f2>;
    close $f2 || die "Cannot close filename2 $filename2 $!";
    
    $min = $sizeFile1 = scalar @file1;
    $max = $sizeFile2 = scalar @file2;
    $file1Bigger = 0;
    
    if ($sizeFile1 > $sizeFile2) {
        $min = $sizeFile2;
        $max = $sizeFile1;
        $file1Bigger = 1;
    }
}
