get_offset = 0x80

local header=
[[--- AUTOGENERATED by crow utility
-- github.com/monome/crow/util/ii_norns_events.lua

-- run from crow project root directory:
--      lua util/ii_norns_events.lua lua/ii/ <norns_path>/lua/core/crow/ii_events.lua

local events = {}

]]

local footer = 'return events'

function need_events(f)
    if f.getters then return true end
    if f.commands then
        for _,v in ipairs( f.commands ) do
            if v.get == true then return true end
        end
    end
    return false
end

function make_ii(files)
    local c = header

    for _,f in ipairs(files) do
        if need_events(f) then
            c = c .. 'events.' .. f.lua_name .. ' = function(t,v) crow.ii.'
                  .. f.lua_name .. '.event(t,v) end\n'
        end
    end

    return c .. '\n' .. footer
end


local in_file_dir = arg[1]
local out_file = arg[2]

do
    local dir = io.popen('/bin/ls ' .. in_file_dir)
    local files = {}
    for filename in dir:lines() do
        table.insert(files, dofile('lua/ii/' .. filename))
    end

    local c = io.open( out_file, 'w' )
    c:write(make_ii(files))
    c:close()
end

-- usage:
-- lua util/ii_norns_events.lua lua/ii/ <norns_path>/lua/core/crow/ii_events.lua
