login

local lud = require "ludweb"

local cogit = require "cogit.cogit"
local hash = require "cogit.hash"

local app = cogit.new_cogit(unpack(arg))

local auth_path = arg[1].."/.login"
local auth_file = io.open(auth_path)
local s, h
if auth_file ~= nil then
    app:log(2, "reading login info from "..auth_path)
    local auth = auth_file:read()
    s, h = auth:match("([%a%d+/=]+):([%a%d+/=]+)")
else
    local pass = hash.get_pass("admin password: ")
    s = hash.get_salt()
    h = hash.hash_pass(pass, s)
    s = lud.crypt.b64_enc(s)
    h = lud.crypt.b64_enc(h)
    io.stderr:write("save login info? Y/[n]: ")
    if io.read() == "Y" then
        auth_file = io.open(auth_path, "w")
        if auth_file ~= nil then
            app:log(2, "writing login info to "..auth_path)
            auth_file:write(s..":"..h.."\n")
            auth_file:close()
        else
            app:log(1, "could not write login info to "..auth_path)
        end
    end
end
app:set_password(s, h)

app:run()