#!/bin/lua

if arg[1] == "-c" then
    if arg[2] then
        os.exit(os.execute(arg[2]))
    else
        print("sh: 0: -c requires an argument")
        os.exit(2)
    end
elseif arg[1] then
    local ok, status = true, nil
    local file = io.read(arg[1])
    if file ~= "" then
        local cmds = string.split(file, "\n")
        for _,cmd in pairs(cmds) do
            ok, status = pcall(os.execute, cmd)
            if not ok then
                print(tostring(status))
                os.exit(1)
            elseif status ~= 0 then
                break
            end
        end
    end

    os.exit(status)
end

if arg[0] == "/bin/sh" then
    pcall(graphics.SetText, io.stdout, "")
    print(string.env(io.read("/etc/motd")))
end
