#! /usr/local/bin/perl

#  Packs the javascript files that are part of the 
#  image fonts package at fonts/HTML-CSS/TeX/png.
#  
#  Usage:  ./packMJfonts

$SRC = "../../mathjax/fonts/HTML-CSS/TeX/png";

sub packDir {
  my ($src,$dst) = @_;
  opendir(SRC,$src);
  my @files = grep(/^[^.]/,readdir(SRC));
  closedir(SRC);
  foreach my $file (@files) {
    if (-d "$src/$file") {
      next if $file =~ m/^\d+$/;
      packDir("$src/$file",$dst.($file eq "unpacked" ? "" :"/$file"));
    } elsif ($file =~ m/\.js$/) {
      packFile($src,$dst,$file);
    }
  }
}

sub packFile {
  my ($src,$dst,$file) = @_;
  $src .= "/$file"; $dst .= "/$file";
  my ($stime,$ssize) = (stat($src))[9,7];
  my $dtime = (stat($dst))[9];
  return if $stime <= $dtime || $ssize <= 1024;
  system("./packMJfile '$src' '$dst'");
  my $dsize = (stat($dst))[7];
  print "Size: $dsize,  Original: $ssize [",$ssize-$dsize," or ",
    sprintf("%.1f",100*($ssize-$dsize)/$ssize),"%]\n";
}

packDir($SRC,$SRC);

1;
