build: change i18n-lua2po.pl to produce a suitable directory layout
[project/luci.git] / build / mklar.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use File::Find;
5 use Digest::MD5 qw(md5 md5_hex);
6
7 my @search = @ARGV;
8 if( !@search ) {
9         @search = (
10                 glob("libs/*"),
11                 glob("applications/*"),
12                 glob("i18n/*"),
13                 glob("modules/*")
14         );
15 }
16
17
18 sub depth {
19         my $p = shift;
20         my $d = 0;
21         $d++ while( $p =~ m{/}g );
22         return $d;
23 };
24
25
26 my @index;
27 my $offset = 0;
28
29
30 #
31 # Build File Members
32 #
33
34 find( sub {
35         # Skip non-files
36         ( -f $_ ) || return;
37
38         # Skip stuff not in /luasrc/
39         ( $File::Find::name =~ m{/luasrc/} ) || return;
40
41         # Skip .svn foo
42         ( $File::Find::name !~ m{/\.svn\b} ) || return;
43
44         # Exclude luci-statistics and lucittpd for now
45         ( $File::Find::name !~ m{/luci-statistics/} && $File::Find::name !~ m{/lucittpd/} ) || return;
46
47
48         my $file = $File::Find::name;
49         $file =~ s{^.+/luasrc/}{luci/};
50
51         my $command = ( $File::Find::name =~ m{\.lua\z} && $ENV{LUAC} )
52                 ? "$ENV{LUAC} -o - $_ |" : "< $_";
53
54         if( open F, $command )
55         {
56                 warn sprintf "Member at 0x%08X: %s\n", $offset, $file;
57                 push @index, [ ];
58
59                 my $size = 0;
60                 my $pad  = 0;
61
62                 $index[-1][0] = $offset;
63
64                 while( read F, my $buffer, 4096 ) {
65                         $size += length $buffer;
66                         print $buffer;
67                 }
68
69                 if( $size % 4 ) {
70                         $pad = ( 4 - ( $size % 4 ) );
71                 }
72
73                 print "\0" x $pad;
74
75                 $index[-1][1] = $size;
76                 $index[-1][2] = md5($file);
77                 $index[-1][3] = 0x0000;
78                 $index[-1][4] = $file;
79
80                 $offset += $size + $pad;
81
82                 close F;
83         }
84 }, @search );
85
86
87 #
88 # Build File List Member
89 #
90
91 my $filelist = join("\0", map $_->[4], @index) . "\0";
92 my $listsize = length $filelist;
93 push @index, [ $offset, $listsize, "", 0xFFFF, undef ];
94
95 warn sprintf "Filelist at 0x%08X, length 0x%08X\n", $offset, $listsize;
96
97 print $filelist;
98 $offset += $listsize;
99
100 if( $listsize % 4 )
101 {
102         $offset += ( 4 - ($listsize % 4) );
103         print "\0" x ( 4 - ($listsize % 4) );
104 }
105
106
107 my $count = 1;
108 foreach my $file ( @index )
109 {
110         warn sprintf "Index[%4d]: 0x%08X 0x%08X 0x%04X 0x%04X %32s\n",
111                 $count++, $file->[0], $file->[1], $file->[3], 0x0000,
112                 $file->[4] ? md5_hex($file->[4]) : "0" x 32
113         ;
114
115         print pack "NNnna16", $file->[0], $file->[1], $file->[3], 0x0000, $file->[2];
116 }
117
118 warn sprintf "Index at 0x%08X, length 0x%08X\n", $offset, @index * 28;
119 print pack "N", $offset;