made mtcfg significantly better, now actually saves if not firstrun

This commit is contained in:
Izaya 2019-04-21 14:39:20 +10:00
parent 7ed7452428
commit 8298c21124
1 changed files with 46 additions and 48 deletions

View File

@ -5,8 +5,18 @@ local shell = require "shell"
local hostname = os.getenv("HOSTNAME") local hostname = os.getenv("HOSTNAME")
local args, ops = shell.parse(...) local args, ops = shell.parse(...)
local cfgfile = args[1] local cfgfile = args[1] or "/etc/minitel.cfg"
local firstrun = false
local cfg = {} local cfg = {}
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 function clear() local function clear()
io.write("\27[2J\27[H") io.write("\27[2J\27[H")
@ -20,30 +30,33 @@ local function writecfg()
print("Settings successfully written!") print("Settings successfully written!")
end end
end end
local function loadcfg(fn)
if cfgfile then -- if a config file argument is specified, load it local fobj = io.open(fn, "rb")
local fobj = io.open(cfgfile, "rb")
if not fobj then if not fobj then
print("Error: couldn't open file") print("Error: couldn't open config file")
return false return false
end end
cfg = serial.unserialize(fobj:read("*a")) ncfg = serial.unserialize(fobj:read("*a"))
fobj:close() fobj:close()
if not cfg then if not ncfg then
print("Error: couldn't unserialize file") print("Error: couldn't unserialize config file")
return return false
end end
else -- if not, set the hostname and edit the minitel config file for k,v in pairs(ncfg) do
-- you could just replace this whole block with an error message I guess, but let's default to configuring minitel cfg[k] = v
cfgfile = "/etc/minitel.cfg" end
if not hostname then return true
print("Hostname not configured.") end
hostname = computer.address():sub(1,8)
io.write("New hostname? ["..hostname.."] ") firstrun = not loadcfg(cfgfile)
local nhostname = io.read()
if nhostname:len() > 0 then if not hostname then
hostname = nhostname print("Hostname not configured. If not set, the hostname will default to the first 8 characters of the computer's address, shown below.")
end hostname = computer.address():sub(1,8)
io.write("New hostname? ["..hostname.."] ")
local nhostname = io.read()
if nhostname:len() > 0 then
hostname = nhostname
local fobj = io.open("/etc/hostname","wb") local fobj = io.open("/etc/hostname","wb")
if fobj then if fobj then
fobj:write(hostname) fobj:write(hostname)
@ -51,35 +64,20 @@ else -- if not, set the hostname and edit the minitel config file
end end
os.execute("hostname --update") os.execute("hostname --update")
print("Hostname set to "..hostname..". Press any key to continue.") print("Hostname set to "..hostname..". Press any key to continue.")
event.pull("key_down")
end end
end
cfg.debug = false -- some default settings if firstrun then -- if first run, quit now
cfg.port = 4096 io.write("Should this machine route packets?\nThis may cause issues in dense wireless networks.\n\nRoute packets? [Y/n]: ")
cfg.retry = 10 local rp = io.read():lower():sub(1,1)
cfg.retrycount = 64 if rp == "n" then
cfg.route = true cfg.route = false
cfg.rctime = 15
cfg.pctime = 30
cfg.sroutes = {}
if ops.firstrun then -- if --firstrun, quit now
io.write("Should this machine route packets?\nThis should be disabled on large networks.\n\nRoute packets? [Y/n]: ")
local rp = io.read():lower():sub(1,1)
if rp == "n" then
cfg.route = false
end
os.execute("rc minitel enable")
writecfg()
print("Run mtcfg to configure advanced settings.")
return false
end
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
os.execute("rc minitel enable")
writecfg()
print("Your hostname is "..hostname..".")
print("Run mtcfg to configure advanced settings.")
return false
end end
local keytab = {} -- contains the keys because we don't want to work with the table indices directly local keytab = {} -- contains the keys because we don't want to work with the table indices directly
@ -153,5 +151,5 @@ if not config then
return return
end end
print("Writing settings...") print("Writing settings for host "..hostname.."...")
writecfg()