#!/usr/bin/luaweb

json = require("json")
omg = require("omg")

eepFile="/data/eeprom"
log=false

function execute(cmd, raw)
    --print(cmd)
    return os.execute(cmd)
end

function eepToTable(file)
    local s=""
    local t={}
    local line

    if not omg.fileExists(file) then 
        return t 
    end

    for line in io.lines(file) do 
        local k,v
        for k, v in string.gmatch(line, "([^%s]-)=([^%s$]*)") do
            t[k] = v
        end
    end

    return t
end

function dumpEepHuman(file)
    t=eepToTable(file)
    for k,v in pairs(t) do
        print(k .. "=" .. v)
    end
end

function dumpEepJson(file)
    t=eepToTable(file)
    local jsonString=json.encode(t)
    io.write(jsonString .. "\n")
end

function readEepOld(file)
    t=eepToTable(file)
    local k,v
    for k,v in pairs(t) do
        io.write(k .. "=" .. v .. " ")
    end
    print("EEPROM=OK")
end

function writeEepOld(file,t)
    local writer = io.open(file, "w")
    if writer ~= nil then
        local k,v
        for k,v in ipairs(t) do
            if string.sub (v, 1 , 1) ~= "-" then
                writer:write(v .. "\n")
            end
        end
        writer:close()
    end
end

function writeEepStreamOld(file)
    local line
    local writer = io.open(file, "w")
    if writer ~= nil then
        for line in io.lines() do 
            local k,v
            for k, v in string.gmatch(line, "([^%s]-)=([^%s$]*)") do
                if string.sub (k, 1 , 1) ~= "-" then
                    writer:write(k .. "=" .. v .. "\n")
                end
            end
        end
        writer:close()
    end
end

function writeEepStreamJson(file)
    local s=""
    for line in io.lines() do 
        s = s .. line
    end
    local j = json.decode(s)
    if j~=nil then
        local writer = io.open(file, "w")
        if writer ~= nil then
            local k,v
            for k,v in pairs(j) do
                writer:write(k .. "=" .. v .."\n")
            end
            writer:close()
        end
    end
end

function setEepOld(file,t)
    if t[1] and t[2] then
        local eep=eepToTable(file)
        eep[t[1]]=t[2]
        local line
        local writer = io.open(file, "w")
        if writer ~= nil then
            local k,v
            for k, v in pairs(eep) do
                writer:write(k .. "=" .. v .. "\n")
            end
            writer:close()
        end
    end
end

function getEepOld(file,t)
    if t[1] then
        local eep=eepToTable(file)
        for k, v in pairs(eep) do
            if t[1] == k then
                print(v)
                break
            end
        end
    end
end

function removeEepOld(file,t)
    if t[1] then
        local eep=eepToTable(file)
        eep[t[1]]=nil
        local line
        local writer = io.open(file, "w")
        if writer ~= nil then
            local k,v
            for k, v in pairs(eep) do
                writer:write(k .. "=" .. v .. "\n")
            end
            writer:close()
        end
    end
end

--******************************************************

_,_,myName=string.find(arg[0],"([^/]-)$")
--print(myName)

options=omg.splitCommandlineString(arg)

if options.dump and options.json then
    dumpEepJson(eepFile)
elseif options.dump then
    dumpEepHuman(eepFile)

elseif options.read and options.json then
    dumpEepJson(eepFile)
elseif options.read or myName=="rd_eep" then
    readEepOld(eepFile)

elseif options.write and options.json then
    writeEepStreamJson(eepFile)
elseif (options.write and arg[2]~=nil) or (myName=="wr_eep" and arg[1]~=nil) then
    writeEepOld(eepFile,arg)
elseif options.write or myName=="wr_eep" then
    writeEepStreamOld(eepFile)

elseif options.set or myName=="set_eep" then
--print(eepFile)
    setEepOld(eepFile,arg)
elseif options.get or myName=="get_eep" then
    getEepOld(eepFile,arg)
elseif options.remove or myName=="rm_eep" then
    removeEepOld(eepFile,arg)
elseif false then
    print("params:")
    for k,v in ipairs(arg) do
        print(k,v)
    end
end

