# include guard
if (NOT REG_EXT_LIB)
	set (REG_EXT_LIB 1)
	
	#
	#    EXTERNAL LIBRARY COMPILATION WORKFLOW:
	#
	# in every library subdirectory there are three additional files:
	#    src/configureHook.sh
	#    src/makeHook.sh
	#    src/CVMFS-CMakeLists.txt
	#
	# 1. the bootstrap.sh script has to decompress the library archive
	#    usually it is copied into the $CMAKE_BINARY_DIR afterwards, to
	#    do an out of source build.
	#
	# 2. external libraries usually bring their own build system
	#    if the library is opted for static linkage, the CVMFS-CMakeLists.txt
	#    will be included and prepares the configure, build and linkage
	#    information
	#
	# 3. while configuring CVMFS with CMake all statically linked
	#    external libraries get their configureHook shellscript invoked
	#    at some point, to start their configuration steps
	#
	# 4. the build system finds out about needed external libraries
	#    and invokes their makeHook shellscript to start the build of
	#    this external dependency
	
	

	# this custom macro registers an external library for building
	#	
	# first it executes the configureHook.sh script of the library to do some
	# configuration work (this is done while configuring the CMake stuff)
	#
	# also it registers a custom target which main targets can depend
	# upon in order to control the external library to build while building
	macro (register_external_lib NAME LOCATION ) # OUTPUTFILE is an additional optional parameter
	
		if (${ARGC} EQUAL 3) # some libraries may not have an output file... they are only configured
		
			# create a build hook (invoked while compiling)
			add_custom_command (
				OUTPUT				${ARGV2}
				COMMAND				sh
				ARGS				"${LOCATION}/makeHook.sh"
				WORKING_DIRECTORY	${LOCATION}
				COMMENT				"Building ${NAME}..."
			)
			add_custom_target (${NAME} ALL DEPENDS ${ARGV2})
		endif (${ARGC} EQUAL 3)

		# configure the library before generating the final build files
		if (NOT EXISTS ${LOCATION}/.external_is_configured)
			message (STATUS "configuring external library: ${NAME} ...")
			execute_process (
				COMMAND				sh ${LOCATION}/configureHook.sh
				WORKING_DIRECTORY	${LOCATION}
				RESULT_VARIABLE     RETVAL
			)
			if (NOT ${RETVAL} EQUAL 0)
				message (FATAL_ERROR "failed to configure ${NAME}. Aborting.")
			endif (NOT ${RETVAL} EQUAL 0)
			file (WRITE ${LOCATION}/.external_is_configured "no coffee break needed...")
		endif (NOT EXISTS ${LOCATION}/.external_is_configured)

	endmacro (register_external_lib)
	
endif (NOT REG_EXT_LIB)
