// File: /workspace/github_c_makefile_star2_O2/fabiovila[P]fs2ch/fs2ch.cpp
// Function: main
int main(int argc, char *argv[])
{
	
  std::vector <stFile> Files;
  stFile tFiles;
  char chBuffer[100];
  string path;
  string outputname;
  MIMELIST = mime();
  
  if (argc <= 1) {
	  boost::filesystem::path cpath(boost::filesystem::current_path());	
	  path = cpath.string();
	} else {
		path = argv[1];
	}

   if (argc == 3){
	   outputname = argv[2];
	   
   } else outputname = "out.h";
  cout << "Input directory \e[92m" << path << "\e[0m\n";
  cout << "Output file as \e[92m" << outputname << "\e[0m\n";
  cout << "\e[1mMounting the filesystem ..." << "\e[0m\n";
  listfolder(path);
  
  for (std::vector<std::string>::const_iterator i = List.begin(); i != List.end(); ++i){
	if (!boost::filesystem::is_regular_file(*i)) continue; // Opened files return undefined length 
		std::ifstream file(*i, std::ios::binary );
		if (file) {
					// Get file size
					std::uintmax_t  length = boost::filesystem::file_size(*i);
					// Alloc buffer
					//cout << *i << " " << length << " " << boost::filesystem::is_regular_file(*i) << " " << boost::filesystem::file_size(*i);
					char * buffer = new char [length];
					// Read and store name and size
					file.read(buffer,length);
						tFiles.name = *i;
						// clear path
						tFiles.name = tFiles.name.substr(path.size(),tFiles.name.size());
						tFiles.mime = getmime(tFiles.name);
						tFiles.bytes = buffer;
						tFiles.size = length;					  
						Files.push_back(tFiles);
					file.close();
		} else exit(1);
	} 

	// Create out.h 
	 std::ofstream out(outputname);
	 int index = 0, a = 0;
	 cout << " Creating \e[92m"<< outputname << "\e[0m file ... \n";
	 out << "typedef struct { const char *name; const char *mime; int size; const unsigned char *data; } stFile;\n";
	 out << "#define FS_FILES	" << Files.size() << "\n";

	 
	 index = 0;
	 for (std::vector<stFile>::iterator i = Files.begin(); i != Files.end(); ++i) {
		 out << "// " << index << " : " << i->name  << " " << i->mime << " " << i->size << "\n";
		 index++;
	 }
	 
	 
	 out << "\n\nconst volatile stFile fsFiles[" << Files.size() <<"] = { "; 
	 

	 for (std::vector<stFile>::iterator i = Files.begin(); i != Files.end(); ++i){
		 cout << " " << i->name << "\n";
		 
		 out << "{\"" << i->name << "\" , ";
		 out << "\"" << i->mime << "\" , ";
		 out << i->size << ", ";
		 out << "(const unsigned char*)\""; 
			for (a = 0; a < i->size; a++) {
				out << dec2hex[(unsigned char) i->bytes[a]];	
			}
		if ( Files.end() - i == 1 ) out << "\"}"; else out << "\"}, "; 		
		 
	 }
	out << "};"; 
	out.close();
	cout << "\n\e[1mFiles embedded: " << Files.size() <<  " in struct: \e[0m\n";
	cout << " \e[96mstruct {\n  const char *name; \n  const char *mime; \n  int size; \n  const unsigned char *data;\n };\e[0m\n";
	cout << "\n\e[1mInclude \e[92m" << outputname <<  "\e[0m in your application and use fsFiles["<< Files.size() << "]\n\n\n\e[0m";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
andq $0x0, -0x30(%rbp)
leaq -0xc8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, %rbx
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0x98(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movl %edi, %r14d
xorps %xmm0, %xmm0
movb %cl, (%rax)
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, -0x40(%rbp)
movb %cl, (%rax)
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq -0x320(%rbp), %rdi
callq 0x4f78
leaq 0xd906(%rip), %rdi      # 0x121d0
leaq -0x320(%rbp), %r15
movq %r15, %rsi
callq 0x9dd0
movq %r15, %rdi
callq 0x9c24
cmpl $0x1, %r14d
jg 0x4914
leaq -0x320(%rbp), %rdi
callq 0x83f1
leaq -0x88(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x2110
leaq -0x320(%rbp), %rdi
callq 0x20e0
jmp 0x4939
movq 0x8(%rbx), %rsi
leaq -0x88(%rbp), %rdi
callq 0x22f0
cmpl $0x3, %r14d
jne 0x4939
movq 0x10(%rbx), %rsi
leaq -0x60(%rbp), %rdi
callq 0x22f0
jmp 0x4949
leaq 0x6bcd(%rip), %rsi      # 0xb50d
leaq -0x60(%rbp), %rdi
callq 0x22f0
movq 0xd688(%rip), %rdi      # 0x11fd8
leaq 0x6bbc(%rip), %rsi      # 0xb513
callq 0x21a0
leaq -0x88(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x6d00(%rip), %rsi      # 0xb672
movq %rax, %rdi
callq 0x21a0
movq 0xd657(%rip), %rdi      # 0x11fd8
leaq 0x6ba1(%rip), %rsi      # 0xb529
callq 0x21a0
leaq -0x60(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x6cd2(%rip), %rsi      # 0xb672
movq %rax, %rdi
callq 0x21a0
movq 0xd629(%rip), %rdi      # 0x11fd8
leaq 0x6b88(%rip), %rsi      # 0xb53e
callq 0x21a0
leaq 0x6cb0(%rip), %rsi      # 0xb672
movq %rax, %rdi
callq 0x21a0
leaq -0x88(%rbp), %rdi
callq 0x475e
movq 0xd7db(%rip), %r14      # 0x121b8
leaq -0x118(%rbp), %r13
cmpq 0xd7d5(%rip), %r14      # 0x121c0
je 0x4b42
movq %r15, %rdi
movq %r14, %rsi
callq 0x2240
movq %r15, %rdi
callq 0x840b
movl %eax, %ebx
movq %r15, %rdi
callq 0x20e0
testb %bl, %bl
je 0x4b39
movq %r15, %rdi
movq %r14, %rsi
pushq $0x4
popq %rdx
callq 0x22b0
movq -0x320(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x300(%rbp,%rax)
jne 0x4ed3
movq %r13, %rdi
movq %r14, %rsi
callq 0x2240
movq %r13, %rdi
callq 0x842f
movq %rax, %r15
movq %r13, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x2030
movq %rax, %r12
leaq -0x320(%rbp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x2220
leaq -0xd8(%rbp), %rdi
movq %r14, %rsi
callq 0x2110
movq %r15, -0x68(%rbp)
movq -0x80(%rbp), %rdx
movq -0xd0(%rbp), %rcx
movq %r13, %rdi
movq %r13, %r15
leaq -0xd8(%rbp), %r13
movq %r13, %rsi
callq 0x21e0
movq %r13, %rdi
movq %r15, %rsi
callq 0x2210
movq %r15, %rdi
callq 0x20e0
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2240
movq %r15, %rdi
movq %rbx, %rsi
callq 0x469c
movq %r15, %r13
leaq -0xa8(%rbp), %rdi
movq %r15, %rsi
callq 0x2210
movq %r15, %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x20e0
movq %r12, -0xb8(%rbp)
movq -0x68(%rbp), %rax
movl %eax, -0xb0(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x843c
leaq -0x320(%rbp), %rdi
callq 0x2060
leaq -0x320(%rbp), %r15
movq %r15, %rdi
callq 0x2070
addq $0x20, %r14
jmp 0x49e4
leaq -0x320(%rbp), %rdi
leaq -0x60(%rbp), %rsi
pushq $0x10
popq %rdx
callq 0x2300
movq 0xd47c(%rip), %rdi      # 0x11fd8
leaq 0x69fb(%rip), %rsi      # 0xb55e
callq 0x21a0
leaq -0x60(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x69f3(%rip), %rsi      # 0xb56e
movq %rax, %rdi
callq 0x21a0
leaq 0x69f4(%rip), %rsi      # 0xb57e
leaq -0x320(%rbp), %rdi
callq 0x21a0
leaq 0x6a46(%rip), %rsi      # 0xb5e3
leaq -0x320(%rbp), %rdi
callq 0x21a0
movq %rax, %rdi
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
pushq $0x50
popq %rcx
cqto
idivq %rcx
movq %rax, %rsi
callq 0x2140
leaq 0x69b1(%rip), %rsi      # 0xb57c
movq %rax, %rdi
callq 0x21a0
xorl %ebx, %ebx
movq -0x40(%rbp), %r14
leaq 0x6a19(%rip), %r13      # 0xb5f9
leaq 0x6a44(%rip), %r15      # 0xb62b
leaq 0x698e(%rip), %r12      # 0xb57c
cmpq -0x38(%rbp), %r14
je 0x4c6b
leaq -0x320(%rbp), %rdi
leaq 0x69f3(%rip), %rsi      # 0xb5f5
callq 0x21a0
movq %rax, %rdi
movl %ebx, %esi
callq 0x22d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x21a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
addq $0x30, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movl -0x8(%r14), %esi
movq %rax, %rdi
callq 0x22d0
movq %rax, %rdi
movq %r12, %rsi
callq 0x21a0
incl %ebx
addq $0x20, %r14
jmp 0x4bee
leaq 0x698b(%rip), %rsi      # 0xb5fd
leaq -0x320(%rbp), %rdi
callq 0x21a0
movq %rax, %rdi
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
cqto
pushq $0x50
popq %rcx
idivq %rcx
movq %rax, %rsi
callq 0x2140
leaq 0x697e(%rip), %rsi      # 0xb61e
movq %rax, %rdi
callq 0x21a0
movq -0x40(%rbp), %rbx
leaq -0x320(%rbp), %r14
leaq 0x696e(%rip), %r15      # 0xb628
leaq 0xd53f(%rip), %r13      # 0x12200
cmpq -0x38(%rbp), %rbx
je 0x4dc2
movq 0xd306(%rip), %rdi      # 0x11fd8
leaq 0x6952(%rip), %rsi      # 0xb62b
callq 0x21a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2180
movq %rax, %rdi
leaq 0x6889(%rip), %rsi      # 0xb57c
callq 0x21a0
movq %r14, %rdi
leaq 0x6923(%rip), %rsi      # 0xb625
callq 0x21a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movq %r14, %rdi
leaq 0x691c(%rip), %rsi      # 0xb643
callq 0x21a0
leaq 0x30(%rbx), %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movl 0x28(%rbx), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %rdi
leaq 0x68d2(%rip), %rsi      # 0xb62a
callq 0x21a0
movq %r14, %rdi
leaq 0x68c6(%rip), %rsi      # 0xb62d
callq 0x21a0
xorl %r12d, %r12d
movslq 0x28(%rbx), %rax
cmpq %rax, %r12
jge 0x4d94
movq 0x20(%rbx), %rax
movzbl (%rax,%r12), %esi
shll $0x5, %esi
addq %r13, %rsi
movq %r14, %rdi
callq 0x2180
incq %r12
jmp 0x4d6f
movq -0x38(%rbp), %rax
subq %rbx, %rax
cmpq $0x50, %rax
leaq 0x68a2(%rip), %rsi      # 0xb648
leaq 0x6898(%rip), %rax      # 0xb645
cmoveq %rax, %rsi
movq %r14, %rdi
callq 0x21a0
addq $0x50, %rbx
jmp 0x4cc1
leaq 0x6884(%rip), %rsi      # 0xb64d
leaq -0x320(%rbp), %rdi
callq 0x21a0
leaq -0x320(%rbp), %rdi
callq 0x2260
movq 0xd1f0(%rip), %rdi      # 0x11fd8
leaq 0x6861(%rip), %rsi      # 0xb650
callq 0x21a0
movq %rax, %rdi
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
cqto
pushq $0x50
popq %rcx
idivq %rcx
movq %rax, %rsi
callq 0x2140
leaq 0x6850(%rip), %rsi      # 0xb666
movq %rax, %rdi
callq 0x21a0
movq 0xd1b3(%rip), %rdi      # 0x11fd8
leaq 0x684c(%rip), %rsi      # 0xb678
callq 0x21a0
movq 0xd1a0(%rip), %rdi      # 0x11fd8
leaq 0x68a5(%rip), %rsi      # 0xb6e4
callq 0x21a0
leaq -0x60(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x68a0(%rip), %rsi      # 0xb6f7
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
cqto
pushq $0x50
popq %rcx
idivq %rcx
movq %rax, %rsi
callq 0x2140
leaq 0x68a0(%rip), %rsi      # 0xb721
movq %rax, %rdi
callq 0x21a0
leaq -0x320(%rbp), %rdi
callq 0x2280
leaq -0x60(%rbp), %rdi
callq 0x20e0
leaq -0x88(%rbp), %rdi
callq 0x20e0
leaq -0xd8(%rbp), %rdi
callq 0x8478
leaq -0x40(%rbp), %rdi
callq 0x8498
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x2200
jmp 0x4f11
jmp 0x4f0c
jmp 0x4f0c
jmp 0x4f0c
jmp 0x4f0c
jmp 0x4f0c
jmp 0x4f22
jmp 0x4f22
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
jmp 0x4f01
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
callq 0x20e0
jmp 0x4f25
jmp 0x4f22
jmp 0x4f37
movq %rax, %rbx
jmp 0x4f46
movq %rax, %rbx
leaq -0x320(%rbp), %rdi
callq 0x20e0
jmp 0x4f46
movq %rax, %rbx
leaq -0x320(%rbp), %rdi
callq 0x2070
jmp 0x4f46
jmp 0x4f37
jmp 0x4f37
movq %rax, %rbx
leaq -0x320(%rbp), %rdi
callq 0x2280
leaq -0x60(%rbp), %rdi
callq 0x20e0
leaq -0x88(%rbp), %rdi
callq 0x20e0
leaq -0xd8(%rbp), %rdi
callq 0x8478
leaq -0x40(%rbp), %rdi
callq 0x8498
movq %rbx, %rdi
callq 0x22e0