#!/usr/bin/perl -w
#
# This proggie 'll compare 2 files with list of packages (or any other strings)
# & produce 2 other files (file.$ext), that will contain only those strings
# that are unical for these files: dublicates are removed from both resulting 
# files.
#
# Simplest compare script. (c) Light Olli,olli@digger.org.ru
#

###### proggie starts:
use Getopt::Long;   # command line processing module.
use File::Basename; # path splitting module.
# don't remove this
local ($help=$files=$resdir=undef,@list=(),$ext=".unique");
local ($name=basename($0));

# check cmdline opts:
GetOptions
('help|?!',\$help,
'f=s',\$files,
'resdir=s',\$resdir,
'ext=s',\$ext) || die "\nTry `$name --help`\n\n";

# do corresponding to specified options work.

if ($help)
{print
"\n$name: Simplest comparing script. Shows unique lines for two given lists.\n",
"(c) Light Olli:\tolli\@digger.org.ru,license is current GNU one.\n\n",
"Usage:\n",
"   $name <-files <file,file> [[-resdir DIR] [-ext ext]]\n",
"Required:\n",
"   -f path1,path2\tCompare two files.\n",
"\t\t\tBy default this will create 2 files:\n",
"\t\t\tpath1$ext & path2$ext.\n",
"\t\t\tThese 'll contain lines that are unique to both files -\n",
"\t\t\tthose that are found only in the corresponding file.\n",
"Optional:\n",
"   -resdir DIRECTORY\tPut resulting '$ext' files in DIRECTORY .\n",
"   -ext EXTENTION\tGive resulting files extention EXTENTION.\n\n",
"   -h|--help|-?\t\tGive this help screen & exit.\n\n";
exit;
}
unless ($files)
{print "\nAbort:'-f' is required. Read help: '$0 -h'!\n\n"; exit}
# real work goes on:
@list=split(/,/,$files);
$filea=$list[0];$fileb=$list[1];
open (FD1,"<$filea") || die "can't open $filea for reading: $!";
open (FD2,"<$fileb") || die "can't open $fileb for reading: $!";
while (<FD1>)
{chomp; chomp;
 push (@array1,$_);
}
while (<FD2>)
{chomp; chomp;
 push (@array2,$_);
}

if ($resdir)
{$resfilea = "$resdir/" . basename($filea) . $ext ;
 $resfileb = "$resdir/" . basename($fileb) . $ext ;
}
else
 {$resfilea = $filea . $ext ; 
  $resfileb = $fileb . $ext ;
 }

open (FD3,">$resfilea") || die "can't open $resfilea for writing: $!";
open (FD4,">$resfileb") || die "can't open $resfileb for writing: $!";

foreach $item1 (@array1)
 {foreach $item2 (@array2)
  {if ($item1 eq $item2)
     {$skip=1;
      push @skiplist,$item2;
	 }
  }
 unless ($skip) {print FD3 "$item1\n"}
 $skip=undef;
}

foreach $item2 (@array2)
 {foreach $item1 (@skiplist)
   {if ($item1 eq $item2)  {$skip=1;}}
  unless($skip) {print FD4 "$item2\n"}
  $skip=undef;
 }

close FD1;
close FD2;
close FD3;
close FD4;
