build: add mklar.pl script to generate lar archives from luci source tree
[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\z} ) || return;
43
44         # Skip non-lua files
45         ( $File::Find::name =~ m{\.lua\z} ) || return;
46
47         # Skip i18n files
48         ( $File::Find::name !~ m{/i18n/} ) || return;
49         
50         # Exclude cbi models and controllers for now
51         ( $File::Find::name !~ m{/controller/} && $File::Find::name !~ m{/model/cbi/} ) || return;
52
53         # Exclude luci-statistics and lucittpd for now
54         ( $File::Find::name !~ m{/luci-statistics/} && $File::Find::name !~ m{/lucittpd/} ) || return;
55
56
57         my $file = $File::Find::name;
58         $file =~ s{^.+/luasrc/}{luci/};
59
60         if( open F, "< $_" )
61         {
62                 warn sprintf "Member at 0x%08X: %s\n", $offset, $file;
63                 push @index, [ ];
64
65                 my $size = 0;
66                 my $pad  = 0;
67
68                 $index[-1][0] = $offset;
69                 
70                 while( read F, my $buffer, 4096 ) {
71                         $size += length $buffer;
72                         print $buffer;
73                 }
74
75                 if( $size % 4 ) {
76                         $pad = ( 4 - ( $size % 4 ) );
77                 }
78
79                 print "\0" x $pad;
80
81                 $index[-1][1] = $size;
82                 $index[-1][2] = md5($file);
83                 $index[-1][3] = 0x0000;
84                 $index[-1][4] = $file;
85
86                 $offset += $size + $pad;
87
88                 close F;
89         }
90 }, @search );
91
92
93 #
94 # Build File List Member
95 #
96
97 my $filelist = join("\0", map $_->[4], @index) . "\0";
98 my $listsize = length $filelist;
99 push @index, [ $offset, $listsize, "", 0xFFFF, undef ];
100 warn sprintf "Filelist at 0x%08X\n", $offset;
101
102 if( $listsize % 4 )
103 {
104         $listsize += ( 4 - ($listsize % 4) );
105         $filelist .= "\0" x ( 4 - ($listsize % 4) );
106 }
107
108 print $filelist;
109 $offset += $listsize;
110
111
112 my $count = 1;
113 foreach my $file ( @index )
114 {
115         warn sprintf "Index[%4d]: 0x%08X 0x%08X 0x%04X 0x%04X %32s\n",
116                 $count++, $file->[0], $file->[1], $file->[3], 0x0000,
117                 $file->[4] ? md5_hex($file->[4]) : "0" x 32
118         ;
119
120         print pack "NNnna16", $file->[0], $file->[1], $file->[3], 0x0000, $file->[2];
121 }
122
123 warn sprintf "Index at 0x%08X, length 0x%08X\n", $offset, @index * 28;
124 print pack "N", $offset;