#!/usr/bin/perl

@a = @ARGV;

open O1, ">Tz.mc";
$pre1 = "// Auto-generated by tz.pl, think carefully before modifying
class Tz {
const TzData = [\n";

open O2, ">settings.xml";
$pre2 = "";

open I, "<$a[0]";
while (<I>) {
	chop;
	$data = $_;

	$name = $data;
	$data =~ s/	.*//;
	$name =~ s/.*	//;
	$sign = substr($data, 0, 1);
	$d = $data;
	$d =~ s/.//;
	@token = split(';|:', $d);
	$hr = 0 + $token[0];
	$min = 0 + $token[1];
	$off = ($hr * 60) + $min;
	$off = -$off			if $sign eq "-";
	if ($token[3] ne "") {
		$data = $off . ";" . $token[2] . ";" . $token[3] . ";" . $token[4];
	} else {
		$data = $off . ";" . $token[2] . ";;-1";
	}

	print O1 "$pre1  \"$name;$data\"";
	$pre1 = ",\n";

	print O2 "$pre2            <listEntry value=\"$max\">$name</listEntry>";
	$pre2 = "\n";
}
close I;

print O2 "\n";
close O2;

print O1 "\n";
print O1 <<END;
];
public function initialize() {}
public function get(idx, dst) {
var i;
var data = TzData[idx];
i = data.find(";");
var name = data.substring(0, i);
data = data.substring(i, null);
i = data.find(";");
var off = data.substring(0, i).toNumber();
data = data.substring(i, null);
i = data.find(";");
var st_ab = data.substring(0, i);
data = data.substring(i, null);
i = data.find(";");
var dst_ab = data.substring(0, i);
data = data.substring(i, null);
i = data.find(";");
var auto = data.substring(0, i).toNumber();
if (dst > 0) {
    auto = (dst == 1) ? -2 : -3; }
return [ off, st_ab, dst_ab, auto, name ];
}
}
END
close O3;
