made mtcfg more generally useful

This commit is contained in:
Izaya 2018-08-12 02:22:49 +10:00
parent 2a2e347663
commit 8f628d7b10
1 changed files with 52 additions and 48 deletions

View File

@ -4,38 +4,29 @@ local computer = require "computer"
local shell = require "shell"
local hostname = os.getenv("HOSTNAME")
local cfgfile = "/etc/minitel.cfg"
local cfg = {}
cfg.debug = false
cfg.port = 4096
cfg.retry = 10
cfg.retrycount = 64
cfg.route = true
cfg.rctime = 15
cfg.pctime = 30
cfg.sroutes = {}
local args, ops = shell.parse(...)
local cfgfile = args[1]
local cfg = {}
local function clear()
io.write("\27[2J\27[H")
end
if cfgfile then -- if a config file argument is specified, load it
local fobj = io.open(cfgfile, "rb")
if fobj then
if not fobj then
print("Error: couldn't open file")
return false
end
cfg = serial.unserialize(fobj:read("*a"))
end
if not hostname then
local fobj = io.open("/etc/hostname","rb")
if fobj then
hostname = fobj:read()
fobj:close()
if not cfg then
print("Error: couldn't unserialize file")
return
end
end
clear()
else -- if not, set the hostname and edit the minitel config file
-- you could just replace this whole block with an error message I guess, but let's default to configuring minitel
cfgfile = "/etc/minitel.cfg"
if not hostname then
print("Hostname not configured.")
hostname = computer.address():sub(1,8)
@ -54,13 +45,29 @@ if not hostname then
event.pull("key_down")
end
if ops.firstrun then
if ops.firstrun then -- if --firstrun, quit now
os.execute("rc minitel enable")
print("Run mtcfg to configure advanced settings.")
return
return false
end
local keytab = {}
cfg.debug = false -- some default settings
cfg.port = 4096
cfg.retry = 10
cfg.retrycount = 64
cfg.route = true
cfg.rctime = 15
cfg.pctime = 30
cfg.sroutes = {}
local fobj = io.open(cfgfile, "rb") -- attempt to replace the default settings
if fobj then
cfg = serial.unserialize(fobj:read("*a")) or cfg
fobj:close()
end
end
local keytab = {} -- contains the keys because we don't want to work with the table indices directly
for k,v in pairs(cfg) do
if type(v) ~= "table" then
keytab[#keytab+1] = k
@ -101,7 +108,7 @@ local function editsetting(k)
end
end
while run do
while run do -- main loop
drawmenu()
local _,_, ch, co = event.pull("key_down")
if ch == 113 and co == 16 then
@ -139,6 +146,3 @@ if fobj then
fobj:close()
print("Settings successfully written!")
end
print("Enabling daemon...")
os.execute("rc minitel enable")