#! /usr/local/bin/perl

#  Compresses a MathJax file and reports the new size and savings.
#  You need to have yuicompressor in order for this to work
#  
#  Usage:  ./packMJfile src-file dest-file

$src = shift; $dst = shift;

$YUI = "yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar";

if ($src =~ m!/config/(local/local|MathJax)\.js$!) {
  print "============================================\n";
  print "Copying: $src\n";
  system("cp '$src' '$dst'");
  exit;
}

print "============================================\n";
print "Compressing: $src\n";
print "--------------------------------------------\n";
print "Phase I...";
system("java -jar '$YUI' -v -o tmp.js '$src' 2>&1 | ./yuiFilter");

print "--------------------------------------------\n";
print "Phase II...\n";
system("./jsCompress.pl tmp.js");

print "--------------------------------------------\n";
open(MJX,"<","tmp.data");
@lines = <MJX>;
close(MJX);
$compressed = join("",@lines);

$template = "template.js";
$template = "template-MathJax.js" if $dst =~ m!/MathJax\.js$!;

open(MJX,"<",$template);
@lines = <MJX>;
close(MJX);
$lines = join("",@lines);

$dstname = $dst; $dstname =~ s!.*/MathJax!/MathJax!;
$lines =~ s/%%%NAME%%%/$dstname/;
$lines =~ s/%%%DATA%%%/$compressed/;

open(MJX,">",$dst);
print MJX $lines;
close(MJX);

unlink "tmp.js";
unlink "tmp.data";

1;

