project (sf_engine)

set(sf_engineInitializeDest "${CMAKE_CURRENT_BINARY_DIR}/include" CACHE INTERNAL "-" FORCE)

set (copyFiles
	sfutil/sf_iph.h
	sfutil/sf_ip.h
	sfutil/sf_ip.c
	sfutil/sfghash.h
	sfutil/sfghash.c
	sfutil/sfhashfcn.h
	sfutil/sfprimetable.h
	sfutil/sfprimetable.c
	sf_types.h
	sf_protocols.h
)

foreach (srcFile ${copyFiles})
	get_filename_component(destFile ${srcFile} NAME)
	set (destFile ${sf_engineInitializeDest}/${destFile})

	add_custom_command(
		OUTPUT ${destFile}
		COMMAND "${CMAKE_COMMAND}" -E make_directory ${sf_engineInitializeDest}
		COMMAND "${CMAKE_COMMAND}" -E copy "${snortSrcRoot}/${srcFile}" "${sf_engineInitializeDest}"
		DEPENDS ${snortSrcRoot}/${srcFile}
	)

	set (generatedFiles ${generatedFiles} ${destFile})
endforeach (srcFile ${copyFiles})

add_custom_command(OUTPUT ${sf_engineInitializeDest}/snort_debug.h
	COMMAND "${CMAKE_COMMAND}" -E make_directory ${sf_engineInitializeDest}
	COMMAND "${sedCommand}" -e 's/DebugMessageFile = /*_ded.debugMsgFile = /' -e 's/DebugMessageLine = /*_ded.debugMsgLine = /' -e 's/; DebugMessageFunc${dollarSign}/; _ded.debugMsg/' -e 's/; DebugWideMessageFunc${dollarSign}/; _ded.debugWideMsg/' '${snortSrcRoot}/snort_debug.h' > \""${sf_engineInitializeDest}/snort_debug.h\""
	DEPENDS ${snortSrcRoot}/snort_debug.h
)

add_custom_command(OUTPUT ${sf_engineInitializeDest}/ipv6_port.h
	COMMAND "${CMAKE_COMMAND}" -E make_directory ${sf_engineInitializeDest}
	COMMAND "${sedCommand}" -e \"s/->iph->ip_src/->ip4_header->source/\" -e \"s/->iph->ip_dst/->ip4_header->destination/\" -e \"s/->iph->/->ip4_header->/\" -e \"s/->iph${dollarSign}${dollarSign}/->ip4_header/\" -e \"s/orig_iph/orig_ip4h/\" -e \"s/ip_verhl/version_headerlength/\" -e \"s/ip_tos/type_service/\" -e \"s/ip_len/data_length/\" -e \"s/ip_id/identifier/\" -e "s/ip_off/offset/" -e \"s/ip_ttl/time_to_live/\" -e \"s/ip_proto/proto/\" -e \"s/ip_csum/checksum/\" \""${snortSrcRoot}/ipv6_port.h\"" > \""${sf_engineInitializeDest}/ipv6_port.h\""
	DEPENDS ${snortSrcRoot}/ipv6_port.h
)

add_custom_command(OUTPUT ${sf_engineInitializeDest}/sfhashfcn.c
	COMMAND "${CMAKE_COMMAND}" -E make_directory ${sf_engineInitializeDest}
	COMMAND "${sedCommand}" -e 's/\\\#ifndef MODULUS_HASH/\\\#ifdef STATIC_HASH/' \""${snortSrcRoot}/sfutil/sfhashfcn.c\"" > \""${sf_engineInitializeDest}/sfhashfcn.c\""
	DEPENDS ${snortSrcRoot}/sfutil/sfhashfcn.c
)

set (generatedFiles ${generatedFiles} ${sf_engineInitializeDest}/snort_debug.h ${sf_engineInitializeDest}/ipv6_port.h ${sf_engineInitializeDest}/sfhashfcn.c)

set_source_files_properties(${generatedFiles} PROPERTIES GENERATED true)

KERIO_TARGET(sf_engine shared)

KERIO_ADD_INCLUDES_DIR(sf_engine ${pcreIncludeDir})
KERIO_ADD_INCLUDES_DIR(sf_engine ..)
KERIO_ADD_INCLUDES_DIR(sf_engine ${sf_engineInitializeDest})
configHIncludeDir(${PROJECT_NAME})

set (sf_engine_SOURCES
	bmh.c
	bmh.h
	sf_snort_detection_engine.c
	sf_snort_detection_engine.h
	sf_snort_packet.h
	sf_snort_plugin_api.c
	sf_snort_plugin_api.h
	sf_snort_plugin_byte.c
	sf_snort_plugin_content.c
	sf_snort_plugin_hdropts.c
	sf_snort_plugin_loop.c
	sf_snort_plugin_pcre.c
	sf_snort_plugin_rc4.c
	sf_decompression.c
	sf_decompression.h
	${generatedFiles}
)

add_definitions(-DSF_SNORT_ENGINE_DLL)
KERIO_ADD_INCLUDES_DIR(sf_engine ${daqIncludeDest})
KERIO_ADD_INCLUDES_DIR(sf_engine ${sf_dynamicInitializeDest})
KERIO_TARGET_DEP(sf_engine sf_dynamic_initialize_lib)
KERIO_TARGET_DONE (sf_engine)

