<?php
//error_reporting(E_ALL);
ini_set('display_errors', '0');
require '../jsmin.php';

$scripts = array('jquery-1.6.2.min.js', 'jquery-ui-1.8.15.custom.min.js', 'jquery.ui.map.full.min.js', 'jquery.timers.js', 'jquery.Storage.js', 'urlEncode.js', 'jquery.sparql.js', 'Three.js', 'Detector.js', 'RequestAnimationFrame.js', 'LG.js', 'FPC.js', 'wp_languages.js', '../dbp_onto.js', 'ColorJizz_js_0.2.js', 'sha1.js' ); /// ,  'jsw.js');
$cachedir = "cache";
$fmt = "fmtime_list.txt";
$delimiter1 = "###\n";

$delimiter2 = ":::";

$fh = fopen($fmt, "rb");
$contents = fread($fh, filesize($fmt));
fclose($fh);
$pairs = explode($delimiter1, $contents);
foreach ($pairs as $pair) {
	$val = explode($delimiter2, $pair);
	if ($val[1] != "") {
		$mtime[$val[0]] = $val[1];

	}
}
// print_r($mtime);

header('Content-Type: application/javascript');
ob_start('ob_gzhandler');
$fh = fopen($fmt, 'wb') or die("can't open file");
$first = true;
foreach ($scripts as $script) {

	if (file_exists($cachedir . "/" . $script . "_" . filemtime($script))) {
		echo file_get_contents($cachedir . "/" . $script . "_" . filemtime($script));
	} else {
		$cmp = JSMin::minify(file_get_contents($script));
		//$cmp = file_get_contents($script);
		$fh2 = fopen($cachedir . "/" . $script . "_" . filemtime($script), 'wb') or die("can't open file");
		fwrite($fh2, $cmp);
		fclose($fh2);
		echo $cmp;

	}
	if ($first) {
		$first = false;
		$delimiter = "";
	} else {
		$delimiter = $delimiter1;
	}
	$stringData = $delimiter . $script . $delimiter2 . filemtime($script);
	fwrite($fh, $stringData);

}
fclose($fh);

ob_end_flush();
?>
