include(CMakeParseArguments)

pacmake_include(download_package)
pacmake_include(package_property)
pacmake_include(build_package)
pacmake_include(default_version)
pacmake_include(log)

#pacmake_include_package(name)
function(pacmake_include_package name)
	set(dir "${PACMAKE_BASEDIR}/package/${name}")
	if(IS_DIRECTORY ${dir} AND EXISTS "${dir}/package.cmake")
		include("${dir}/package.cmake")
	endif()
endfunction(pacmake_include_package)

#pacmake_add_package(name [STATIC|SHARED] VERSION version)
function(pacmake_add_package)
	cmake_parse_arguments(args "STATIC;SHARED" "VERSION" "" ${ARGN})
	
	list(LENGTH args_UNPARSED_ARGUMENTS unparsed_length)
	if(NOT ${unparsed_length} EQUAL 1)
		pacmake_log(ERROR "pacmake_add_package: Invalid arguments.")
		message(FATAL_ERROR)
	endif()
		
	set(args_NAME "${args_UNPARSED_ARGUMENTS}")
	if(${args_STATIC})
		set(args_TYPE STATIC)
	elseif(${args_SHARED})
		set(args_TYPE SHARED)
	endif()
	
	if(NOT args_NAME)
		pacmake_log(ERROR "pacmake_add_package: No name specified")
		message(FATAL_ERROR)
	endif()
	
	list(FIND PACMAKE_PACKAGE_LIST ${args_NAME} package_index)
	if(${package_index} LESS 0)
		pacmake_log(INFO "pacmake_add_package(${args_NAME}): Including package.")
		pacmake_include_package(${args_NAME})
		
		list(FIND PACMAKE_PACKAGE_LIST ${args_NAME} package_index)
		if(${package_index} LESS 0)
			pacmake_log(ERROR "pacmake_add_package(${args_NAME}): Package could not be found.")
			message(FATAL_ERROR)
		endif()
	endif()
	
	if(NOT args_VERSION)
		pacmake_get_default_version(${args_NAME} args_VERSION)
		pacmake_log(INFO "pacmake_add_package(${args_NAME}): No version specified, using default.")
	endif()
	if(NOT args_VERSION)
		pacmake_log(ERROR "pacmake_add_package(${args_NAME}): No default version set.")
		message(FATAL_ERROR)
	endif()
	
	pacmake_get_package_property(GENERIC ${args_NAME} ${args_VERSION} REGISTERED is_registered)
	if(NOT is_registered)
		pacmake_log(ERROR "pacmake_add_package(${args_NAME}, ${args_VERSION}): Version could not be found.")
		message(FATAL_ERROR)
	endif()
		
	if(NOT args_TYPE)
		pacmake_log(INFO "pacmake_add_package(${args_NAME}): No library type specified, using default(${PACMAKE_DEFAULT_LIBRARY_TYPE}).")
		set(args_TYPE ${PACMAKE_DEFAULT_LIBRARY_TYPE})
	endif()
	
	pacmake_get_package_property(GENERIC ${args_NAME} ${args_VERSION} DEPENDENCIES deps)
	foreach(dep IN ITEMS ${deps})
		pacmake_log(INFO "pacmake_add_package(${args_NAME}, ${args_VERSION}): Depends on ${dep}, running pacmake_add_package.")		
		pacmake_add_package(${dep})
		
		pacmake_get_package_property(GENERIC ${dep} ${PACMAKE_PACKAGE_VERSION_${dep}} CONFIG_PATH dep_config)
		pacmake_get_package_property(GENERIC ${dep} ${PACMAKE_PACKAGE_VERSION_${dep}} CMAKE_ARGS_CONFIG_PATH dep_config_args)
		list(APPEND dep_config_args "-D${dep}_DIR=${dep_config}")
		
		pacmake_get_package_property(GENERIC ${args_NAME} ${args_VERSION} CMAKE_ARGS_CONFIG_PATH config_args)
		foreach(arg IN ITEMS ${dep_config_args})
			list(FIND config_args "${arg}" prefix_index)
			if(${prefix_index} LESS 0) # add dependency config path if not already added
				list(APPEND config_args ${arg})
				pacmake_set_package_property(GENERIC ${args_NAME} ${args_VERSION} CMAKE_ARGS_CONFIG_PATH ${config_args})
			endif()
		endforeach()
	endforeach()
	
	pacmake_download_package(${args_NAME} ${args_VERSION} dir)
	pacmake_build_package(${args_NAME} ${args_VERSION} ${dir} ${args_TYPE})
	
	pacmake_log(INFO "pacmake_add_package(${args_NAME}, ${args_VERSION}): Running find_package.")
	find_package(${args_NAME} REQUIRED NO_DEFAULT_PATH) #package_DIRs have already been set by build_package
endfunction(pacmake_add_package)
