#!/usr/bin/perl

if($0=~m/mkpackages/i) {
  $pack=1;
  $desc="uncompress size";
  $DSC="USIZE   ";
}
else {
  $desc="md5";
  $DSC="MD5                             ";
}

$dbfile=shift;

$|=1;

if(length($dbfile)==0) {
  print STDERR "Usage: $0 <dbfile>\n";
  exit(1);
}

sub basename {
  my $name=shift;
  my ($i,@e);
  $i=rindex($name,"/");
  if($i>=0) {
    $name=substr($name,$i+1);
  }
  return($name);
}

sub dirname {
  my $name=shift;
  my ($i);
  $i=rindex($name,"/");
  if($i>=0) {
    return(substr($name,0,$i));
  }
  else {
    return("");
  }
}

sub removeext {
  my $name=shift;
  @e=split("\\.",$name);
  pop(@e);
  $name=join(".",@e);
  return($name);
}

$total=0;
if(open(F,$dbfile)) {
  while(<F>) {
    chop;
    ($size,$time,$md5,$file)=split("\t");
    $md5{$file}=join("\t",$size,$time,$md5);
    $total++;
  }
  close(F);
}

$count=0;
while($file=<STDIN>) {
  chop($file);
  if(($count % 100)==0) {
    if($total==0 || $count>$total) { $p=$count; }
    else {
      $p=sprintf("%.1f%%",$count*100/$total);
    }
    if(length($file)>50) {
      $filecut=substr($file,0,50)."[]...";
    }
    else {
      $filecut=$file . "..." . (" " x (52-length($file)));
    }
    print STDERR "[$p] $filecut\r";
  }
  @s=stat($file);
  undef($md5);
  if(defined($cache=$md5{$file})) {
    ($size,$time,$md5)=split("\t",$cache);
    if($size!=$s[7] || $time!=$s[9]) {
      #print STDERR "SIZE ($size $s[7]) or TIME ($time $s[9]) not match $file\n";
      undef($md5);
    }
  }
  $size=$s[7];
  $totsize+=$size;
  $time=$s[9];
  $md5[$count]=join("\t",$size,$time,$md5);
  $file[$count]=$file;
  $file{$file}=$count;
  if(length($md5)==0) {
    push(@tomd5,$file);
  }
  elsif($pack) {
    $totusize+=$md5;
  }
  $count++;
}
$tomd5=$#tomd5+1;
$total=$tomd5;

print STDERR "Found ".($tomd5)." files which need $desc to be computed...                 \n";

if($tomd5>0) {
  $TMP=$ENV{TMP};
  if(length($TMP)==0) { $TMP="/tmp"; }
  if($pack) {
    # For each file compute uncompress size
    $count=0;
    foreach $file (@tomd5) { 
      (@s)=split("\\.",$file);
      $packageext=$s[-1];
      if($packageext eq "tgz") {
        $decomp="gzip -d -c";
      }
      elsif($packageext eq "tbz") {
        $decomp="bzip2 -d -c";
      }
      elsif($packageext eq "tlz") {
        $decomp="lzma -d -c";
      }
      elsif($packageext eq "txz") {
        $decomp="xz -d -c";
      }
      if(($count % 100)==0 || 1) {
	if($total==0 || $count>$total) { $p=$count; }
	else {
	  $p=sprintf("%.1f%%",$count*100/$total);
	}
	if(length($file)>50) {
	  $filecut=substr($file,0,50)."[]...";
	}
	else {
	  $filecut=$file . "..." . (" " x (52-length($file)));
	}
	print STDERR "[$p] $filecut\r";
      }
      $md5=`cat $file | $decomp | wc -c`+0;
      if(!defined($i=$file{$file})) {
	print STDERR "** ERROR: $file not found in hash (should not happen)\n";
      }
      else {
	($size,$time)=split("\t",$md5[$i]);
	$md5[$i]=join("\t",$size,$time,$md5);
	$totusize+=$md5;
      }
      $count++;
    }
  }
  else {
    $tmp="$TMP/md5.$$";

    open(M,">$tmp");
    foreach(@tomd5) { print M "$_\n"; }
    close(M);

    $count=0;
    open(M,"cat $tmp|xargs md5sum|");
    while(<M>) {
      chop;
      ($md5,$file)=m/(\S+)\s+(.*)/;
      if(($count % 100)==0 || 1) {
	if($total==0 || $count>$total) { $p=$count; }
	else {
	  $p=sprintf("%.1f%%",$count*100/$total);
	}
	if(length($file)>50) {
	  $filecut=substr($file,0,50)."[]...";
	}
	else {
	  $filecut=$file . "..." . (" " x (52-length($file)));
	}
	print STDERR "[$p] $filecut\r";
      }
      if(!defined($i=$file{$file})) {
	print STDERR "** ERROR: $file not found in hash (should not happen)\n";
      }
      else {
	($size,$time)=split("\t",$md5[$i]);
	$md5[$i]=join("\t",$size,$time,$md5);
      }
      $count++;
    }
    close(M);
    unlink("$tmp");
  }
}

if($pack) 
{
  # Print header
  $ENV{TZ}="/usr/share/zoneinfo/UTC";
  $ENV{LANG}="C";
  $date=`date`;
  chomp($date);
  print "\n";
  print "PACKAGES.TXT;  $date\n";
  print "\n";
  print "This file provides details on the Slackware packages found\n";
  ($dir)=($file[0]=~m|/(\S+?)/|);
  if(length($dir)>0) {
    print "in the ./$dir/ directory.\n"
  }
  else {
    print "in this directory.\n"
  }
  print "\n";
  $CTOTAL=int($totsize/1024/1024+0.5);
  $UTOTAL=int($totusize/1024/1024+0.5);
  print "Total size of all packages (compressed):  $CTOTAL MB\n";
  print "Total size of all packages (uncompressed):  $UTOTAL MB\n";
  print "\n";
  print "\n";
}

open(F,">$dbfile");
print F "SIZE     \tTIME      \t$DSC\t--FILENAME--\n";
for($i=0;$i<=$#md5;$i++) {
  ($size,$time,$md5)=split("\t",$md5[$i]);
  $file=$file[$i];
  $md5c=$md5;
  if($pack) {
    $md5c=$md5.(" " x (9-length($md5)));
  }
  print F "".$size.(" " x (9-length($size)))."\t$time\t$md5c\t$file\n";
  if($pack) {
    $size=int($size/1024+0.5);
    $md5=int($md5/1024+0.5);
    print "PACKAGE NAME:  ".basename($file)."\n";
    print "PACKAGE LOCATION  ".dirname($file)."\n";
    print "PACKAGE SIZE (compressed):  $size K\n";
    print "PACKAGE SIZE (uncompressed):  $md5 K\n";
    print "PACKAGE DESCRIPTION:\n";
    if(open(D,removeext($file).".txt")) {
      $last="";
      while(<D>) {
        if($_ ne $last) {
	  print;
	}
	$last=$_;
      }
      close(D);
    }
    print "\n";
  }
  else {
    print "$md5  $file\n";
  }
}
close(F);
print STDERR "$i ${desc}[s] written.".(" "x50)."\n";
