#
# Shrew Soft VPN / IKE Tools
# Cross Platform Make File
#
# author : Matthew Grooms
#        : mgrooms@shrew.net
#        : Copyright 2007, Shrew Soft Inc
#

project( IKE )

#
# Unix Target Constants
#

add_definitions( -DUNIX )

include( CheckCSourceCompiles )
include( CheckLibraryExists )

set(
	RELVER
	"2.0.3" )

subdirs(
	source/iked
	source/libike
	source/libip
	source/libith
	source/liblog
	source/libpfk )

set(
	SEARCH_INC
	/usr/include
	/usr/local/include )

set(
	SEARCH_LIB
	/usr/lib
	/usr/local/lib )

set(
	SEARCH_BIN
	/usr/local/bin
	/usr/pkg/bin
	/usr/bin )

set(
	SEARCH_SYS
	/usr/local
	/usr/share
	/usr )

#
# Path Option Checks
#

if( NOT EXISTS ${CMAKE_INSTALL_PREFIX} )

	set(
		CMAKE_INSTALL_PREFIX
		"/usr" )

endif( NOT EXISTS ${CMAKE_INSTALL_PREFIX} )

message(
	STATUS 
	"Using install prefix ${CMAKE_INSTALL_PREFIX} ..." )

# etc path

if( ETCDIR )

	set(	PATH_ETC
		${ETCDIR} )

else( ETCDIR )

	if( EXISTS ${CMAKE_INSTALL_PREFIX}/etc )

		set(	PATH_ETC
			"${CMAKE_INSTALL_PREFIX}/etc" )

	else( EXISTS ${CMAKE_INSTALL_PREFIX}/etc )
		
		set(	PATH_ETC
			"/etc" )

	endif( EXISTS ${CMAKE_INSTALL_PREFIX}/etc )

endif( ETCDIR )

message(
	STATUS
	"Using etc path ${PATH_ETC} ..." )

# man path

if( MANDIR )

	set(	PATH_MAN
		${MANDIR} )

else( MANDIR )

	find_path(
		PATH_MAN
		NAMES "man"
		PATHS ${SEARCH_SYS}
		NO_DEFAULT_PATH )

	if( PATH_MAN )

		set(	PATH_MAN
			"${PATH_MAN}/man" )

	else( PATH_MAN )

		set(	PATH_MAN
			"${CMAKE_INSTALL_PREFIX}/man" )

	endif( PATH_MAN )

endif( MANDIR )

message(
	STATUS
	"Using man path ${PATH_MAN} ..." )

if( KRNINC )

	set(
		INC_KERNEL_DIR
		${KRNINC} )

	message(
		STATUS
		"Using kernel include path ${INC_KERNEL_DIR} ..." )

endif( KRNINC )

#
# PreRequisite Checks
#

find_path(
	PATH_INC_CRYPTO
	NAMES "openssl/evp.h"
	PATHS ${SEARCH_INC} )

if( NOT PATH_INC_CRYPTO )

	message(
		FATAL_ERROR
		"Unable to locate openssl crypto include files" )

endif( NOT PATH_INC_CRYPTO )

find_library(
	PATH_LIB_CRYPTO
	NAMES "crypto"
	PATHS ${SEARCH_LIB} )

if( NOT PATH_LIB_CRYPTO )

	message(
		FATAL_ERROR
		"Unable to locate openssl crypto library file" )

endif( NOT PATH_LIB_CRYPTO )

find_package( Threads )

if( NOT CMAKE_USE_PTHREADS_INIT )

	message(
		FATAL_ERROR
		"Unable to locate required package : pthreads" )

endif( NOT CMAKE_USE_PTHREADS_INIT )

check_library_exists(
	${CMAKE_THREAD_LIBS_INIT}
	"pthread_mutex_timedlock"
	""
	FUNC_LIB_TIMEDLOCK )

if( FUNC_LIB_TIMEDLOCK )

	add_definitions( -DOPT_TIMEDLOCK )

endif( FUNC_LIB_TIMEDLOCK )

find_program(
	PATH_BIN_FLEX
	NAMES "flex"
	PATHS ${SEARCH_BIN} )

if( NOT PATH_BIN_FLEX )

	message(
		FATAL_ERROR
		"Unable to locate required binary : flex" )

endif( NOT PATH_BIN_FLEX )
		
message(
	STATUS 
	"Using binary ${PATH_BIN_FLEX} ..." )

find_program(
	PATH_BIN_BISON
	NAMES "bison"
	PATHS ${SEARCH_BIN} )

if( NOT PATH_BIN_BISON )

	message(
		FATAL_ERROR
		"Unable to locate required binary : bison" )

endif( NOT PATH_BIN_BISON )
		
message(
	STATUS 
	"Using binary ${PATH_BIN_BISON} ..." )

find_path(
	PATH_INC_NETIPSEC
	NAMES "ipsec.h"
	PATHS "/usr/include/netipsec" )

if( PATH_INC_NETIPSEC )

	add_definitions( -DOPT_NETIPSEC )

endif( PATH_INC_NETIPSEC )

find_library(
	PATH_LIB_RT
	NAMES "rt"
	PATHS ${SEARCH_LIB} )

find_path(
	PATH_INC_PFKEYV2
	NAMES "net/pfkeyv2.h" "linux/pfkeyv2.h"
	PATHS ${SEARCH_INC} ${INC_KERNEL_DIR} )

if( NOT PATH_INC_PFKEYV2 )

	message(
		FATAL_ERROR
		"Unable to locate system pfkeyv2 include file" )

endif( NOT PATH_INC_PFKEYV2 )

#
# Build Option Checks
#

OPTION( DEBUG  "include Debug Symbol support" )
OPTION( NATT   "include NAT Traversal support" )
OPTION( LDAP   "include LDAP Authentication support" )
OPTION( QTGUI  "include Client QT GUI support" )

# Debug Symbols Option

if( DEBUG )

	add_definitions( -g )

endif( DEBUG )

# NAT Traversion option

if( NATT )

	check_c_source_compiles(
		"
		#ifdef __linux__
		# include <inttypes.h>
		# include <linux/pfkeyv2.h>
		#else
		# include <sys/types.h>
		# include <net/pfkeyv2.h>
                #endif

		int main()
		{
			struct sadb_x_nat_t_type test;

			return 0;
		}
		"
		NATT_FOUND )

	if( NOT NATT_FOUND )

		message(
			FATAL_ERROR
			"Unable to locate required NAT-T pfkey structures" )

	endif( NOT NATT_FOUND )

	# success

	message(
		STATUS 
		"Enabled NAT Traversal support ..." )

	add_definitions( -DOPT_NATT )

endif( NATT )

# LDAP support option

if( LDAP )

	# ldap include

	find_path(
		PATH_INC_LDAP
		NAMES "ldap.h"
		PATHS ${SEARCH_INC} )

	if( NOT PATH_INC_LDAP )

		message(
			FATAL_ERROR
			"Unable to locate LDAP include file" )

	endif( NOT PATH_INC_LDAP )

	# ldap libraries

	find_library(
		PATH_LIB_LDAP
		NAMES "ldap"
		PATHS ${SEARCH_LIB} )

	find_library(
		PATH_LIB_LBER
		NAMES "lber"
		PATHS ${SEARCH_LIB} )

	if( NOT PATH_LIB_LDAP OR NOT PATH_LIB_LBER )

		message(
			FATAL_ERROR
			"Unable to locate LDAP library file" )

	endif( NOT PATH_LIB_LDAP OR NOT PATH_LIB_LBER )

	# success

	message( 
		STATUS
		"Enabled LDAP Authentication support ..." )

	add_definitions( -DOPT_LDAP )

endif( LDAP )

# Client GUI option

if( QTGUI )

	add_definitions( -DQT_THREAD_SUPPORT )

	set( QT_MT_REQUIRED TRUE )

	find_package( Qt3 )

	if( NOT QT_FOUND )

		message(
			FATAL_ERROR
			"Unable to locate required package : QT" )

	endif( NOT QT_FOUND )
		
	# success

	message(
		STATUS 
		"Enabled Client QT GUI support ..." )

	add_definitions( ${QT_DEFINITIONS} )

	add_subdirectory( source/ikea )
	add_subdirectory( source/ikec )

endif( QTGUI )


