FOREACH(i @TOOLS_EXECUTABLES@)
	FILE(COPY @CMAKE_RUNTIME_OUTPUT_DIRECTORY@/${i}
		   DESTINATION @BALLAXY_TOOLS_DIR@/bin)
ENDFOREACH()

FILE(MAKE_DIRECTORY @BALLAXY_TOOLS_DIR@/config)

FOREACH(BALLAXY_TOOL @TOOLS_EXECUTABLES@)
	EXECUTE_PROCESS(COMMAND @BALLAXY_TOOLS_DIR@/bin/${BALLAXY_TOOL} -write_par @BALLAXY_TOOLS_DIR@/config/${BALLAXY_TOOL}_tmp.xml OUTPUT_QUIET ERROR_QUIET)
	EXECUTE_PROCESS(COMMAND @BALLAXY_TOOLS_DIR@/bin/GalaxyConfigGenerator -i @BALLAXY_TOOLS_DIR@/config/${BALLAXY_TOOL}_tmp.xml -o @BALLAXY_TOOLS_DIR@/config/${BALLAXY_TOOL}.xml OUTPUT_QUIET ERROR_QUIET)
	FILE(REMOVE @BALLAXY_TOOLS_DIR@/config/${BALLAXY_TOOL}_tmp.xml)
ENDFOREACH()

FILE(GLOB XML_FILES "@BALLAXY_TOOLS_DIR@/config/*.xml")

SET(TOOL_CATEGORIES)

FOREACH(i ${XML_FILES})
	FILE(READ ${i} XML_CONTENT)
	STRING(REGEX MATCH "[ \t]*<!--Proposed Tool Section: [^>]*" result "${XML_CONTENT}")
	STRING(REGEX REPLACE ".*\\[(.*)\\].*" \\1 result ${result})
	IF (NOT result)
		SET(result "DEFAULT")
	ENDIF()
	LIST(APPEND ${result}_TOOL_LIST ${i})
	LIST(APPEND TOOL_CATEGORIES ${result})
ENDFOREACH()

SET(TOOL_DEFINITION)
LIST(REMOVE_DUPLICATES TOOL_CATEGORIES) 
FOREACH(category ${TOOL_CATEGORIES})
	SET(TOOL_DEFINITION "${TOOL_DEFINITION} \t<section name=\"${category}\" id=\"${category}\">")
	FOREACH(tool ${${category}_TOOL_LIST})
		GET_FILENAME_COMPONENT(tool_basename ${tool} NAME)
		SET(TOOL_DEFINITION "${TOOL_DEFINITION}\n\t\t<tool file=\"BALL/${tool_basename}\"/>")
	ENDFOREACH()
	SET(TOOL_DEFINITION "${TOOL_DEFINITION}\n\t</section>\n\n")
ENDFOREACH()

FILE(WRITE "@BALLAXY_TOOLS_DIR@/tool_conf.xml.section" ${TOOL_DEFINITION})

