#!/usr/bin/perl

sub getfield {
	my $field=shift;
	if (! exists $ENV{$field}) {
		die "$field not in environment!";
	}
	return $ENV{$field};
}

while (<>) {
	# Replace the blah in '"blah"; # MOOROOT SET AT BUILD TIME' with
	# the actual value, from mooix.conf, by way of the environment.
	s/["'].*?["']\s*;\s*#\s*(\w+)\s*SET AT BUILD TIME/"'".getfield($1)."';"/e;
	
	if ($ENV{PERL_STDOUT_FLUSH_HACK} eq 'yes') {
		s/#PERL_STDOUT_FLUSH_HACK//;
	}
	
	# Remove comments and pod, which will aid load time.
	# Yes, this will remove s### stuff, so don't do that! If you must
	# include a literal comment in the code, prefix it with a \
	$cutting=1 if /^=/;
	$cutting="" if /^=cut/;
	next if $cutting || /^(=|\s*#)/ || $_ eq "\n";
	next if /^\s*#/;
	s/\s+#.*$//;
	
	print $_ if length $_;
}
