if arg then
  args = arg
  arg = nil
end
local function main()
  ffi = ffi or require("ffi")
  bit = bit or require("bit")
  def_external()
  def_shader_structs()
  def_images_struct()
  stdLib = {}
  for k, v in pairs(_G) do
    if type(v) ~= "function" then stdLib[k] = true end
  end
  if args[1] == "live" then
    print("LIVE")
    _LIVE = true
    _ASSIGNS = {}
    lib_dir = "../dep/lua/"
  else
    _FUSED = true
    print("INTERNAL")
    local source_txt = LibDeflate:DecompressDeflate(source_lua)

    -- file  = io.open("source.lua","w+")
    -- io.output(file)
    -- io.write(source_txt)

    source = loadstring(source_txt)()
    -- print("Im gay")
    lib_dir = "dep/"
    local source2 = {}

    function fixPaths()
      for k, v in pairs(source) do
        local path = k:gsub("/", ".")
        path = path:gsub(".lua", "")
        path = path:gsub("./", "")
        if path:sub(1, 2) == ".." then path = path:sub(3) end
        source2[path] = v
        -- print("path",path,v:len())
        package.preload[path] = loadstring(v)
      end
      source = source2
    end
    fixPaths()

    local realRequire = _G.require
    function require(file)
      local file = file:gsub("/", ".")
      -- print("require",file)
      if source[file] then
        -- print("IS INTERNAL")
      end
      local init = source[file] and file or (source[file .. ".init"] and file .. ".init" or file)
      -- print(init,(source[init])~=nil,#source[init])
      -- return package.preload[init]
      -- return realRequire(init)
      local ok, res = xpcall(realRequire, debug.traceback, init)
      if ok then
        return res
      else
        error(res)
      end
    end
  end

  local date = os.date("%Y%m%d%H%M%S")

  collectgarbage("stop")

  local function selfGC(func, t)
    func(t)
  end

  function setGC(t, func, mt)
    local d = newproxy(true) -- New userdata
    local m = getmetatable(d) -- Metatable associated with userdata
    m.__gc = function()
      func(t)
    end
    t[d] = true
    if mt then
      for k, v in pairs(mt) do
        m[k] = v
      end
    end
    setmetatable(t, m)
    return t
  end
  PRINT_DESTRUCTORS = false
  print("OS: ", ffi.os)

  if ffi.os == "Windows" then
    WINDOWS = true
    MAC = false
    UNIX = false
    LINUX = false
    os.name = "Windows"
    os.slash = "\\"
    os.rm = "del /f "
    os.copy = "copy "
  elseif ffi.os == "Linux" then
    WINDOWS = false
    UNIX = true
    LINUX = true
    MAC = false
    function os.link(dir1, dir2)
      local cmd = 'ln -s "' .. dir1 .. '" "' .. dir2 .. '"'
      os.execute(cmd)
    end
    os.name = "Linux"
    os.slash = "/"
    os.rm = "rm -rf "
    os.copy = "cp"
  elseif ffi.os == "OSX" then
    WINDOWS = false
    UNIX = true
    MAC = true
    LINUX = false
    function os.link(dir1, dir2)
      local cmd = 'ln -s "' .. dir1 .. '" "' .. dir2 .. '"'
      os.execute(cmd)
    end
    os.name = "MacOS"
    os.slash = "/"
    os.rm = "rm -rf "
    os.copy = "cp "
  end

  time = 0
  pi = 3.14159265359
  math.randomseed(os.time())

  launchWidth = 640
  launchHeight = 480

  ZIP_MODE = false
  VECTOR_TYPE = "float"
  STROKE_TYPE = "float[?][10]"
  SEGMENT_TYPE = "float[?][24]"
  STROKEPT_SIZE = ffi.sizeof("float[10]")
  SEGMENT_SIZE = ffi.sizeof("float[24]")

  empty = {}
  _no_copy = {}

  nothing = function(a, b, c, d, e, f, g, h, i, j, k) end

  io.stdout:setvbuf("no")

  fs = require(lib_dir .. "fs")
  cpml = require(lib_dir .. "cpml/init")
  iqm = require(lib_dir .. "iqm-init")
  lume = require(lib_dir .. "lume")
  anim9 = require(lib_dir .. "anim9")
  ik = require(lib_dir .. "fabrik")
  obj_loader = require(lib_dir .. "objLoader")

  inspect = require(lib_dir .. "inspect")
  ansicolors = require(lib_dir .. "ansicolors")
  fuzzy = require(lib_dir .. "fuzzy")

  print("deps")

  -- if _LIVE then
  --   for name,d in fs.dir("img") do
  --     if name:sub(-4)==".png" then
  --       table.insert(_internal_interface_image_list,name:sub(0,-5))
  --     end
  --   end
  -- end

  print("imgslisted")
  for i, image in ipairs(_internal_interface_image_list) do
    _internal_interface_image_list[image] = true
  end
  print("imgslisthashed")

  require("core/engine")
  require("core/core")
  local textSize = ffi.new("Vector2")

  if EXIT_EARLY then
    print("fail---------------------------------------------------")
    return 0
  else
    print("ok ----------------------------------------------------")
    return 1
  end
end

local ok, err = xpcall(main, debug.traceback)
if not ok then
  print(err)
  os.exit()
end
