#!/bin/lua --[[ Copyright (c) 2026 Alexander Hill Permission to use, copy, modify, and/or 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 liquid = require("liquid") local posix = require("posix") local tinytoml = require("tinytoml") local config local config_path = "/etc/maple.toml" local sysroot = "/" local templates = "/usr/share/mapleconf" local function render_directory(stack) local path if #stack == 0 then path = "/" else path = "/" .. table.concat(stack, "/") .. "/" end local files = posix.dir(templates .. path) for _, entry in ipairs(files) do if not string.match(entry, "^%.+$") then local fullpath = path .. entry local stat = posix.stat(templates .. fullpath) if stat.type == "directory" then if not posix.access(sysroot .. fullpath) then posix.mkdir(sysroot .. fullpath) end local newstack = {} -- Why does Lua lack the ability to copy tables? ~ahill for _, v in ipairs(stack) do table.insert(newstack, v) end table.insert(newstack, entry) render_directory(newstack) elseif stat.type == "regular" then print("Updating " .. fullpath) local template = io.open(templates .. fullpath, "r") if template then local target = io.open(sysroot .. fullpath, "w") if target then local context = liquid.InterpreterContext:new(config) local engine = liquid.Template:parse(template:read("*all")) target:write(engine:render(context)) target:close() end template:close() end else print("BUG: Directory entry type " .. stat.type .. " not recognized.") end end end end -- ENTRY POINT for opt, optarg, optind in posix.unistd.getopt(arg, "c:hr:t:") do if opt == "c" then config_path = optarg elseif opt == "h" then -- Pardon the formatting. This should be cleaned up later. ~ahill print[[Usage: mapleconf [option [option ...] ] -c - Sets the file to source configuration data from -h - Displays this help message -r - Sets the sysroot to configure -t - Sets the path of the template directory]] os.exit(1) elseif opt == "r" then sysroot = optarg elseif opt == "t" then templates = optarg else print("Unknown option: " .. arg[optind - 1]) os.exit(1) end end config = tinytoml.parse(config_path) render_directory{}