--  ========================================================================
--
--  FILE: init.lua
--
--  SERVICES: Initializes the interface, datastore and fetch
--
--  ========================================================================
--  ========================================================================
--
--               Copyright  1999-2006 QUALCOMM Incorporated
--                     All Rights Reserved.
--                   QUALCOMM Proprietary/GTDR
--
--  ========================================================================
--  ========================================================================

-- Package Definition
local P = {}   
script_init = P  -- Package name

setmetatable(P, {__index = _G})

--setting environment to this package
setfenv(1,P)

-- Import Section:

-- Everything is local in file
function start()

	local pcall_err_1, pcall_result_1 = pcall(require, "fetch.lua")
	local pcall_err_2, pcall_result_2 = pcall(require, "datastore.lua")
	local pcall_err_3, pcall_result_3 = pcall(require, "interface.lua")
	
	if pcall_err_1 and pcall_err_2 and pcall_err_3 then
	
		local info = debug.getinfo(1, "Sl")
		print ( string.format("[%s]:%d Calling fetch.lua/datastore.lua/interface.lua",info.short_src, info.currentline) )
	
	    datastore.initNewStore()
	
		local info = debug.getinfo(1, "Sl")
		print ( string.format("[%s]:%d datastore.initNewStore()",info.short_src, info.currentline) )
		
		fetch_init()    
	
		local info = debug.getinfo(1, "Sl")
		print ( string.format("[%s]:%d fetch_init() ",info.short_src, info.currentline) )
	
		interfaceInit()
	
		local info = debug.getinfo(1, "Sl")
		print ( string.format("[%s]:%d interfaceInit()",info.short_src, info.currentline) )
	
	else
	
	    if ( type(pcall_result_1) == "string" ) then
			local info = debug.getinfo(1, "Sl")
			print ( string.format("[%s]:%d ERROR %s",info.short_src, info.currentline, pcall_result_1) )

		end
	    
	    if ( type(pcall_result_2) == "string" ) then
			local info = debug.getinfo(1, "Sl")
			print ( string.format("[%s]:%d ERROR %s",info.short_src, info.currentline, pcall_result_2) )

		end

	    if ( type(pcall_result_3) == "string" ) then
			local info = debug.getinfo(1, "Sl")
			print ( string.format("[%s]:%d ERROR %s",info.short_src, info.currentline, pcall_result_3) )

		end

		return EFAILED
	
	end
	
	return true -- Fetch, DataStore and Interface are not returning error codes in init functions

end
