local username = arg[1] or _G.loggedInUser
local passwdData = settings.get("passwd")

if passwdData[username] == nil then
	error("passwd: user '" .. username .. "' does not exist")
end

local crypto = require("ccryptolib")
crypto.random.initWithTiming()

print("Changing password for " .. username .. ".")
write("New password: ")
local pwd1 = crypto.sha512.digest(read(""))
write("Retype new password: ")
local pwd2 = crypto.sha512.digest(read(""))

if pwd1 ~= pwd2 then
	error("Sorry, passwords do not match.")
end

if pwd1 == passwdData[username] then
	error("The password has not been changed.")
end

passwdData[username] = crypto.util.toHex(pwd1)
settings.set("passwd", passwdData)
settings.save()
print("passwd: password updated successfully")