local cps,
      harel,
      hareltableparser
	  = ...
      
local verify

function verify(continuation, root, fileName, parent, fileParent)
	-- if fileParent then print(harel.getQualifiedName(fileParent)) end
	return json.load(
		function(data, error)
			if not data then
				print(fileName..":\n"..error)
				return continuation()
			end

			local stateTable, builtStates, error = hareltableparser.parse(root, fileName, parent, fileParent, data)
			if not stateTable then
				print(fileName..":\n"..error)
				return continuation()
			else
				-- filter output states
				if fileParent and fileParent.outputs then
					for k in pairs(fileParent.outputs) do builtStates[k] = nil end
				end

				return cps.parallelApply(
											nil,
											continuation,
											function(_, c, _, v)
												if v.type == "external" and v.external == v then
													return verify(c, root, v.file, v.parent, v)
												else
													return c()
												end
											end,
											pairs(builtStates)
										)
			end
		end,
		fileName)
end

local function run(continuation, exeName, fileName)
    if not fileName then
        print("usage: "..exeName.." fileName")
        return continuation()
    else
        return verify(continuation, getpath(fileName), fileName)
    end
end

return run
