summaryrefslogtreecommitdiffstats
path: root/tools/buildzip.pl
blob: cc6bf8458b532ac77a54067c259bc90ee59f79e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/usr/bin/perl
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#

$ROOT="..";

my $ziptool="zip";
my $output="rockbox.zip";
my $verbose;
my $exe;
my $target;
my $archos;
my $incfonts;

while(1) {
    if($ARGV[0] eq "-r") {
        $ROOT=$ARGV[1];
        shift @ARGV;
        shift @ARGV;    
    }

    elsif($ARGV[0] eq "-z") {
        $ziptool=$ARGV[1];
        shift @ARGV;
        shift @ARGV;    
    }

    elsif($ARGV[0] eq "-t") {
        # The target name as used in ARCHOS in the root makefile
        $archos=$ARGV[1];
        shift @ARGV;
        shift @ARGV;    
    }
    elsif($ARGV[0] eq "-i") {
        # The target id name as used in TARGET_ID in the root makefile
        $target_id=$ARGV[1];
        shift @ARGV;
        shift @ARGV;    
    }
    elsif($ARGV[0] eq "-o") {
        $output=$ARGV[1];
        shift @ARGV;
        shift @ARGV;    
    }
    elsif($ARGV[0] eq "-f") {
        $incfonts=$ARGV[1]; # 0 - no fonts, 1 - fonts only 2 - fonts and package
        shift @ARGV;
        shift @ARGV;    
    }

    elsif($ARGV[0] eq "-v") {
        $verbose =1;
        shift @ARGV;
    }
    else {
        $target = $ARGV[0];
        $exe = $ARGV[1];
        last;
    }
}


my $firmdir="$ROOT/firmware";
my $appsdir="$ROOT/apps";
my $viewer_bmpdir="$ROOT/apps/plugins/bitmaps/viewer_defaults";

my $cppdef = $target;

sub gettargetinfo {
    open(GCC, ">gcctemp");
    # Get the LCD screen depth and graphical status
    print GCC <<STOP
\#include "config.h"
#ifdef HAVE_LCD_BITMAP
Bitmap: yes
Depth: LCD_DEPTH
Icon Width: CONFIG_DEFAULT_ICON_WIDTH
Icon Height: CONFIG_DEFAULT_ICON_HEIGHT
#endif
Codec: CONFIG_CODEC
#ifdef HAVE_REMOTE_LCD
Remote Depth: LCD_REMOTE_DEPTH
Remote Icon Width: CONFIG_REMOTE_DEFAULT_ICON_WIDTH
Remote Icon Height: CONFIG_REMOTE_DEFAULT_ICON_HEIGHT
#else
Remote Depth: 0
#endif
#ifdef HAVE_RECORDING
Recording: yes
#endif
STOP
;
    close(GCC);

    my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";

    # print STDERR "CMD $c\n";

    open(TARGET, "$c|");

    my ($bitmap, $depth, $swcodec, $icon_h, $icon_w);
    my ($remote_depth, $remote_icon_h, $remote_icon_w);
    my ($recording);
    $icon_count = 1;
    while(<TARGET>) {
        # print STDERR "DATA: $_";
        if($_ =~ /^Bitmap: (.*)/) {
            $bitmap = $1;
        }
        elsif($_ =~ /^Depth: (\d*)/) {
            $depth = $1;
        }
        elsif($_ =~ /^Icon Width: (\d*)/) {
            $icon_w = $1;
        }
        elsif($_ =~ /^Icon Height: (\d*)/) {
            $icon_h = $1;
        }
        elsif($_ =~ /^Codec: (\d*)/) {
            # SWCODEC is 1, the others are HWCODEC
            $swcodec = ($1 == 1);
        }
        elsif($_ =~ /^Remote Depth: (\d*)/) {
            $remote_depth = $1;
        }
        elsif($_ =~ /^Remote Icon Width: (\d*)/) {
            $remote_icon_w = $1;
        }
        elsif($_ =~ /^Remote Icon Height: (\d*)/) {
            $remote_icon_h = $1;
        }
        if($_ =~ /^Recording: (.*)/) {
            $recording = $1;
        }
    }
    close(TARGET);
    unlink("gcctemp");

    return ($bitmap, $depth, $icon_w, $icon_h, $recording,
            $swcodec, $remote_depth, $remote_icon_w, $remote_icon_h);
}

sub filesize {
    my ($filename)=@_;
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        $atime,$mtime,$ctime,$blksize,$blocks)
        = stat($filename);
    return $size;
}

sub buildlangs {
    my ($outputlang)=@_;
    my $dir = "$ROOT/apps/lang";
    opendir(DIR, $dir);
    my @files = grep { /\.lang$/ } readdir(DIR);
    closedir(DIR);

    # Exclude some bad languages (uncomment and adjust in case of release
    # and/or End Times)
    # @files = grep(!/(afrikaans|hindi|slovenscina|turkce)\.lang/, @files);

    # Exclude more languages on the player which won't work on charcell display
    if ($archos =~ /^"?player:/ ) {
        @files = grep(!/(chinese-simp|bulgarian|chinese-trad|hindi|japanese|korean|thai)\.lang/, @files);
    }

    for(@files) {
        my $output = $_;
        $output =~ s/(.*)\.lang/$1.lng/;
        print "$ROOT/tools/genlang -e=$dir/english.lang -t=$archos -i=$target_id -b=$outputlang/$output $dir/$_\n" if($verbose);
        system ("$ROOT/tools/genlang -e=$dir/english.lang -t=$archos -i=$target_id -b=$outputlang/$output $dir/$_ >/dev/null 2>&1");
    }
}

sub buildzip {
    my ($zip, $image, $fonts)=@_;

    my ($bitmap, $depth, $icon_w, $icon_h, $recording, $swcodec,
        $remote_depth, $remote_icon_w, $remote_icon_h) = &gettargetinfo();

    # print "Bitmap: $bitmap\nDepth: $depth\nSwcodec: $swcodec\n";

    # remove old traces
    `rm -rf .rockbox`;

    mkdir ".rockbox", 0777;

    if(!$bitmap) {
        # always disable fonts on non-bitmap targets
        $fonts = 0;
    }
    # create the file so the database does not try indexing a folder
    `touch .rockbox/database.ignore`;
    
    if($fonts) {
        mkdir ".rockbox/fonts", 0777;

        opendir(DIR, "$ROOT/fonts") || die "can't open dir fonts";
        my @fonts = grep { /\.bdf$/ && -f "$ROOT/fonts/$_" } readdir(DIR);
        closedir DIR;

        for(@fonts) {
            my $f = $_;

            print "FONT: $f\n" if($verbose);
            my $o = $f;
            $o =~ s/\.bdf/\.fnt/;
            my $cmd ="$ROOT/tools/convbdf -f -o \".rockbox/fonts/$o\" \"$ROOT/fonts/$f\" >/dev/null 2>&1";
            print "CMD: $cmd\n" if($verbose);
            `$cmd`;
            
        }
        if($fonts < 2) {
          # fonts-only package, return
          return;
        }
    }

    mkdir ".rockbox/langs", 0777;
    mkdir ".rockbox/rocks", 0777;
    mkdir ".rockbox/rocks/games", 0777;
    mkdir ".rockbox/rocks/apps", 0777;
    mkdir ".rockbox/rocks/demos", 0777;
    mkdir ".rockbox/rocks/viewers", 0777;

    if ($recording) {
        mkdir ".rockbox/recpresets", 0777;
    }

    if($swcodec) {
        mkdir ".rockbox/eqs", 0777;

        `cp $ROOT/apps/eqs/*.cfg .rockbox/eqs/`; # equalizer presets
    }

    mkdir ".rockbox/wps", 0777;
    mkdir ".rockbox/themes", 0777;
    if ($bitmap) {
        open(THEME, ">.rockbox/themes/rockbox_default_icons.cfg");
        print THEME <<STOP
# this config file was auto-generated to make it
# easy to reset the icons back to default
iconset: -
# taken from apps/gui/icon.c
viewers iconset: /.rockbox/icons/viewers.bmp
remote iconset: -
# taken from apps/gui/icon.c
remote viewers iconset: /.rockbox/icons/remote_viewers.bmp

STOP
;
        close(THEME);
    }
    
    mkdir ".rockbox/codepages", 0777;

    if($bitmap) {
        system("$ROOT/tools/codepages");
    }
    else {
        system("$ROOT/tools/codepages -m");
    }
    $c = 'find . -name "*.cp" ! -empty -exec mv {} .rockbox/codepages/ \; >/dev/null 2>&1';
    `$c`;

    if($bitmap) {
        mkdir ".rockbox/codecs", 0777;
        if($depth > 1) {
            mkdir ".rockbox/backdrops", 0777;
        }

        my $c = 'find apps -name "*.codec" ! -empty -exec cp {} .rockbox/codecs/ \; 2>/dev/null';
        `$c`;

        my @call = `find .rockbox/codecs -type f 2>/dev/null`;
        if(!$call[0]) {
            # no codec was copied, remove directory again
            rmdir(".rockbox/codecs");

        }
    }

    $c= 'find apps "(" -name "*.rock" -o -name "*.ovl" ")" ! -empty -exec cp {} .rockbox/rocks/ \;';
    print `$c`;

    open VIEWERS, "$ROOT/apps/plugins/viewers.config" or
        die "can't open viewers.config";
    @viewers = <VIEWERS>;
    close VIEWERS;

    open VIEWERS, ">.rockbox/viewers.config" or
        die "can't create .rockbox/viewers.config";

    foreach my $line (@viewers) {
        if ($line =~ /([^,]*),([^,]*),/) {
            my ($ext, $plugin)=($1, $2);
            my $r = "${plugin}.rock";
            my $oname;

            my $dir = $r;
            my $name;

            # strip off the last slash and file name part
            $dir =~ s/(.*)\/(.*)/$1/;
            # store the file name part
            $name = $2;

            # get .ovl name (file part only)
            $oname = $name;
            $oname =~ s/\.rock$/.ovl/;

            # print STDERR "$ext $plugin $dir $name $r\n";

            if(-e ".rockbox/rocks/$name") {
                if($dir ne "rocks") {
                    # target is not 'rocks' but the plugins are always in that
                    # dir at first!
                    `mv .rockbox/rocks/$name .rockbox/rocks/$r`;
                }
                print VIEWERS $line;
            }
            elsif(-e ".rockbox/rocks/$r") {
                # in case the same plugin works for multiple extensions, it
                # was already moved to the viewers dir
                print VIEWERS $line;
            }

            if(-e ".rockbox/rocks/$oname") {
                # if there's an "overlay" file for the .rock, move that as
                # well
                `mv .rockbox/rocks/$oname .rockbox/rocks/$dir`;
            }
        }
    }
    close VIEWERS;
                
    open CATEGORIES, "$ROOT/apps/plugins/CATEGORIES" or
        die "can't open CATEGORIES";
    @rock_targetdirs = <CATEGORIES>;
    close CATEGORIES;
    foreach my $line (@rock_targetdirs) {
        if ($line =~ /([^,]*),(.*)/) {
            my ($plugin, $dir)=($1, $2);
            `mv .rockbox/rocks/${plugin}.rock .rockbox/rocks/$dir 2> /dev/null`;
        }
    }
    
    if ($bitmap) {
        mkdir ".rockbox/icons", 0777;
        `cp $viewer_bmpdir/viewers.${icon_w}x${icon_h}x$depth.bmp .rockbox/icons/viewers.bmp`;
        if ($remote_depth) {
            `cp $viewer_bmpdir/remote_viewers.${remote_icon_w}x${remote_icon_h}x$remote_depth.bmp .rockbox/icons/remote_viewers.bmp`;
        }
    }
    
    `cp $ROOT/apps/tagnavi.config .rockbox/`;
      
    if($bitmap) {
        `cp $ROOT/apps/plugins/sokoban.levels .rockbox/rocks/games/`; # sokoban levels
        `cp $ROOT/apps/plugins/snake2.levels .rockbox/rocks/games/`; # snake2 levels
    }

    if($image) {
        # image is blank when this is a simulator
        if( filesize("rockbox.ucl") > 1000 ) {
            `cp rockbox.ucl .rockbox/`;  # UCL for flashing
        }
        if( filesize("rombox.ucl") > 1000) {
            `cp rombox.ucl .rockbox/`;  # UCL for flashing
        }
        
        # Check for rombox.target
        if ($image=~/(.*)\.(\w+)$/)
        {
            my $romfile = "rombox.$2";
            if (filesize($romfile) > 1000)
            {
                `cp $romfile .rockbox/`;
            }
        }
    }

    mkdir ".rockbox/docs", 0777;
    for(("COPYING",
         "LICENSES",
         "KNOWN_ISSUES"
        )) {
        `cp $ROOT/docs/$_ .rockbox/docs/$_.txt`;
    }
    if ($fonts) {
        `cp $ROOT/docs/profontdoc.txt .rockbox/docs/`;
    }
    for(("sample.colours",
         "sample.icons"
        )) {
        `cp $ROOT/docs/$_ .rockbox/docs/`;
    }

    # Now do the WPS dance
    if(-d "$ROOT/wps") {
        system("perl $ROOT/wps/wpsbuild.pl -r $ROOT $ROOT/wps/WPSLIST $target");
    }
    else {
        print STDERR "No wps module present, can't do the WPS magic!\n";
    }

    # and the info file
    system("cp rockbox-info.txt .rockbox/");

    # now copy the file made for reading on the unit:
    #if($notplayer) {
    #    `cp $webroot/docs/Help-JBR.txt .rockbox/docs/`;
    #}
    #else {
    #    `cp $webroot/docs/Help-Stu.txt .rockbox/docs/`;
    #}

    buildlangs(".rockbox/langs");

}

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
 localtime(time);

$mon+=1;
$year+=1900;

#$date=sprintf("%04d%02d%02d", $year,$mon, $mday);
#$shortdate=sprintf("%02d%02d%02d", $year%100,$mon, $mday);

# made once for all targets
sub runone {
    my ($target, $fonts)=@_;

    # build a full install .rockbox directory
    buildzip($output, $target, $fonts);

    # create a zip file from the .rockbox dfir

    `rm -f $output`;
    if($verbose) {
      print "find .rockbox | xargs $ziptool $output >/dev/null\n";
    }
    `find .rockbox | xargs $ziptool $output >/dev/null`;

    if($target && ($fonts != 1)) {
        # On some targets, rockbox.* is inside .rockbox
        if($target !~ /(mod|ajz|wma)\z/i) {
            `cp $target .rockbox/$target`;
            $target = ".rockbox/".$target;
        }
        
        if($verbose) {
            print "$ziptool $output $target\n";
        }
        `$ziptool $output $target`;
    }

    # remove the .rockbox afterwards
    `rm -rf .rockbox`;
};

if(!$exe) {
    # not specified, guess!
    if($target =~ /(recorder|ondio)/i) {
        $exe = "ajbrec.ajz";
    }
    elsif($target =~ /iriver/i) {
        $exe = "rockbox.iriver";
    }
    else {
        $exe = "archos.mod";
    }
}
elsif($exe =~ /rockboxui/) {
    # simulator, exclude the exe file
    $exe = "";
}

runone($exe, $incfonts);