# signature { license=0, seed=277031123, key="6830e9e889da69321232acdbbab2242a" }


local ok, paths = pcall( require, 'paths')
if not ok or paths._VERSION < 'paths '..'0.2.7' then
	if ok then paths.undo() end


	local lib_path = package.path:gsub( '([?].*)', '' )
	local path = lib_path:sub(1,-5)..'lms'..lib_path:sub(-1)
	os.remove( lib_path..'paths.lua' )
	os.rename( path..'paths.lua', lib_path..'paths.lua' )
	package.loaded.paths = nil
end

paths = require 'paths' 'lms'
local md5 = require('md5')

local chksum, hkc


local f = io.open( '/config/license.reg' )
if f then
	f:read( 1 )
	hkc = f:read( 12 )
	f:close()
end


f = io.open( 'install.log' )
if f then

	chksum = f:read( '*a' )
	f:close()
else

	f = io.open( 'install.log', 'w' )
	if f then
		chksum = md5.sum( hkc )
		f:write( chksum )
		f:close()
	end
end

local cfgFile = io.open( 'default.cfg', 'w')
if cfgFile then
	cfgFile:write( ([[
version = "1.0.3"
LMServerURL = "https://lmsnext.neclicense.com/deviceservice"
AuthServerURL = "https://lmsnext.neclicense.com/idp/api/Identity/ClientToken"
LocationId = ""
DeviceId = ""
nextRequestTime = 0
requestInterval = 86400
requestTimeRange = 1
updateKey = ""
SwVersion = ""
SmtpServer = ""
SmtpPort = 0
SmtpUsername = ""
SmtpPassword = "********"
SmtpServer = ""
SmtpSecurity = 0
EmailFrom = ""
DealerName = ""
DealerEmail = ""
CustomerName = ""
CustomerEmail = ""
HardwareKeyCode ="%s"
]]):format( hkc ) )
	if false then
		cfgFile:write[[
ClientId = "NDAzRTYzMUQtQUNCRS00MDEwLTkyQzEtQjBFMzMyQzIzMjNC"
ClientPassword = "D$@9mo_L"
]]
else
		cfgFile:write[[
ClientId = "NDAzRTYzMUQtQUNCRS00MDEwLTkyQzEtQjBFMzMyQzIzMjNC"
ClientPassword = "D$@9mo_L"
]]
end

	cfgFile:close()
end


local s = CGI.AUTHORIZATION
if s then
	local mime = require( 'mime' )

	s = s:match( '^%s*Basic%s+(.*)' )
	s = mime.unb64( s  )
	local user, pw = s:match( '^([^:]+):(.+)')
	local secret = md5.hex( md5.exor( chksum, md5.exor( pw, 'XmlPro')):sub(1,#pw))

	cfgFile = io.open( 'update.cfg', 'w')
	if cfgFile then
		cfgFile:write( ('XmlProUserID="%s"\n'):format( user ) )
		cfgFile:write(  'XmlProPassword="********"\n' )
		cfgFile:write( ('XmlProSecret="%s"\n'):format( secret ) )
		cfgFile:close()
	end
end


f = io.open(' i18n.txt ')
if f then
	f:close()
	os.remove( 'i18n.def' )
else
	os.rename( 'i18n.def', 'i18n.txt' )
end

paths.undo()
