#! /usr/local/bin/perl

#  Compresses the MathJax javascript files that have changed
#  since the last compression was performed.
#  Note that the mathjax/font/HTML-CSS/TeX/png font data files
#  are handled separately by packMJfonts.
#  Note also that you need a copy of yuicompressor to be
#  in this directory.
#  
#  Usage:  ./packMJ

$SRC = "../../mathjax/unpacked";
$DST = $SRC; $DST =~ s!/unpacked$!!;

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 "$src/$file" eq "$SRC/fonts";
      next if "$src/$file" eq "$SRC/docs";
      next if "$src/$file" eq "$SRC/unpacked";
      if (! -e "$dst/$file") {mkdir "$dst/$file"}
      packDir("$src/$file","$dst/$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  was: $ssize [saved: ",$ssize-$dsize," or ",
    sprintf("%.1f",100*($ssize-$dsize)/$ssize),"%]\n";
}

packDir($SRC,$DST);

1;
