#!/usr/bin/perl

$count=0;
$limit=0;

# process the command line arguments.
$output = "";
$input = "";
$outputFile = "MD5SUMS";
$inputDir = ".";

for($i = 0; $i <= $#ARGV; $i++)
{
   if($output)
   {
      $outputFile = $ARGV[$i];
      $output = "";
      next;
   }

   if($input)
   {
      $inputDir = $ARGV[$i];
      $inputDir .= "/" unless $inputDir =~ /\/$/;
      $input = "";
      next;
   }

   if($ARGV[$i] eq "-o")
   {
      $output = "true";
   }
   elsif($ARGV[$i] eq "-d")
   {
      $input = "true";
   }
   else
   {
      print "usage: $0 [-o output_file] [-d input_directory]\n";
      exit(0);
   }
}

open(ofp,">$outputFile");
open(lfp,"find $inputDir -type f |");
while(<lfp>) {
    chop;
    $filename=$_;
    print "Processing [$filename]...\n";
    $count++;
    open(ifp,"md5sum $filename |");
    while(<ifp>) {
	chop;
	print ofp "$_\n";
    }
    close ifp;
    if (($limit > 0) && ($count == $limit)) {
	close lfp;
	close ofp;
	die "Finished at $limit\n";
    }
}
close lfp;
close ofp;
print "\nFiles: $count\n";
