#!/usr/bin/perl -w

use strict;
use warnings;
use 5.010;
use FindBin '$Bin';

use constant HTTPD_SERVER_AGENT => "lwIP/1.3.1 (http://savannah.nongnu.org/projects/lwip)";

my @HTTPHeaderStrings =
(
   "Content-type: text/html\r\nContent-Encoding: gzip\r\n",
   "Content-type: text/html\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n",
   "Content-type: image/gif\r\n",
   "Content-type: image/png\r\n",
   "Content-type: image/jpeg\r\n",
   "Content-type: image/bmp\r\n",
   "Content-type: image/x-icon\r\n",
   "Content-type: application/octet-stream\r\n",
   "Content-type: application/x-javascript\r\nContent-Encoding: gzip\r\n",
   "Content-type: application/x-javascript\r\nContent-Encoding: gzip\r\n",
   "Content-type: text/css\r\nContent-Encoding: gzip\r\n",
   "Content-type: application/x-shockwave-flash\r\n",
   "Content-type: text/xml\r\n",
   "Content-type: text/plain\r\n",
   "HTTP/1.0 200 OK\r\n",
   "HTTP/1.0 404 File not found\r\n",
   "HTTP/1.0 400 Bad Request\r\n",
   "HTTP/1.0 501 Not Implemented\r\n",
   "HTTP/1.1 200 OK\r\n",
   "HTTP/1.1 404 File not found\r\n",
   "HTTP/1.1 400 Bad Request\r\n",
   "HTTP/1.1 501 Not Implemented\r\n",
   "Content-Length: ",
   "Connection: Close\r\n",
   "Server: ".HTTPD_SERVER_AGENT."\r\n",
   "\r\n<html><body><h2>404: The requested file cannot be found.</h2></body></html>\r\n"
);

use constant {
  HTTP_HDR_HTML           => 0,  # text/html
  HTTP_HDR_SSI            => 1,  # text/html Expires...
  HTTP_HDR_GIF            => 2,  # image/gif
  HTTP_HDR_PNG            => 3,  # image/png
  HTTP_HDR_JPG            => 4,  # image/jpeg
  HTTP_HDR_BMP            => 5,  # image/bmp
  HTTP_HDR_ICO            => 6,  # image/x-icon
  HTTP_HDR_APP            => 7,  # application/octet-stream
  HTTP_HDR_JS             => 8,  # application/x-javascript
  HTTP_HDR_RA             => 9,  # application/x-javascript
  HTTP_HDR_CSS            => 10, # text/css
  HTTP_HDR_SWF            => 11, # application/x-shockwave-flash
  HTTP_HDR_XML            => 12, # text/xml
  HTTP_HDR_DEFAULT_TYPE   => 13, # text/plain
  HTTP_HDR_OK             => 14, # 200 OK
  HTTP_HDR_NOT_FOUND      => 15, # 404 File not found
  HTTP_HDR_BAD_REQUEST    => 16, # 400 Bad request
  HTTP_HDR_NOT_IMPL       => 17, # 501 Not Implemented
  HTTP_HDR_OK_11          => 18, # 200 OK
  HTTP_HDR_NOT_FOUND_11   => 19, # 404 File not found
  HTTP_HDR_BAD_REQUEST_11 => 20, # 400 Bad request
  HTTP_HDR_NOT_IMPL_11    => 21, # 501 Not Implemented
  HTTP_HDR_CONTENT_LENGTH => 22, # Content-Length: (HTTP 1.1)
  HTTP_HDR_CONN_CLOSE     => 23, # Connection: Close (HTTP 1.1)
  HTTP_HDR_SERVER         => 24, # Server: HTTPD_SERVER_AGENT
  DEFAULT_404_HTML        => 25, # default 404 body
};

use constant HEX_BYTES_PER_LINE => 16;



my $cwd;
my $fsdir;
my $num_args = $#ARGV + 1;
if ($num_args > 0) {
  foreach my $argnum (0 .. $#ARGV) {
    if ($ARGV[$argnum] eq "-h" or $ARGV[$argnum] eq "--help"){
      say "    __  ___      __        ____         __      __       ";
      say "   /  |/  /___ _/ /_____  / __/________/ /___ _/ /_____ _";
      say "  / /|_/ / __ `/ //_/ _ \\/ /_/ ___/ __  / __ `/ __/ __ `/";
      say " / /  / / /_/ / ,< /  __/ __(__  ) /_/ / /_/ / /_/ /_/ / ";
      say "/_/  /_/\\__,_/_/|_|\\___/_/ /____/\\__,_/\\__,_/\\__/\\__,_/  ";
      say "                                                         ";
      say "makefsdata - HTML to C source converter\n\n";
      say "Usage: makefsdata.pl <fsdata_file_dir> <fs_folder_dir>\n";
      exit;
    }
  }
  if($num_args != 2) {
    say "Incorrect arguments!\n";
    say "Usage: makefsdata.pl <fsdata_file_dir> <fs_folder_dir>\n";
    exit;
  }
  if(-d $ARGV[0] and -d $ARGV[1] ){
    # say $ARGV[0];
    # say $ARGV[1];
    $cwd = $ARGV[0];
    $fsdir = $ARGV[1];
  } else {
    say "Drectory dosn't exist!\n";
    exit;
  }
} else {
  $cwd = $Bin;
  $fsdir = "$cwd/fs";
}

open(OUTPUT, "> $cwd/fsdata.c");
chdir($fsdir);

my $dir = '.';

opendir(DIR, $dir) or die "Can't open directory $!";

my $file;
my $fvar;
my $prevfile;
my @files;
my @fvars;

print(OUTPUT "#include \"lwip/def.h\"\n");
print(OUTPUT "#include \"fsdata.h\"\n");
print(OUTPUT "\n\n#define file_NULL (struct fsdata_file *) NULL\n\n\n");

my $z = 0;
while($file = readdir(DIR)) {

  # Do not include files in CVS directories nor backup files.
  if($file =~ /(CVS|~)/) {
    next;
  }
  next if ($file =~ m/^\./);
  say("Processing --> $file");
  my $size = -s $file;

  my $curHeader;
  if($file =~ /\.html$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_HTML];
  } elsif($file =~ /\.js$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_JS];
  } elsif($file =~ /\.css$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_CSS];
  } elsif($file =~ /\.ico$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_ICO];
  } elsif($file =~ /\.gif$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_GIF];
  } elsif($file =~ /\.png$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_PNG];
  } elsif($file =~ /\.jpg$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_JPG];
  } elsif($file =~ /\.class$/) {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_APP];
  } else {
    $curHeader = $HTTPHeaderStrings[HTTP_HDR_DEFAULT_TYPE];
  }

  my $contentLength = "$HTTPHeaderStrings[HTTP_HDR_CONTENT_LENGTH]$size\r\n";
  say $curHeader;
  say "$HTTPHeaderStrings[HTTP_HDR_CONTENT_LENGTH]$size";

  system("cp $file /tmp/file");
  open(FILE, "/tmp/file");
  unlink("/tmp/file");

  my $i = 0;
  my $fvar = $file;
  $fvar =~ s-/-_-g;
  $fvar =~ s-\.-_-g;
  my $filenamelen = length("/$file");
  print(OUTPUT "static const unsigned int dummy_align__$fvar = $z;\n");
  print(OUTPUT "static const unsigned char data__".$fvar."[] = {\n");
  print(OUTPUT "/* /$file ($filenamelen chars) */\n");

  # Filename to hex
  for(my $j = 0; $j < $filenamelen; $j++) {
    printf(OUTPUT "0x%02.2x,", unpack("C", substr("/$file", $j, 1)));
  }
  printf(OUTPUT "0x00,\n");
  $z++;

  print(OUTPUT "\n/* HTTP header */\n");
  sub hearderGen{
    my ($header) = @_;
    my $header_len = length($header);
    print(OUTPUT "\n/* \"$header\" ($header_len bytes) */");
    for(my $j = 0; $j < length($header); $j++) {
      if(($j % HEX_BYTES_PER_LINE) == 0) {
        print(OUTPUT "\n");
      }
      printf(OUTPUT "0x%02.2x,", unpack("C", substr($header, $j, 1)));
    }
    return;
  }

  # Generate random Etag sting
  sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] }
  my $etag = rndStr(15, 'A'..'Z', 0..9, 'a'..'z');

  # Put hex heders to file
  if($file =~ /404/) {
    hearderGen($HTTPHeaderStrings[HTTP_HDR_NOT_FOUND_11]);
  } else {
    hearderGen($HTTPHeaderStrings[HTTP_HDR_OK_11]);
  }
  hearderGen($HTTPHeaderStrings[HTTP_HDR_SERVER]);
  hearderGen($contentLength);
  hearderGen($HTTPHeaderStrings[HTTP_HDR_CONN_CLOSE]);
  hearderGen($curHeader);
  hearderGen("ETag: \"$etag\"\r\n");
  # /r/n
  printf(OUTPUT "0x0d,0x0a,\n");

  say "Etag: \"$etag\"";
  say "----------------------------\n";

  # RAW file to hex
  print(OUTPUT "\n/* raw file data ($size bytes) */");
  while(read(FILE, my $data, 1)) {
    if($i == 0) {
      print(OUTPUT "\n");
    }

    printf(OUTPUT "0x%02.2x,", unpack("C", $data));
    $i++;
    if($i == HEX_BYTES_PER_LINE) {
      print(OUTPUT "");
      $i = 0;
    }
  }
  print(OUTPUT "};\n\n");
  close(FILE);
  push(@fvars, $fvar);
  push(@files, $file);
}

my $i;
for($i = 0; $i < @fvars; $i++) {
  $file = $files[$i];
  $fvar = $fvars[$i];

  if($i == 0) {
    $prevfile = "file_NULL";
  } else {
    $prevfile = "file__" . $fvars[$i - 1];
  }
  print(OUTPUT "const struct fsdata_file file__".$fvar."[] = {{\n\t$prevfile,\n\tdata__$fvar,\n\t");
  print(OUTPUT "data__$fvar + ". (length($file) + 2) .",\n\t");
  print(OUTPUT "sizeof(data__$fvar) - ". (length($file) + 2) .",\n\t1,\n}};\n\n");
}

print(OUTPUT "#define FS_ROOT file__$fvars[$i - 1]\n");
print(OUTPUT "#define FS_NUMFILES $i\n");

say "Done!\nNow you have fsdata.c";