1#!/usr/bin/perl
2
3open(OUTPUT, "> fsdata.c");
4
5chdir("fs");
6open(FILES, "find . -type f |");
7
8while($file = <FILES>) {
9
10    # Do not include files in CVS directories nor backup files.
11    if($file =~ /(CVS|~)/) {
12    	next;
13    }
14
15    chop($file);
16
17    open(HEADER, "> /tmp/header") || die $!;
18    if($file =~ /404/) {
19	print(HEADER "HTTP/1.0 404 File not found\r\n");
20    } else {
21	print(HEADER "HTTP/1.0 200 OK\r\n");
22    }
23    print(HEADER "Server: lwIP/pre-0.6 (http://www.sics.se/~adam/lwip/)\r\n");
24    if($file =~ /\.html$/) {
25	print(HEADER "Content-type: text/html\r\n");
26    } elsif($file =~ /\.gif$/) {
27	print(HEADER "Content-type: image/gif\r\n");
28    } elsif($file =~ /\.png$/) {
29	print(HEADER "Content-type: image/png\r\n");
30    } elsif($file =~ /\.jpg$/) {
31	print(HEADER "Content-type: image/jpeg\r\n");
32    } elsif($file =~ /\.class$/) {
33	print(HEADER "Content-type: application/octet-stream\r\n");
34    } elsif($file =~ /\.ram$/) {
35	print(HEADER "Content-type: audio/x-pn-realaudio\r\n");
36    } else {
37	print(HEADER "Content-type: text/plain\r\n");
38    }
39    print(HEADER "\r\n");
40    close(HEADER);
41
42    unless($file =~ /\.plain$/ || $file =~ /cgi/) {
43	system("cat /tmp/header $file > /tmp/file");
44    } else {
45	system("cp $file /tmp/file");
46    }
47
48    open(FILE, "/tmp/file");
49    unlink("/tmp/file");
50    unlink("/tmp/header");
51
52    $file =~ s/\.//;
53    $fvar = $file;
54    $fvar =~ s-/-_-g;
55    $fvar =~ s-\.-_-g;
56    print(OUTPUT "static const unsigned char data".$fvar."[] = {\n");
57    print(OUTPUT "\t/* $file */\n\t");
58    for($j = 0; $j < length($file); $j++) {
59	printf(OUTPUT "%#02x, ", unpack("C", substr($file, $j, 1)));
60    }
61    printf(OUTPUT "0,\n");
62
63
64    $i = 0;
65    while(read(FILE, $data, 1)) {
66        if($i == 0) {
67            print(OUTPUT "\t");
68        }
69        printf(OUTPUT "%#02x, ", unpack("C", $data));
70        $i++;
71        if($i == 10) {
72            print(OUTPUT "\n");
73            $i = 0;
74        }
75    }
76    print(OUTPUT "};\n\n");
77    close(FILE);
78    push(@fvars, $fvar);
79    push(@files, $file);
80}
81
82for($i = 0; $i < @fvars; $i++) {
83    $file = $files[$i];
84    $fvar = $fvars[$i];
85
86    if($i == 0) {
87        $prevfile = "NULL";
88    } else {
89        $prevfile = "file" . $fvars[$i - 1];
90    }
91    print(OUTPUT "const struct fsdata_file file".$fvar."[] = {{$prevfile, data$fvar, ");
92    print(OUTPUT "data$fvar + ". (length($file) + 1) .", ");
93    print(OUTPUT "sizeof(data$fvar) - ". (length($file) + 1) .", FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT}};\n\n");
94}
95
96print(OUTPUT "#define FS_ROOT file$fvars[$i - 1]\n\n");
97print(OUTPUT "#define FS_NUMFILES $i\n");
98