#!/usr/bin/perl

use strict;
use XML::DOM;

my $index = 'index_uncompiled.html';
my $output = 'index.html';
my $jsOutput = 'lib/GeoMOOSE.js';

my $parse = XML::DOM::Parser->new();

open fh, $index;
open output, '>'.$output;
open js, '>'.$jsOutput;

my $date = `date`;
chomp($date);

print js qq(/*
 * Compiled $date with the GeoMOOSE compiler.
 */
);
while(<fh>) {
	my $line = $_;
	if($line =~ m/\<script/i and $line =~ m/src=/i and not($line =~ /\-\-/)) {
		my $scriptNode = $parse->parse($line);
		my $src= $scriptNode->getFirstChild()->getAttribute('src');
		if($src !~ m/Configuration\.js/i) {
			print 'Adding... '.$src;
			open jsfile, $src;
			while(<jsfile>) {
				print js $_;
			}
			close jsfile;
			print " Done.\n";
		} else {
			print output $line;
		}
	} else {
		print output $line;
	}
}

print qq(<script type="text/javascript" src="$jsOutput"></script>\n);
close js;
close output;
close fh;
