#!/usr/bin/lua local cjson = require "cjson" local invoker = require "invoker" local gfmt = string.format local function fix_func(arg1) if not arg1 then arg1 = "UNKNOWN" end if not invoker.statfile("/opt/lnxall_app/bin/sdmount") then local _, mach = invoker.invoke(invoker.OUTPUT, "uname", "-a") if type(mach) == "string" and string.find(mach, "armv7l", 1, true) then if invoker.invoke(0, "bash", "-c", "cd /tmp && chmod 755 sdmount && mv -v -f sdmount /opt/lnxall_app/bin/") ~= 0 then io.stderr:write("Error, failed to copy: sdmount\n") io.stderr:flush() return false end else io.stderr:write("Error, file not found: sdmount\n") io.stderr:flush() return false end end local okay, output = invoker.invoke(invoker.OUTPUT, "pidof", "sdmount") if okay == 0 and type(output) == "string" and #output > 0 then io.stderr:write(gfmt("Warning, sdmount running happily: %s\n", output)) io.stderr:flush() return false end if invoker.glob("/opt/lnxall_app/app/upstart/applist/*sdmount.json") then io.stderr:write("Warning, sdmount found in applist!\n") io.stderr:flush() return false end local applist, jdat = "/app/config/lnxall_applist.json", nil local cfg = invoker.readfile(applist) if not cfg then io.stderr:write(gfmt("Error, file cannot read: %s\n", applist)) io.stderr:flush() return false end okay, jdat = pcall(cjson.decode, cfg) if not okay or type(jdat) ~= "table" or type(jdat.applist) ~= "table" then io.stderr:write(gfmt("Error, file cannot decode: %s\n", applist)) io.stderr:flush() return false end local apptab = jdat.applist local idx, found = 0, nil while idx < 100 do idx = idx + 1 local entry = apptab[idx] if type(entry) == "table" and entry.name == "sdmount" then found = true break end end if found then io.stdout:write(gfmt("Warning, sdmount found in %s\n", applist)) io.stdout:flush() return false end idx = #apptab apptab[idx + 1] = { ["name"] = "sdmount", ["path"] = "/opt/lnxall_app/bin/sdmount", ["restart"] = true, ["nostdio"] = false } invoker.invoke(0, "cp", "-v", "-f", applist, applist .. ".old") local fp = io.open(applist, "wb") if fp then fp:write(cjson.encode(jdat)) fp:write("\n") fp:close(); fp = nil end invoker.setenv("PATH", "/opt/lnxall_app/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin") invoker.invoke(0, "bash", "-c", "exec nothup /opt/lnxall_app/bin/sdmount") end os.exit(fix_func(arg[1]) and 0 or 1)