#!/opt/lnxall_app/bin/lua

-- Created by jiaqiang.ye@lnxall.com
-- Simple reset-cause for non-openwrt systems
local hal = require "lnxhal"

local g_SOFTRST         = "SoftRst"
local g_UNKNOWN         = "Unknown"
local g_POWERONRST      = "PowerOnRst"
local g_WATCHDOGRST     = "WatchdogRst"

local function mainfunc()
	if hal.hw_has(hal.HWCAP_RESET_CAUSE) ~= 1 then
		io.stderr:write(string.format(
			'Warning, unsupported system hardware.\n%s\n', g_UNKNOWN))
		io.stdout:flush()
		return false
	end

	local cause = hal.reset_cause()
	if cause == nil then
		io.stderr:write(string.format(
			'Error, failed to query reset cause\n%s\n', g_UNKNOWN))
		io.stderr:flush()
		return false
	end

	if cause == hal.RESET_CAUSE_POWER_ON then
		io.stdout:write(g_POWERONRST .. '\n')
	elseif cause == hal.RESET_CAUSE_SOFTWARE then
		io.stdout:write(g_SOFTRST .. '\n')
	elseif cause == hal.RESET_CAUSE_WATCHDOG then
		io.stdout:write(g_WATCHDOGRST .. '\n')
	else
		io.stdout:write(g_UNKNOWN .. '\n')
	end

	io.stdout:flush()
	return true
end

mainfunc()
os.exit(0)
