cmake_minimum_required(VERSION 2.6)
project(EncFSMP)

#SET(CMAKE_VERBOSE_MAKEFILE TRUE)
SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_SOURCE_DIR}/cmake")

# Enable Unicode
ADD_DEFINITIONS(-D_UNICODE -DUNICODE)

IF(NOT DEFINED ${IS_64BITS})
	SET(IS_64BITS 0)
ENDIF(NOT DEFINED ${IS_64BITS})

IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
	SET(IS_64BITS 1)
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)

# Retrieve version numbers from version.h
FILE(STRINGS "${CMAKE_SOURCE_DIR}/version.h" ENCFSMP_VERSION_MAJOR_LINE REGEX "ENCFSMP_VERSION_MAJOR .?")
STRING(FIND ${ENCFSMP_VERSION_MAJOR_LINE} " " EFS_VMJL_LAST_SPACE_POS REVERSE)
STRING(SUBSTRING ${ENCFSMP_VERSION_MAJOR_LINE} ${EFS_VMJL_LAST_SPACE_POS} -1 ENCFSMP_VERSION_MAJOR)
STRING(STRIP ${ENCFSMP_VERSION_MAJOR} ENCFSMP_VERSION_MAJOR)
FILE(STRINGS "${CMAKE_SOURCE_DIR}/version.h" ENCFSMP_VERSION_MINOR_LINE REGEX "ENCFSMP_VERSION_MINOR .?")
STRING(FIND ${ENCFSMP_VERSION_MINOR_LINE} " " EFS_VMNL_LAST_SPACE_POS REVERSE)
STRING(SUBSTRING ${ENCFSMP_VERSION_MINOR_LINE} ${EFS_VMNL_LAST_SPACE_POS} -1 ENCFSMP_VERSION_MINOR)
STRING(STRIP ${ENCFSMP_VERSION_MINOR} ENCFSMP_VERSION_MINOR)
FILE(STRINGS "${CMAKE_SOURCE_DIR}/version.h" ENCFSMP_VERSION_BUILD_LINE REGEX "ENCFSMP_VERSION_BUILD .?")
STRING(FIND ${ENCFSMP_VERSION_BUILD_LINE} " " EFS_VBLL_LAST_SPACE_POS REVERSE)
STRING(SUBSTRING ${ENCFSMP_VERSION_BUILD_LINE} ${EFS_VBLL_LAST_SPACE_POS} -1 ENCFSMP_VERSION_BUILD)
STRING(STRIP ${ENCFSMP_VERSION_BUILD} ENCFSMP_VERSION_BUILD)
FILE(STRINGS "${CMAKE_SOURCE_DIR}/version.h" ENCFSMP_COPYRIGHT_YEAR_LINE REGEX "ENCFSMP_COPYRIGHT_YEAR .?")
STRING(FIND ${ENCFSMP_COPYRIGHT_YEAR_LINE} " " EFS_CY_LAST_SPACE_POS REVERSE)
STRING(SUBSTRING ${ENCFSMP_COPYRIGHT_YEAR_LINE} ${EFS_CY_LAST_SPACE_POS} -1 ENCFSMP_COPYRIGHT_YEAR)
STRING(REPLACE "\"" "" ENCFSMP_COPYRIGHT_YEAR ${ENCFSMP_COPYRIGHT_YEAR})
STRING(STRIP ${ENCFSMP_COPYRIGHT_YEAR} ENCFSMP_COPYRIGHT_YEAR)
#message("${ENCFSMP_VERSION_MAJOR}.${ENCFSMP_VERSION_MINOR}.${ENCFSMP_VERSION_BUILD}")

# Write version numbers into plist (OS X only)
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/res/osx/encfsmp-Info.plist.in encfsmp-Info.plist)

FIND_PACKAGE(Threads)
IF(CMAKE_USE_PTHREADS_INIT AND NOT WIN32)
	SET(HAVE_PTHREAD TRUE)
ENDIF(CMAKE_USE_PTHREADS_INIT AND NOT WIN32)

# Configuration options
INCLUDE(CheckIncludeFile)
INCLUDE(CheckIncludeFileCXX)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckTypeSize)
INCLUDE(CheckCXXSourceCompiles)

CHECK_INCLUDE_FILE(dlfcn.h		HAVE_DLFCN_H)
CHECK_INCLUDE_FILE(stdint.h		HAVE_STDINT_H)
CHECK_INCLUDE_FILE(inttypes.h   HAVE_INTTYPES_H)
CHECK_INCLUDE_FILE(sys/stat.h   HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILE(sys/types.h  HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILE(sys/dir.h	HAVE_SYS_DIR_H)
CHECK_INCLUDE_FILE(unistd.h     HAVE_UNISTD_H)
CHECK_INCLUDE_FILE(windows.h	HAVE_WINDOWS_H)
CHECK_INCLUDE_FILE(stdlib.h		HAVE_STDLIB_H)
CHECK_INCLUDE_FILE(strings.h	HAVE_STRINGS_H)
CHECK_INCLUDE_FILE(string.h		HAVE_STRING_H)
CHECK_INCLUDE_FILE(sys/xattr.h	HAVE_SYS_XATTR_H)
CHECK_INCLUDE_FILE(memory.h		HAVE_MEMORY_H)
CHECK_INCLUDE_FILE(sys/utime.h	HAVE_SYS_UTIME_H)
CHECK_INCLUDE_FILE(utime.h		HAVE_UTIME_H)
CHECK_INCLUDE_FILE(io.h			HAVE_IO_H)
CHECK_INCLUDE_FILE(sys/fsuid.h	HAVE_SYS_FSUID_H)

CHECK_TYPE_SIZE(ssize_t			SSIZE_T)

SET(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h")
CHECK_TYPE_SIZE(mode_t			MODE_T)
CHECK_TYPE_SIZE(pid_t			PID_T)
CHECK_TYPE_SIZE(off_t			OFF_T)
CHECK_TYPE_SIZE(off64_t			OFF64_T)

IF(HAVE_UNISTD_H)
	SET(CMAKE_EXTRA_INCLUDE_FILES "unistd.h")
	CHECK_TYPE_SIZE(gid_t			GID_T)
	CHECK_TYPE_SIZE(uid_t			UID_T)
ELSE(HAVE_UNISTD_H)
	SET(HAVE_GID_T FALSE)
	SET(HAVE_UID_T FALSE)
ENDIF(HAVE_UNISTD_H)

SET(CMAKE_EXTRA_INCLUDE_FILES "stdio.h")
CHECK_FUNCTION_EXISTS(vsnprintf	HAVE_VSNPRINTF)
CHECK_FUNCTION_EXISTS(_vsnprintf	HAVE__VSNPRINTF)

IF(HAVE_SYS_FSUID_H)
	SET(CMAKE_EXTRA_INCLUDE_FILES "sys/fsuid.h")
	CHECK_FUNCTION_EXISTS(setfsuid	HAVE_SETFSUID)
ELSE(HAVE_SYS_FSUID_H)
	SET(HAVE_SETFSUID FALSE)
ENDIF(HAVE_SYS_FSUID_H)

IF(HAVE_SYS_STAT_H)
	SET(CMAKE_EXTRA_INCLUDE_FILES "sys/stat.h")
	IF(MINGW)
		SET(CMAKE_REQUIRED_DEFINITIONS "-D__MSVCRT_VERSION__=0x0601")
		ADD_DEFINITIONS("-D__MSVCRT_VERSION__=0x0601")
	ENDIF(MINGW)
	CHECK_TYPE_SIZE("struct stat" STRUCT_STAT)
	CHECK_TYPE_SIZE("struct stat64" STRUCT_STAT64)
	CHECK_TYPE_SIZE("struct _stat64" STRUCT__STAT64)
	CHECK_TYPE_SIZE("struct __stat64" STRUCT___STAT64)
	SET(CMAKE_EXTRA_INCLUDE_FILES )
ELSE(HAVE_SYS_STAT_H)
	SET(HAVE_STRUCT_STAT FALSE)
	SET(HAVE_STRUCT_STAT64 FALSE)
	SET(HAVE_STRUCT__STAT64 FALSE)
	SET(HAVE_STRUCT___STAT64 FALSE)
ENDIF(HAVE_SYS_STAT_H)

IF(WIN32)
	SET(CMAKE_EXTRA_INCLUDE_FILES "wchar.h")
	CHECK_FUNCTION_EXISTS(_wsopen_s	HAVE__WSOPEN_S)
ELSE(WIN32)
	SET(HAVE__WSOPEN_S FALSE)
ENDIF(WIN32)

CONFIGURE_FILE(config.h.in config.h)
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})

CHECK_CXX_SOURCE_COMPILES("
void test(int val) { }
int main()
{
static int foo __attribute__ (( section(\"__DATA, RLOG_DATA\") )) = 0;
test( foo );
return 0;
}
" HAVE_GCC_DATA_SECTION_ATTRIBUTE )

CHECK_CXX_SOURCE_COMPILES("
void test(int val) { }
int main()
{
static int foo __attribute__ (( section(\"RLOG_DATA\") )) = 0;
test( foo );
return 0;
}
" HAVE_GCC_SIMPLE_SECTION_ATTRIBUTE )

CONFIGURE_FILE(rlog/common.h.in rlog/common.h)
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/rlog)

IF(HAVE_INTTYPES_H)
	ADD_DEFINITIONS(-D__STDC_FORMAT_MACROS)
ENDIF(HAVE_INTTYPES_H)

# For some MinGW compilers, add -DENCFSMP_DEFINE_STOCKICONINFO=TRUE
IF(ENCFSMP_DEFINE_STOCKICONINFO)
	ADD_DEFINITIONS(-DEFS_DEFINE_STOCKICONINFO)
ENDIF(ENCFSMP_DEFINE_STOCKICONINFO)

# Pismo File Mount Library
FIND_PACKAGE(PFM REQUIRED)
INCLUDE_DIRECTORIES(${PFM_INCLUDE_DIR})
ADD_DEFINITIONS(-DRLOG_COMPONENT="encfs")

# wxWidgets
IF(WIN32)
	IF(CMAKE_COMPILER_IS_GNUCC)
		IF(${IS_64BITS} EQUAL 1)
			SET(wxWidgets_LIB_DIR ${wxWidgets_ROOT_DIR}/lib/gcc_lib64)
		ELSE(${IS_64BITS} EQUAL 1)
			SET(wxWidgets_LIB_DIR ${wxWidgets_ROOT_DIR}/lib/gcc_lib)
		ENDIF(${IS_64BITS} EQUAL 1)
	ELSE(CMAKE_COMPILER_IS_GNUCC)
		IF(${IS_64BITS} EQUAL 1)
			SET(wxWidgets_LIB_DIR ${wxWidgets_ROOT_DIR}/lib/vc_lib64)
		ELSE(${IS_64BITS} EQUAL 1)
			SET(wxWidgets_LIB_DIR ${wxWidgets_ROOT_DIR}/lib/vc_lib)
		ENDIF(${IS_64BITS} EQUAL 1)
	ENDIF(CMAKE_COMPILER_IS_GNUCC)
	IF(CMAKE_BUILD_TYPE="Debug")
		SET(wxWidgets_CONFIGURATION mswud)
	ELSE()
		SET(wxWidgets_CONFIGURATION mswu)
	ENDIF()
ENDIF(WIN32)
message("wxWidgets_ROOT_DIR = ${wxWidgets_ROOT_DIR}")
#message("wxWidgets_LIB_DIR = ${wxWidgets_LIB_DIR}")
#message("wxWidgets_CONFIGURATION = ${wxWidgets_CONFIGURATION}")

FIND_PACKAGE(wxWidgets REQUIRED adv core base)
INCLUDE(${wxWidgets_USE_FILE})
# Workaround for MSYS
IF(DEFINED ENCFSMP_ALT_wxWidgets_INCLUDE_DIRS)
	SET(wxWidgets_INCLUDE_DIRS = "${ENCFSMP_ALT_wxWidgets_INCLUDE_DIRS}" )
ENDIF(DEFINED ENCFSMP_ALT_wxWidgets_INCLUDE_DIRS)

IF(ENCFSMP_REMOVE_ADDITIONAL_LIBRARIES)
	# These are superfluous libraries added by wx-config
	# These libraries are already linked to, but statically (depending on the platform)
	# On OS X, these link parameters lead to a dependency on the shared libraries
	STRING(REPLACE "-lz" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
	STRING(REPLACE "-lpthread" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
	STRING(REPLACE "-llzma" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
	STRING(REPLACE "-ljbig" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
	STRING(REPLACE "-ljpeg" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
	STRING(REPLACE "-lpng" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
	STRING(REPLACE "-ltiff" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
	STRING(REPLACE "-liconv" "" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}" )
ENDIF(ENCFSMP_REMOVE_ADDITIONAL_LIBRARIES)

message("wxWidgets_INCLUDE_DIRS = ${wxWidgets_INCLUDE_DIRS}")
message("wxWidgets_LIBRARIES = ${wxWidgets_LIBRARIES}")
INCLUDE_DIRECTORIES(${wxWidgets_INCLUDE_DIRS})

# Google Test
INCLUDE_DIRECTORIES(${GOOGLE_TEST_INCLUDE})


message("OPENSSL_LIBRARIES = ${OPENSSL_LIBRARIES}")

# Backup
SET( OPENSSL_LIBRARIES_BAK ${OPENSSL_LIBRARIES} )

# OpenSSL
FIND_PACKAGE(OpenSSL REQUIRED)

IF(OPENSSL_LIBRARIES_BAK)
	SET( OPENSSL_LIBRARIES ${OPENSSL_LIBRARIES_BAK} )
ENDIF(OPENSSL_LIBRARIES_BAK)

IF(OPENSSL_FOUND)
	message("OpenSSL version ${OPENSSL_VERSION} found")
	message("OPENSSL_INCLUDE_DIR = ${OPENSSL_INCLUDE_DIR}")
	message("OPENSSL_LIBRARIES = ${OPENSSL_LIBRARIES}")

	INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
ELSE(OPENSSL_FOUND)
	message("OpenSSL not found")
ENDIF(OPENSSL_FOUND)

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
IF(MSVC)
	SET(Boost_USE_STATIC_RUNTIME     OFF)
ELSE(MSVC)
	IF(MSYS)
		SET(Boost_USE_STATIC_RUNTIME     OFF)
	ELSE(MSYS)
		SET(Boost_USE_STATIC_RUNTIME     ON)
	ENDIF(MSYS)
ENDIF(MSVC)
#SET(Boost_DEBUG TRUE)
find_package( Boost 1.53.0 COMPONENTS date_time thread
	system chrono serialization filesystem locale random)
IF(Boost_FOUND)
	include_directories(${Boost_INCLUDE_DIRS})
	IF(UNIX AND NOT APPLE)
		# Workaround for apparent bug in FindBoost (librt is not added at the end):
		message("Boost_LIBRARIES = ${Boost_LIBRARIES}")
		SET(Boost_LIBRARIES "${Boost_LIBRARIES};rt")
	ENDIF(UNIX AND NOT APPLE)
ENDIF(Boost_FOUND)

# Determine whether 32 or 64 bits
IF(${IS_64BITS} EQUAL 1)
	SET( EFS_CMAKE_ARCHITECTURE "x64" )
	SET( EFS_COMPILER_ARCH "" )
	IF(MINGW)
		SET( CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} -D WX_CPU_AMD64" )
	ENDIF(MINGW)
ELSE(${IS_64BITS} EQUAL 1)
	SET( EFS_CMAKE_ARCHITECTURE "x86")
	SET( EFS_COMPILER_ARCH "/arch:SSE2")
	IF(MINGW)
		SET( CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} -D WX_CPU_X86" )
	ENDIF(MINGW)
ENDIF(${IS_64BITS} EQUAL 1)

# Prepare config.h with several defines and make sure it's in our include path
IF(WIN32)
	SET( EFS_WIN32 1 )
ENDIF(WIN32)
IF(APPLE)
	SET( EFS_MACOSX 1 )
ENDIF(APPLE)
CONFIGURE_FILE(efs_config.h.in efs_config.h)
include_directories(${CMAKE_BINARY_DIR})


# Set options for Intel
IF(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")
	SET( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD /Ox /fp:fast=2 /Qansi-alias /Qalias-args- /Qalias-const- /Qinline-max-size- ${EFS_COMPILER_ARCH} /Qprec-div- /Qprec-sqrt- ${INTEL_COMPILER_EXTRA_OPTIONS}" )
	message("CMAKE_C_FLAGS_RELEASE: ${CMAKE_C_FLAGS_RELEASE}")
	SET( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /Ox /fp:fast=2 /Qansi-alias /Qalias-args- /Qalias-const- /Qinline-max-size- ${EFS_COMPILER_ARCH} /Qprec-div- /Qprec-sqrt- ${INTEL_COMPILER_EXTRA_OPTIONS}" )
	message("CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")

	# Do not link the Intel math library libmmd.dll, but instead use the default Microsoft CRT
#	SET( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} svml_dispmt.lib libmmt.lib /NODEFAULTLIB:libmmd.lib /INCREMENTAL:NO" )
	SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /INCREMENTAL:NO /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib" )
	message("CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS}")
ENDIF(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")

if(MSVC)
	SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EFS_COMPILER_ARCH} /MP" )
	SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EFS_COMPILER_ARCH} /MP" )
	SET( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Ox /Ob2 /Oi /Ot /GL /GS- ${EFS_COMPILER_ARCH} /fp:fast /MP" )
	SET( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox /Ob2 /Oi /Ot /GL /GS- ${EFS_COMPILER_ARCH} /fp:fast /MP" )

	# Enable Large address aware in 32 bit builds
	IF(${IS_64BITS} EQUAL 0 AND WIN32)
		SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LARGEADDRESSAWARE")
	ENDIF(${IS_64BITS} EQUAL 0 AND WIN32)

ENDIF(MSVC)

IF(CMAKE_COMPILER_IS_GNUCC)
	SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2 -mfpmath=sse -Wno-long-long ${EXTRA_COMPILER_OPTIONS}" )
	SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2 -mfpmath=sse -fpermissive -Wno-long-long ${EXTRA_COMPILER_OPTIONS}" )
#	SET( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -march=core2 -ffast-math -funsafe-loop-optimizations" )
#	SET( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=core2 -ffast-math -funsafe-loop-optimizations" )

	IF(MINGW)
		ADD_DEFINITIONS("-D__MSVCRT_VERSION__=0x0601")
	ENDIF(MINGW)

	IF(EFS_USE_STATIC_LIBGCC)
		SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
	ENDIF(EFS_USE_STATIC_LIBGCC)
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -msse2 ${EXTRA_COMPILER_OPTIONS}")
	
	# Enable Large address aware in 32 bit builds
	IF(${IS_64BITS} EQUAL 0 AND WIN32)
		SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--large-address-aware")
	ENDIF(${IS_64BITS} EQUAL 0 AND WIN32)
	IF(WIN32)
		# Add some security flags: Enable ASLR and DEP
		SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--dynamicbase -Wl,--nxcompat")
	ENDIF(WIN32)

	# Strip everything in Release builds
	SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -Wl,--strip-all")

ENDIF(CMAKE_COMPILER_IS_GNUCC)

SET(ALL_SRC ${ALL_SRC} fs_layer.cpp pfm_layer.cpp EncFSMPMainFrameBase.cpp
	EncFSMPMainFrame.cpp PFMProxy.cpp PFMHandlerThread.cpp OpenSSLProxy.cpp
	PFMMonitorThread.cpp OpenExistingFSDialog.cpp EncFSUtilities.cpp MountList.cpp
	CreateNewEncFSDialog.cpp ChangePasswordDialog.cpp EncFSMPStrings.cpp
	EncFSMPTaskBarIcon.cpp EncFSMPErrorLog.cpp EncFSMPLogger.cpp FileStatCache.cpp )

SET(ALL_HEADERS ${ALL_HEADERS} fs_layer.h pfm_layer.h EncFSMPMainFrameBase.h
	EncFSMPMainFrame.h CommonIncludes.h version.h PFMProxy.h PFMHandlerThread.h OpenSSLProxy.h
	PFMMonitorThread.h OpenExistingFSDialog.h EncFSUtilities.h MountList.h
	CreateNewEncFSDialog.h ChangePasswordDialog.h EncFSMPStrings.h
	EncFSMPTaskBarIcon.h EncFSMPErrorLog.h EncFSMPLogger.h FileStatCache.h )

IF(WIN32)
	# Add resource definitions, Win32Utils and IPC for Windows
	SET(ALL_SRC ${ALL_SRC} Win32Utils.cpp res/win/encfsmp.rc EncFSMPIPCWin.cpp)
	SET(ALL_HEADERS ${ALL_HEADERS} Win32Utils.h EncFSMPIPCWin.h)
ELSE(WIN32)
	SET(ALL_SRC ${ALL_SRC} EncFSMPIPCPosix.cpp)
	SET(ALL_HEADERS ${ALL_HEADERS} EncFSMPIPCPosix.h)
ENDIF(WIN32)

# Add OS X workaround for empty menus
IF(APPLE)
	SET(ALL_SRC ${ALL_SRC} osx/MacMenuWorkaroundBridge.cpp osx/MacMenuWorkaround.m)
	SET(ALL_HEADERS ${ALL_HEADERS} osx/MacMenuWorkaroundBridge.h osx/MacMenuWorkaround.h)
	SET_SOURCE_FILES_PROPERTIES( "osx/MacMenuWorkaroundBridge.cpp" PROPERTIES COMPILE_FLAGS " -x objective-c++ " )
ENDIF(APPLE)

# For Apple set the icns file containing icons
IF(APPLE)
	# icon files to copy in the bundle
	SET( OSX_ICON_FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/osx/encfsmp.icns )
	# set where in the bundle to put the icns files
	SET_SOURCE_FILES_PROPERTIES( ${OSX_ICON_FILES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
	# include the icns files in the target
	SET( ALL_SRC ${ALL_SRC} ${OSX_ICON_FILES} )
ENDIF(APPLE)

ADD_SUBDIRECTORY(encfs)
ADD_SUBDIRECTORY(rlog)
ADD_SUBDIRECTORY(intl)

INCLUDE_DIRECTORIES(".")
INCLUDE_DIRECTORIES("encfs")
INCLUDE_DIRECTORIES("rlog")
INCLUDE_DIRECTORIES("intl")

SET(EncFSMP_LINK_LIBRARIES ${wxWidgets_LIBRARIES} libencfs rlog intl ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES})
IF(WIN32)
	SET(EncFSMP_LINK_LIBRARIES ${EncFSMP_LINK_LIBRARIES} psapi.lib Crypt32.lib Ws2_32.lib)
ENDIF(WIN32)

IF(DEFINED ENCFSMP_ADDITIONAL_LINK_LIBRARIES)
	SET(EncFSMP_LINK_LIBRARIES ${EncFSMP_LINK_LIBRARIES} ${ENCFSMP_ADDITIONAL_LINK_LIBRARIES})
ENDIF(DEFINED ENCFSMP_ADDITIONAL_LINK_LIBRARIES)

ADD_EXECUTABLE(EncFSMP WIN32 MACOSX_BUNDLE ${ALL_SRC} ${ALL_HEADERS} EncFSMPApp.cpp EncFSMPApp.h )

# The test program: Use "make check" to build and run it
SET(CMAKE_CTEST_COMMAND ${CMAKE_CTEST_COMMAND} -V)
ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND})
SET(ALL_TEST_SRC EncFSMPTestApp.cpp TestBigFile.cpp TestOpenFileTwice.cpp
	TestFileHelper.cpp TestReadOnlyFlag.cpp )
SET(ALL_TEST_HEADERS EncFSMPTestApp.h TestBigFile.h TestOpenFileTwice.h TestFileHelper.h TestReadOnlyFlag.h)
IF(WIN32)
	LIST(APPEND ALL_TEST_SRC TestFileWin32.cpp )
	LIST(APPEND ALL_TEST_HEADERS TestFileWin32.h )
ENDIF(WIN32)
ADD_EXECUTABLE(EncFSMPTest EXCLUDE_FROM_ALL ${ALL_SRC} ${ALL_HEADERS} ${ALL_TEST_SRC} ${ALL_TEST_HEADERS} )

TARGET_LINK_LIBRARIES(EncFSMP ${EncFSMP_LINK_LIBRARIES})
TARGET_LINK_LIBRARIES(EncFSMPTest ${EncFSMP_LINK_LIBRARIES} ${GOOGLE_TEST_LIBRARY})

# For make-based builds, defines make target named test.
# For Visual Studio builds, defines Visual Studio project named RUN_TESTS.
ENABLE_TESTING()
ADD_TEST(
    NAME EncFSMPTest
    COMMAND EncFSMPTest "Z:\\"
)
ADD_DEPENDENCIES(check EncFSMPTest)


IF(APPLE)
	# configure CMake to use a custom Info.plist
#	SET_TARGET_PROPERTIES( EncFSMP PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/res/osx/encfsmp-Info.plist )
	SET_TARGET_PROPERTIES( EncFSMP PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_BINARY_DIR}/encfsmp-Info.plist )
ENDIF(APPLE)

# Installer definitions
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "EncFS MP")
SET(CPACK_PACKAGE_VENDOR "Roman Hiestand")
#SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/ReadMe.txt")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/licenses/Copyright.txt")
SET(CPACK_PACKAGE_VERSION_MAJOR ${ENCFSMP_VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${ENCFSMP_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${ENCFSMP_VERSION_BUILD})
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "EncFSMP")
SET(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
IF(APPLE)
	SET(CPACK_GENERATOR "DragNDrop")
#	SET(CPACK_GENERATOR "PackageMaker")
#	SET(CPACK_PREFLIGHT_SCRIPT "${CMAKE_SOURCE_DIR}/install/install_test.sh")
	SET(CPACK_OSX_PACKAGE_VERSION "10.6")
#	SET(CMAKE_INSTALL_PREFIX "/Applications")
#	SET(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
ENDIF(APPLE)
IF(UNIX)
	SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>=2.7-18)")
	SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
ENDIF(UNIX)

INSTALL(TARGETS EncFSMP DESTINATION ".")
IF(APPLE)
	ADD_CUSTOM_COMMAND(
		OUTPUT "${CMAKE_BINARY_DIR}/Install PFM.app"
		COMMAND osacompile -o "${CMAKE_BINARY_DIR}/Install PFM.app" "${CMAKE_SOURCE_DIR}/install/Install PFM.scpt")
	ADD_CUSTOM_TARGET(Install_PFM ALL DEPENDS "${CMAKE_BINARY_DIR}/Install PFM.app")
	ADD_CUSTOM_COMMAND(
		OUTPUT "${CMAKE_BINARY_DIR}/Uninstall PFM.app"
		COMMAND osacompile -o "${CMAKE_BINARY_DIR}/Uninstall PFM.app" "${CMAKE_SOURCE_DIR}/install/Uninstall PFM.scpt")
	ADD_CUSTOM_TARGET(Uninstall_PFM ALL DEPENDS "${CMAKE_BINARY_DIR}/Uninstall PFM.app")

	INSTALL(PROGRAMS ${CMAKE_SOURCE_DIR}/install/res/pfm-183-encfsmp-mac.run DESTINATION pfm)
	INSTALL(DIRECTORY "${CMAKE_BINARY_DIR}/Install PFM.app" DESTINATION "." USE_SOURCE_PERMISSIONS)
	INSTALL(DIRECTORY "${CMAKE_BINARY_DIR}/Uninstall PFM.app" DESTINATION "." USE_SOURCE_PERMISSIONS)
ENDIF(APPLE)
INCLUDE(CPack)
