#!/usr/bin/env lua

--
--  Copyright (c) 2018 Caspar Schutijser <caspar.schutijser@sidn.nl>
-- 
--  Permission to use, copy, modify, and distribute this software for any
--  purpose with or without fee is hereby granted, provided that the above
--  copyright notice and this permission notice appear in all copies.
-- 
--  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
--  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
--  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
--  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
--  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
--  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
--  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--

local nm = require "nm"

local signal = require "posix.signal"

function shutdown(signum)
	print("Done, exiting\n")
	nm.shutdown()
	os.exit(128+signum)
end

if not arg[1] then
	io.stderr:write[[
Usage: ./stdin_nm.lua db_file
]]
	os.exit(1)
end

nm.initialize(false, arg[1])

signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGKILL, shutdown)

for line in io.lines() do
	nm.handle_msg(line)
end

nm.shutdown()

