use utf8;
use warnings;
use strict;

my $ANT_HOME = $ENV{'ANT_HOME'};

# "${java.class.path}"
my $java_class_path = $ARGV[0];

my $JAVA_HOME;

my @a = split /;/, $java_class_path;
   @a = grep /[\/\\]lib[\/\\]tools\.jar$/, @a;
foreach my $s (@a) {
	$s =~ s/[\/\\]lib[\/\\]tools\.jar$//;
	if (-f "$s/bin/javac.exe") {
		$JAVA_HOME = $s;
		last;
	}
}

die 'ERROR' if not $ANT_HOME and not $JAVA_HOME;

my $outputfile = 'builder#.bat';
my $inputfile;
  if (-f $outputfile) {$inputfile = $outputfile}
  else                {$inputfile = 'builder.bat'}

open IN, '<:encoding(cp932)', $inputfile;
read IN, my $bat, -s IN;
close IN;

my $bak = $bat;
$bat =~ s/(rem|) *(SET ANT_HOME=).*/$2$ANT_HOME/ if $ANT_HOME;
$bat =~ s/(rem|) *(SET JAVA_HOME=).*/$2$JAVA_HOME/ if $JAVA_HOME;

if ($bat ne $bak) {
	print "SET ANT_HOME=$ANT_HOME\n" if $ANT_HOME;
	print "SET JAVA_HOME=$JAVA_HOME\n" if $JAVA_HOME;
	open OUT, '>:encoding(cp932)', $outputfile;
	print OUT $bat;
	close OUT;
	print "$outputfile - configurate.";
}
else {
	print "$outputfile - already done.";
}
