Merge pull request #1818 from dibdot/lxc_fix
[project/luci.git] / build / zoneinfo2lua.pl
index 2e24c95..1a0bee1 100755 (executable)
@@ -1,17 +1,17 @@
 #!/usr/bin/perl
 # zoneinfo2lua.pl - Make Lua module from /usr/share/zoneinfo
-# Execute from within /usr/share/zoneinfo
+# Execute from within root of Luci feed, usually feeds/luci
 # $Id$
 
 use strict;
 
 my %TZ;
-my $tzdir = $ARGV[0] || "/usr/share/zoneinfo";
 
-chdir($tzdir) || die "chdir($tzdir): $!\n";
+my $tzdin  = $ARGV[0] || "/usr/share/zoneinfo";
+my $tzdout = $ARGV[1] || "./modules/luci-base/luasrc/sys/zoneinfo";
 
 local $/ = "\012";
-open( ZTAB, "< ./zone.tab" ) || die "Unable to open zone.tab: $!";
+open( ZTAB, "< $tzdin/zone.tab" ) || die "open($tzdin/zone.tab): $!";
 
 while( ! eof ZTAB ) {
        chomp( my $line = readline ZTAB );
@@ -21,7 +21,7 @@ while( ! eof ZTAB ) {
 
        printf STDERR "%-40s", $zone;
 
-       if( open ZONE, "< ./$zone" ) {
+       if( open ZONE, "< $tzdin/$zone" ) {
                seek ZONE, -2, 2;
 
                while( tell(ZONE) > 0 ) {
@@ -40,36 +40,69 @@ while( ! eof ZTAB ) {
        }
        else
        {
-               print STDERR "Unable to open $zone: $!\n";
+               print STDERR "open($tzdin/$zone): $!\n";
        }
 }
 
 close ZTAB;
 
+# Add Etc/GMT zones from manually as they are not in zone.tab
+$TZ{"Etc/GMT"} = "GMT0";
+$TZ{"Etc/GMT-1"} = "<+01>-1";
+$TZ{"Etc/GMT-2"} = "<+02>-2";
+$TZ{"Etc/GMT-3"} = "<+03>-3";
+$TZ{"Etc/GMT-4"} = "<+04>-4";
+$TZ{"Etc/GMT-5"} = "<+05>-5";
+$TZ{"Etc/GMT-6"} = "<+06>-6";
+$TZ{"Etc/GMT-7"} = "<+07>-7";
+$TZ{"Etc/GMT-8"} = "<+08>-8";
+$TZ{"Etc/GMT-9"} = "<+09>-9";
+$TZ{"Etc/GMT-10"} = "<+10>-10";
+$TZ{"Etc/GMT-11"} = "<+11>-11";
+$TZ{"Etc/GMT-12"} = "<+12>-12";
+$TZ{"Etc/GMT-13"} = "<+13>-13";
+$TZ{"Etc/GMT-14"} = "<+14>-14";
+$TZ{"Etc/GMT+1"} = "<-01>1";
+$TZ{"Etc/GMT+2"} = "<-02>2";
+$TZ{"Etc/GMT+3"} = "<-03>3";
+$TZ{"Etc/GMT+4"} = "<-04>4";
+$TZ{"Etc/GMT+5"} = "<-05>5";
+$TZ{"Etc/GMT+6"} = "<-06>6";
+$TZ{"Etc/GMT+7"} = "<-07>7";
+$TZ{"Etc/GMT+8"} = "<-08>8";
+$TZ{"Etc/GMT+9"} = "<-09>9";
+$TZ{"Etc/GMT+10"} = "<-10>10";
+$TZ{"Etc/GMT+11"} = "<-11>11";
+$TZ{"Etc/GMT+12"} = "<-12>12";
+
+open(O, "> $tzdout/tzdata.lua") || die "open($tzdout/tzdata.lua): $!\n";
+
+print STDERR "Writing time zones to $tzdout/tzdata.lua ... ";
+print O <<HEAD;
+-- Licensed to the public under the Apache License 2.0.
+
+module "luci.sys.zoneinfo.tzdata"
 
-print <<HEAD;
---[[
-LuCI - Autogenerated Zoneinfo Module
+TZ = {
+HEAD
 
-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
+foreach my $zone ( sort keys %TZ ) {
+       printf O "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
+}
 
-       http://www.apache.org/licenses/LICENSE-2.0
+print O "}\n";
+close O;
 
-]]--
+print STDERR "done\n";
 
-module "luci.sys.zoneinfo"
 
-TZ = {
-HEAD
+open (O, "> $tzdout/tzoffset.lua") || die "open($tzdout/tzoffset.lua): $!\n";
 
-foreach my $zone ( sort keys %TZ ) {
-       printf "\t{ '%s', '%s' },\n", $zone, $TZ{$zone}
-}
+print STDERR "Writing time offsets to $tzdout/tzoffset.lua ... ";
+print O <<HEAD;
+-- Licensed to the public under the Apache License 2.0.
 
-print <<HEAD;
-}
+module "luci.sys.zoneinfo.tzoffset"
 
 OFFSET = {
 HEAD
@@ -103,8 +136,8 @@ foreach my $tz ( sort keys %TZ ) {
                        $offset  = $s * $h * 60 * 60;
                        $offset += $s * $m * 60;
 
-                       printf("\t%-5s = %6d,\t-- %s\n",
-                               lc($std), $offset, $std);
+                       printf O "\t%-5s = %6d,\t-- %s\n",
+                               lc($std), $offset, $std;
 
                        $seen{$std} = 1;
 
@@ -122,15 +155,15 @@ foreach my $tz ( sort keys %TZ ) {
                                        $offset += 60 * 60;
                                }
 
-                               printf("\t%-5s = %6d,\t-- %s\n",
-                                       lc($dst), $offset, $dst);
+                               printf O "\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);
+                       printf O "\t%-5s = %6d,\t-- %s\n",
+                               lc($std), $offset, $std;
 
                        $seen{$std} = 1;
                }
@@ -138,4 +171,7 @@ foreach my $tz ( sort keys %TZ ) {
        }
 }
 
-print "}\n";
+print O "}\n";
+close O;
+
+print STDERR "done\n";