#!/usr/bin/perl
# zoneinfo2lua.pl - Make Lua module from /usr/share/zoneinfo
# Execute from within /usr/share/zoneinfo
# $Id: zoneinfo2lua.pl 3860 2008-12-06 03:18:14Z jow $

use strict;

my %TZ;

local $/ = "\012";
open( ZTAB, "< ./zone.tab" ) || die "Unable to open zone.tab: $!";

while( ! eof ZTAB ) {
	chomp( my $line = readline ZTAB );
	next if $line =~ /^#/ || $line =~ /^\s+$/;

	my ( undef, undef, $zone, @comment ) = split /\s+/, $line;

	printf STDERR "%-40s", $zone;

	if( open ZONE, "< ./$zone" ) {
		seek ZONE, -2, 2;

		while( tell(ZONE) > 0 ) {
			read ZONE, my $char, 1;
			( $char eq "\012" ) ? last : seek ZONE, -2, 1;
		}

		chomp( my $tz = readline ZONE );
		print STDERR ( $tz || "(no tzinfo found)" ), "\n";
		close ZONE;

		if( $tz ) {
			$zone =~ s/_/ /g;
			$TZ{$zone} = $tz;
		}
	}
	else
	{
		print STDERR "Unable to open $zone: $!\n";
	}
}

close ZTAB;


print <<HEAD;
--[[
LuCI - Autogenerated Zoneinfo Module

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

]]--

module "luci.sys.zoneinfo"

TZ = {
HEAD

foreach my $zone ( sort keys %TZ ) {
	printf "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
}

print <<HEAD;
}

OFFSET = {
HEAD

my %seen;
foreach my $tz ( sort keys %TZ ) {
	my $zone = $TZ{$tz};

	if( $zone =~ /^
		([A-Z]+)
		(?:
			( -? \d+ (?: : \d+ )? )
			(?:
				([A-Z]+)
				( -? \d+ (?: : \d+ )? )? 
			)?
		)?
	\b /xo ) {
		my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
		my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );

		next if $seen{$std}; # and ( !$dst or $seen{$dst} );

		if ( $soffset ) {
			( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;

			$s = $s ? 1 : -1;
			$h ||= 0;
			$m ||= 0;

			$offset  = $s * $h * 60 * 60;
			$offset += $s * $m * 60;

			printf("\t%-5s = %6d,\t-- %s\n",
				lc($std), $offset, $std);

			$seen{$std} = 1;

			if( $dst ) {
				if( $doffset ) {
					( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;

					$s = $s ? 1 : -1;
					$h ||= 0;
					$m ||= 0;

					$offset  = $s * $h * 60 * 60;
					$offset += $s * $m * 60;
				} else  {
					$offset += 60 * 60;
				}

				printf("\t%-5s = %6d,\t-- %s\n",
					lc($dst), $offset, $dst);
	
				$seen{$dst} = 1;
			}
		}
		else {
			printf("\t%-5s = %6d,\t-- %s\n",
				lc($std), $offset, $std);

			$seen{$std} = 1;
		}

	}
}

print "}\n";
