luci-mod-admin-full: allow setting dns cachesize
[project/luci.git] / build / zoneinfo2lua.pl
1 #!/usr/bin/perl
2 # zoneinfo2lua.pl - Make Lua module from /usr/share/zoneinfo
3 # Execute from within root of Luci feed, usually feeds/luci
4 # $Id$
5
6 use strict;
7
8 my %TZ;
9
10 my $tzdin  = $ARGV[0] || "/usr/share/zoneinfo";
11 my $tzdout = $ARGV[1] || "./modules/luci-base/luasrc/sys/zoneinfo";
12
13 local $/ = "\012";
14 open( ZTAB, "< $tzdin/zone.tab" ) || die "open($tzdin/zone.tab): $!";
15
16 while( ! eof ZTAB ) {
17         chomp( my $line = readline ZTAB );
18         next if $line =~ /^#/ || $line =~ /^\s+$/;
19
20         my ( undef, undef, $zone, @comment ) = split /\s+/, $line;
21
22         printf STDERR "%-40s", $zone;
23
24         if( open ZONE, "< $tzdin/$zone" ) {
25                 seek ZONE, -2, 2;
26
27                 while( tell(ZONE) > 0 ) {
28                         read ZONE, my $char, 1;
29                         ( $char eq "\012" ) ? last : seek ZONE, -2, 1;
30                 }
31
32                 chomp( my $tz = readline ZONE );
33                 print STDERR ( $tz || "(no tzinfo found)" ), "\n";
34                 close ZONE;
35
36                 if( $tz ) {
37                         $zone =~ s/_/ /g;
38                         $TZ{$zone} = $tz;
39                 }
40         }
41         else
42         {
43                 print STDERR "open($tzdin/$zone): $!\n";
44         }
45 }
46
47 close ZTAB;
48
49 # Add Etc/GMT zones from manually as they are not in zone.tab
50 $TZ{"Etc/GMT"} = "GMT0";
51 $TZ{"Etc/GMT-1"} = "<+01>-1";
52 $TZ{"Etc/GMT-2"} = "<+02>-2";
53 $TZ{"Etc/GMT-3"} = "<+03>-3";
54 $TZ{"Etc/GMT-4"} = "<+04>-4";
55 $TZ{"Etc/GMT-5"} = "<+05>-5";
56 $TZ{"Etc/GMT-6"} = "<+06>-6";
57 $TZ{"Etc/GMT-7"} = "<+07>-7";
58 $TZ{"Etc/GMT-8"} = "<+08>-8";
59 $TZ{"Etc/GMT-9"} = "<+09>-9";
60 $TZ{"Etc/GMT-10"} = "<+10>-10";
61 $TZ{"Etc/GMT-11"} = "<+11>-11";
62 $TZ{"Etc/GMT-12"} = "<+12>-12";
63 $TZ{"Etc/GMT-13"} = "<+13>-13";
64 $TZ{"Etc/GMT-14"} = "<+14>-14";
65 $TZ{"Etc/GMT+1"} = "<-01>1";
66 $TZ{"Etc/GMT+2"} = "<-02>2";
67 $TZ{"Etc/GMT+3"} = "<-03>3";
68 $TZ{"Etc/GMT+4"} = "<-04>4";
69 $TZ{"Etc/GMT+5"} = "<-05>5";
70 $TZ{"Etc/GMT+6"} = "<-06>6";
71 $TZ{"Etc/GMT+7"} = "<-07>7";
72 $TZ{"Etc/GMT+8"} = "<-08>8";
73 $TZ{"Etc/GMT+9"} = "<-09>9";
74 $TZ{"Etc/GMT+10"} = "<-10>10";
75 $TZ{"Etc/GMT+11"} = "<-11>11";
76 $TZ{"Etc/GMT+12"} = "<-12>12";
77
78 open(O, "> $tzdout/tzdata.lua") || die "open($tzdout/tzdata.lua): $!\n";
79
80 print STDERR "Writing time zones to $tzdout/tzdata.lua ... ";
81 print O <<HEAD;
82 -- Licensed to the public under the Apache License 2.0.
83
84 module "luci.sys.zoneinfo.tzdata"
85
86 TZ = {
87 HEAD
88
89 foreach my $zone ( sort keys %TZ ) {
90         printf O "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
91 }
92
93 print O "}\n";
94 close O;
95
96 print STDERR "done\n";
97
98
99 open (O, "> $tzdout/tzoffset.lua") || die "open($tzdout/tzoffset.lua): $!\n";
100
101 print STDERR "Writing time offsets to $tzdout/tzoffset.lua ... ";
102 print O <<HEAD;
103 -- Licensed to the public under the Apache License 2.0.
104
105 module "luci.sys.zoneinfo.tzoffset"
106
107 OFFSET = {
108 HEAD
109
110 my %seen;
111 foreach my $tz ( sort keys %TZ ) {
112         my $zone = $TZ{$tz};
113
114         if( $zone =~ /^
115                 ([A-Z]+)
116                 (?:
117                         ( -? \d+ (?: : \d+ )? )
118                         (?:
119                                 ([A-Z]+)
120                                 ( -? \d+ (?: : \d+ )? )? 
121                         )?
122                 )?
123         \b /xo ) {
124                 my ( $offset, $s, $h, $m ) = ( 0, 1, 0, 0 );
125                 my ( $std, $soffset, $dst, $doffset ) = ( $1, $2, $3, $4 );
126
127                 next if $seen{$std}; # and ( !$dst or $seen{$dst} );
128
129                 if ( $soffset ) {
130                         ( $s, $h, $m ) = $soffset =~ /^(-)?(\d+)(?::(\d+))?$/;
131
132                         $s = $s ? 1 : -1;
133                         $h ||= 0;
134                         $m ||= 0;
135
136                         $offset  = $s * $h * 60 * 60;
137                         $offset += $s * $m * 60;
138
139                         printf O "\t%-5s = %6d,\t-- %s\n",
140                                 lc($std), $offset, $std;
141
142                         $seen{$std} = 1;
143
144                         if( $dst ) {
145                                 if( $doffset ) {
146                                         ( $s, $h, $m ) = $doffset =~ /^(-)?(\d+)(?::(\d+))?$/;
147
148                                         $s = $s ? 1 : -1;
149                                         $h ||= 0;
150                                         $m ||= 0;
151
152                                         $offset  = $s * $h * 60 * 60;
153                                         $offset += $s * $m * 60;
154                                 } else  {
155                                         $offset += 60 * 60;
156                                 }
157
158                                 printf O "\t%-5s = %6d,\t-- %s\n",
159                                         lc($dst), $offset, $dst;
160         
161                                 $seen{$dst} = 1;
162                         }
163                 }
164                 else {
165                         printf O "\t%-5s = %6d,\t-- %s\n",
166                                 lc($std), $offset, $std;
167
168                         $seen{$std} = 1;
169                 }
170
171         }
172 }
173
174 print O "}\n";
175 close O;
176
177 print STDERR "done\n";