#!/usr/bin/python

#This file is run on Linux
import os
#Compress all html resources
os.system('/usr/bin/7z a microIDE.html.gz microIDE.html -aoa -mx=5  -tgzip>/dev/null')
os.system('/usr/bin/7z a microIDE.css.gz microIDE.css -aoa -mx=5  -tgzip>/dev/null')
os.system('/usr/bin/7z a microIDE.js.gz microIDE.js -aoa -mx=5  -tgzip>/dev/null')
os.system('/usr/bin/7z a favicon.svg.gz favicon.svg -aoa -mx=5  -tgzip>/dev/null')
            #Ace.js is already compressed
            #mode-python.js is already compressed
            
            


# | ------------------------- Function for adding ------------------------- |
#define function for future use
def hexify(fname,hardname,g=open('microIDE.hx','w')):
  f=open(fname,'r')
  #add to _sendStacic function in an if-statement
  g.write("    if file == b'/")
  g.write(hardname)
  g.write("' or file == b'/")
  g.write(hardname)
  g.write(".gz' : #Auto Generated gz file - using https://dl.microIDE.com/sub_pack.py\r\n")
  #if true then send the response headers
  g.write("      f.write(self._con_200)\r\n      f.write(self._con_gz)\r\n      self._sendMime(f,file)\r\n      f.write('\\r\\n')\r\n")
  #then send the file in 8192 byte packages
  line=f.read(8192)
  while len(line)>0:
      #the file is converted into a hex and added as line to the function like f.write(b'\x1f\x8b\x08\x08....[4096 total]...\x4c')'
      out =  ''.join('\\x{:02x}'.format(xx) for xx in bytearray(line))
      g.write("      f.write(b'")
      g.write(out)
      g.write("')\r\n")
      #then garbage collect is called in _sendStatic to prefent stack collision
      g.write("      gc.collect()\r\n")
      line=f.read(8192)
  #after file is send return true in _sendStatic to let the main program know it sucessfuly served the request
  g.write("      return True; #end\r\n")
  f.close()
  print("Conversion Done")
  
  
  
# | ------------------------- Make the lite verions ------------------------- |
#add favicon.svg to the uncompiled version
g=open('microIDE.hx','w')
hexify("favicon.svg.gz","favicon.svg",g)         #Favorite Icon for  website to have an icon - svg makes it scalable
g.close()

g=open('microIDE.py','w')
g.write("#This verions needs all web files to be downloaded seperatly - see https://git.microIDE.com/setup.py\r\n")
f=open('microIDE_nostatic.py','r')
line=f.readline()
while len(line)>0:
  g.write(line)
  if '#<STAICFILEGLAG>' in line:
    e=open('microIDE.hx','r')
    line=e.read(8192)
    while len(line)>0:
      g.write(line)
      line=e.read(8192)
    e.close()
  line=f.readline()
f.close()
g.close()




# | ------------------------- Make the firmware verions ------------------------- |
# This has a lot of stuff coded into the firmware - quite the Elepahnt but it saves significant RAM when pre-compiled
#Open the output file
g=open('microIDE.hx','w')
#add these files to the outputfile as python script
hexify("favicon.svg.gz","favicon.svg",g)         #Favorite Icon for  website to have an icon - svg makes it scalable
hexify("microIDE.html.gz","microIDE.html",g)     #Main HTML file
hexify("microIDE.js.gz","microIDE.js",g)         #Main JS file
hexify("microIDE.css.gz","microIDE.css",g)       #Main CSS file
#hexify("term.js.gz","term.js",g)                #Depreciated webRepl site - support removed in firmware (to save space)
#hexify("chart.js.gz","chart.js",g)               #Chart js - TODO:ADD LINK
#hexify("debug.html.gz","debug.html",g)          #Depreciated Debugsingle page with CSS,JS,& HTML in one
#hexify("board.svg.gz","board.svg",g)            #Picture of Board
hexify("ace.js.gz","ace.js",g)                   #Ace.js editor gzipp
hexify("mode-python.js.gz","mode-python.js",g)   #Ace.js python mode look-nicer

g.close()


#line by line copy the actual file (microIDE_nostatic.py) into microIDE.py + insert the above file when the #<STAICFILEGLAG> is reached
#
# This adds bytecode of the .gz files to the _sendStatic function so it can be compiled into the firmware
#
g=open('microIDE_for_firmwares.py','w')
f=open('microIDE_nostatic.py','r')
line=f.readline()
while len(line)>0:
  g.write(line)
  if '#<STAICFILEGLAG>' in line:
    e=open('microIDE.hx','r')
    line=e.read(8192)
    while len(line)>0:
      g.write(line)
      line=e.read(8192)
    e.close()
  line=f.readline()
f.close()
g.close()
os.system('rm microIDE.hx')

