build: get version from /etc/openwrt_release if applicable
[project/luci.git] / build / zoneinfo2lua.pl
1 #!/usr/bin/perl
2 # zoneinfo2lua.pl - Make Lua module from /usr/share/zoneinfo
3 # Execute from within /usr/share/zoneinfo
4 # $Id$
5
6 use strict;
7
8 my %TZ;
9
10 local $/ = "\012";
11 open( ZTAB, "< ./zone.tab" ) || die "Unable to open zone.tab: $!";
12
13 while( ! eof ZTAB ) {
14         chomp( my $line = readline ZTAB );
15         next if $line =~ /^#/ || $line =~ /^\s+$/;
16
17         my ( undef, undef, $zone, @comment ) = split /\s+/, $line;
18
19         printf STDERR "%-40s", $zone;
20
21         if( open ZONE, "< ./$zone" ) {
22                 seek ZONE, -2, 2;
23
24                 while( tell(ZONE) > 0 ) {
25                         read ZONE, my $char, 1;
26                         ( $char eq "\012" ) ? last : seek ZONE, -2, 1;
27                 }
28
29                 chomp( my $tz = readline ZONE );
30                 print STDERR ( $tz || "(no tzinfo found)" ), "\n";
31                 close ZONE;
32
33                 if( $tz ) {
34                         $zone =~ s/_/ /g;
35                         $TZ{$zone} = $tz;
36                 }
37         }
38         else
39         {
40                 print STDERR "Unable to open $zone: $!\n";
41         }
42 }
43
44 close ZTAB;
45
46
47 print <<HEAD;
48 --[[
49 LuCI - Autogenerated Zoneinfo Module
50
51 Licensed under the Apache License, Version 2.0 (the "License");
52 you may not use this file except in compliance with the License.
53 You may obtain a copy of the License at
54
55         http://www.apache.org/licenses/LICENSE-2.0
56
57 ]]--
58
59 module "luci.sys.zoneinfo"
60
61 TZ = {
62 HEAD
63
64 foreach my $zone ( sort keys %TZ ) {
65         printf "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
66 }
67
68 print <<HEAD;
69 }
70
71 OFFSET = {
72 HEAD
73
74 my %seen;
75 foreach my $tz ( sort keys %TZ ) {
76         my $zone = $TZ{$tz};
77
78         if( $zone =~ /^
79                 ([A-Z]+)
80                 (?:
81                         ( -? \d+ (?: : \d+ )? )
82                         (?:
83                                 ([A-Z]+)
84                                 ( -? \d+ (?: : \d+ )? )? 
85                         )?
86                 )?
87         \b /xo ) {
88                 my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
89                 my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );
90
91                 next if $seen{$std}; # and ( !$dst or $seen{$dst} );
92
93                 if ( $soffset ) {
94                         ( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;
95
96                         $s = $s ? 1 : -1;
97                         $h ||= 0;
98                         $m ||= 0;
99
100                         $offset  = $s * $h * 60 * 60;
101                         $offset += $s * $m * 60;
102
103                         printf("\t%-5s = %6d,\t-- %s\n",
104                                 lc($std), $offset, $std);
105
106                         $seen{$std} = 1;
107
108                         if( $dst ) {
109                                 if( $doffset ) {
110                                         ( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;
111
112                                         $s = $s ? 1 : -1;
113                                         $h ||= 0;
114                                         $m ||= 0;
115
116                                         $offset  = $s * $h * 60 * 60;
117                                         $offset += $s * $m * 60;
118                                 } else  {
119                                         $offset += 60 * 60;
120                                 }
121
122                                 printf("\t%-5s = %6d,\t-- %s\n",
123                                         lc($dst), $offset, $dst);
124         
125                                 $seen{$dst} = 1;
126                         }
127                 }
128                 else {
129                         printf("\t%-5s = %6d,\t-- %s\n",
130                                 lc($std), $offset, $std);
131
132                         $seen{$std} = 1;
133                 }
134
135         }
136 }
137
138 print "}\n";