#!/usr/bin/python

#Copyright (c) 2016, prpl Foundation
#
#Permission to use, copy, modify, and/or distribute this software for any purpose with or without 
#fee is hereby granted, provided that the above copyright notice and this permission notice appear 
#in all copies.
#
#THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
#INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE 
#FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
#LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 
#ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#This code was written by Carlos Moratelli at Embedded System Group (GSE) at PUCRS/Brazil.

import codecs
import os



listOfFiles = [ 	"bg_stripe.png",
			"pic32mz.jpg",		
			"board.js",
			"cloud.png",
			"favicon.png",
			"index.html",
			"picotcp_style.css"]

def  write_permission(output):
	output.write("""/*
Copyright (c) 2016, prpl Foundation

Permission to use, copy, modify, and/or distribute this software for any purpose with or without 
fee is hereby granted, provided that the above copyright notice and this permission notice appear 
in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE 
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

This code was written by Carlos Moratelli at Embedded System Group (GSE) at PUCRS/Brazil.

*/

""")

#Write the C header file 
def write_header(output):
	write_permission(output)
	
	output.write("""
// THIS FILE IS AUTOMATICALLY GENERATED.
// MODIFY html-sources/make-Cfiles.py INSTEAD OF THIS.

#ifndef WEB_FILES_H_
#define WEB_FILES_H_

""")
	output.write("""

struct web_file {
	const char * filename;
	const int * filesize;
	const char * content;
	const int cacheable;
};

extern const struct web_file web_files[%d];
extern const int num_web_files;
""" % (len(listOfFiles), ) )

	output.write("\n#endif\n")


#Write the C file 
def write_c_file(output, path):
	write_permission(output)
	
	output.write("""
// THIS FILE IS AUTOMATICALLY GENERATED.
// MODIFY sources/mkCfiles.py INSTEAD OF THIS.

		
#include "web_files.h"

""")
	
	hex_encoder = codecs.getencoder('hex')
	
	for name in listOfFiles:
		name_as_scores = name.replace(".","_")
		lenname = name_as_scores + "_len"
		count = 0;
		bin_handle = open(path + name,'rb')
		output.write("static const unsigned char %s[] = {\n    " % (name_as_scores, ) )
		while(1):
			c = bin_handle.read(1)
			if not c: 
				break
			output.write("0x%s, " % (hex_encoder(c)[0].decode(), ))
			count+=1
			if (count % 16 == 0):
				output.write("\n    ")

		output.write("\n};\nstatic const int %s = %s;\n\n" % (lenname, count))

	output.write("const struct web_file web_files[%d] = {\n"%(len(listOfFiles),))
	for name in listOfFiles:
		name_as_scores = name.replace(".","_")
		lenname = name_as_scores + "_len"
		output.write("    { \"%s\", &%s, %s, 1 },\n"%(name, lenname, name_as_scores))

	output.write("};\n")
	output.write("const int num_web_files = %d;\n\n"%(len(listOfFiles),))


local_path = os.path.dirname(os.path.realpath(__file__)) + '/'
print local_path

try:
	header_file = open(local_path + "../web_files.h",'w')
	c_file = open(local_path + "../web_files.c", 'w')
except:
	print("Failed to open files for writing!")
	exit()


write_header(header_file)

write_c_file(c_file, local_path)

header_file.close()

c_file.close()

