xref: /freebsd/release/packages/generate-ucl.lua (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1#!/usr/libexec/flua
2
3--[[ usage:
4generare-ucl.lua [<variablename> <variablevalue>]... <sourceucl> <destucl>
5
6In the <destucl> files the variable <variablename> (in the form ${variablename}
7in the <sourceucl>) will be expanded to <variablevalue>.
8
9The undefined variables will reamin unmofifier "${variablename}"
10]]--
11
12local ucl = require("ucl")
13
14if #arg < 2 or #arg % 2 ~= 0 then
15	io.stderr:write(arg[0] .. ": expected an even number of arguments, got " .. #arg)
16	os.exit(1)
17end
18
19local parser = ucl.parser()
20for i = 2, #arg - 2, 2 do
21	parser:register_variable(arg[i - 1], arg[i])
22end
23local res,err = parser:parse_file(arg[#arg - 1])
24if not res then
25	io.stderr:write(arg[0] .. ": fail to parse("..arg[#arg - 1].."): "..err)
26	os.exit(1)
27end
28local f,err = io.open(arg[#arg], "w")
29if not f then
30	io.stderr:write(arg[0] .. ": fail to open("..arg[#arg].."): ".. err)
31	os.exit(1)
32end
33local obj = parser:get_object()
34f:write(ucl.to_format(obj, 'ucl'))
35f:close()
36