# Copyright (C) 2010 Kerio Technologies s.r.o.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free
# Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

cmake_minimum_required(VERSION 2.6)

project(gmime)

set(gmime_GUID_CMAKE	"F281AAC0-61AB-4E5C-849D-34FDB3687675" CACHE INTERNAL "")

get_target_property (type glib TYPE)
if (type-NOTFOUND STREQUAL "${type}")
	message("-- Using internal glib imports")
	include("${CMAKE_CURRENT_SOURCE_DIR}/glib.cmake")
endif()

set(gmime_PUBLIC_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "" FORCE)
set(gmime_INTERFACE_LIBS "gmime" CACHE INTERNAL "" FORCE)
set(gmime_PUBLIC_INCLUDES_GENERATED 1 CACHE INTERNAL "-" FORCE)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/util)

set(gmime_SOURCES
	gmime/gen-table.c
	gmime/gmime-charset-map-private.h
	gmime/gmime-charset.c
	gmime/gmime-charset.h
	gmime/gmime-crypto-context.c
	gmime/gmime-crypto-context.h
	gmime/gmime-common.c
	gmime/gmime-common.h
	gmime/gmime-content-type.c
	gmime/gmime-content-type.h
	gmime/gmime-data-wrapper.c
	gmime/gmime-data-wrapper.h
	gmime/gmime-disposition.c
	gmime/gmime-disposition.h
	gmime/gmime-encodings.c
	gmime/gmime-encodings.h
	gmime/gmime-error.h
	gmime/gmime-events.c
	gmime/gmime-events.h
	gmime/gmime-filter-basic.c
	gmime/gmime-filter-basic.h
	gmime/gmime-filter-best.c
	gmime/gmime-filter-best.h
	gmime/gmime-filter-charset.c
	gmime/gmime-filter-charset.h
	gmime/gmime-filter-crlf.c
	gmime/gmime-filter-crlf.h
	gmime/gmime-filter-enriched.c
	gmime/gmime-filter-enriched.h
	gmime/gmime-filter-from.c
	gmime/gmime-filter-from.h
	gmime/gmime-filter-gzip.c
	gmime/gmime-filter-gzip.h
	gmime/gmime-filter-html.c
	gmime/gmime-filter-html.h
	gmime/gmime-filter-md5.c
	gmime/gmime-filter-md5.h
	gmime/gmime-filter-strip.c
	gmime/gmime-filter-strip.h
	gmime/gmime-filter-windows.c
	gmime/gmime-filter-windows.h
	gmime/gmime-filter-yenc.c
	gmime/gmime-filter-yenc.h
	gmime/gmime-filter.c
	gmime/gmime-filter.h
	gmime/gmime-header.c
	gmime/gmime-header.h
	gmime/gmime-iconv-utils.c
	gmime/gmime-iconv-utils.h
	gmime/gmime-iconv.c
	gmime/gmime-iconv.h
	gmime/gmime-message-part.c
	gmime/gmime-message-part.h
	gmime/gmime-message-partial.c
	gmime/gmime-message-partial.h
	gmime/gmime-message.c
	gmime/gmime-message.h
	gmime/gmime-multipart-encrypted.c
	gmime/gmime-multipart-encrypted.h
	gmime/gmime-multipart-signed.c
	gmime/gmime-multipart-signed.h
	gmime/gmime-multipart.c
	gmime/gmime-multipart.h
	gmime/gmime-object.c
	gmime/gmime-object.h
	gmime/gmime-param.c
	gmime/gmime-param.h
	gmime/gmime-parse-utils.c
	gmime/gmime-parse-utils.h
	gmime/gmime-parser.c
	gmime/gmime-parser.h
	gmime/gmime-part.c
	gmime/gmime-part.h
	gmime/gmime-part-charset.c
	gmime/gmime-part-charset.h
	gmime/gmime-pkcs7-context.c
	gmime/gmime-pkcs7-context.h
	gmime/gmime-stream-buffer.c
	gmime/gmime-stream-buffer.h
	gmime/gmime-stream-cat.c
	gmime/gmime-stream-cat.h
	gmime/gmime-stream-file.c
	gmime/gmime-stream-file.h
	gmime/gmime-stream-filter.c
	gmime/gmime-stream-filter.h
	gmime/gmime-stream-fs.c
	gmime/gmime-stream-fs.h
	gmime/gmime-stream-gio.c
	gmime/gmime-stream-gio.h
	gmime/gmime-stream-mem.c
	gmime/gmime-stream-mem.h
	gmime/gmime-stream-mmap.c
	gmime/gmime-stream-mmap.h
	gmime/gmime-stream-null.c
	gmime/gmime-stream-null.h
	gmime/gmime-stream-pipe.c
	gmime/gmime-stream-pipe.h
	gmime/gmime-stream.c
	gmime/gmime-stream.h
	gmime/gmime-table-private.h
	gmime/gmime-utils.c
	gmime/gmime-utils.h
	gmime/gmime-version.h
	gmime/gmime-version.h.in
	gmime/gmime.c
	gmime/gmime.h
	gmime/internet-address.c
	gmime/internet-address.h
	util/cache.c
	util/cache.h
	util/gtrie.h
	util/gtrie.c
	util/list.h
	util/list.c
	util/md5-utils.c
	util/md5-utils.h
	util/url-scanner.c
	util/url-scanner.h
)

if (WIN32)
	set(config_h_path "${CMAKE_CURRENT_BINARY_DIR}/config.h")
	set(config_h_in_path "${CMAKE_CURRENT_SOURCE_DIR}/build/vs2005/config-win32.h.in")
	set(unistd_h_path "${CMAKE_CURRENT_BINARY_DIR}/unistd.h")
	set(unistd_h_in_path "${CMAKE_CURRENT_SOURCE_DIR}/build/vs2005/unistd.h.in")
	add_custom_command(
		OUTPUT "${config_h_path}" "${unistd_h_path}"
		DEPENDS "${config_h_in_path}" "${unistd_h_in_path}"
		COMMAND "${CMAKE_COMMAND}" -E copy "${config_h_in_path}" "${config_h_path}"
		COMMAND "${CMAKE_COMMAND}" -E copy "${unistd_h_in_path}" "${unistd_h_path}"
	)

	include_directories(${CMAKE_CURRENT_BINARY_DIR})

	add_definitions(-D HAVE_CONFIG_H -DWIN32 -D_WINDOWS -D_USRDLL -D_CRT_SECURE_NO_WARNINGS -DGMIME_EXPORTS )

	set (gmime_SOURCES 
		${gmime_SOURCES}
		build/vs2005/gmime.def
		${config_h_path}
		${unistd_h_path}
	)
else()
	add_definitions(
		-DENABLE_CRYPTOGRAPHY=1
		-DGMIME_VERSION="2.5.3"
		-DHAVE_DLFCN_H=1
		-DHAVE_FSYNC=1
		-DHAVE_GETADDRINFO=1
		-DHAVE_GETDOMAINNAME=1
		-DHAVE_GETHOSTNAME=1
		-DHAVE_GETOPT_H=1
		-DHAVE_GETPAGESIZE=1
		-DHAVE_GMTIME_R=1
		-DHAVE_ICONV=1
		-DHAVE_ICONV_DETECT_H=1
		-DHAVE_INTTYPES_H=1
		-DHAVE_LOCALTIME=1
		-DHAVE_MEMORY_H=1
		-DHAVE_MMAP=1
		-DHAVE_MSYNC=1
		-DHAVE_MUNMAP=1
		-DHAVE_NETDB_H=1
		-DHAVE_POLL=1
		-DHAVE_POLL_H=1
		-DHAVE_REGEX_H=1
		-DHAVE_SELECT=1
		-DHAVE_STDINT_H=1
		-DHAVE_STDLIB_H=1
		-DHAVE_STRFTIME=1
		-DHAVE_STRINGS_H=1
		-DHAVE_STRING_H=1
		-DHAVE_SYS_MMAN_H=1
		-DHAVE_SYS_PARAM_H=1
		-DHAVE_SYS_STAT_H=1
		-DHAVE_SYS_TYPES_H=1
		-DHAVE_TIME_H=1
		-DHAVE_TM_GMTOFF=1
		-DHAVE_UNISTD_H=1
		-DHAVE_ZLIB_H=1
		-DSIZEOF_OFF_T=8
		-DSIZEOF_SIZE_T=4
		-DSIZEOF_SSIZE_T=4
		-DSTDC_HEADERS=1
		-D_FILE_OFFSET_BITS=64
		-DICONV_CONST=
	)

	if (LINUX)
		add_definitions(
			-DHAVE_UTSNAME_DOMAINNAME=1
		)
	endif()
endif()

add_definitions(-DG_DISABLE_DEPRECATED -DG_LOG_DOMAIN="GMime")

add_library(gmime SHARED
	${gmime_SOURCES}
)

target_link_libraries(gmime z glib gobject gio kticonv )

if (WIN32)
	target_link_libraries(gmime Ws2_32 )
endif()

if (APPLE)
	set_target_properties(gmime PROPERTIES
		LINK_FLAGS "-Wl,-single_module"
	)
	cmake_policy(SET CMP0026 OLD)
	get_target_property(gmime_LIBNAME gmime LOCATION_${CMAKE_BUILD_TYPE})
	add_custom_command(
		TARGET gmime
		POST_BUILD
		COMMAND dsymutil ${gmime_LIBNAME}
	)
endif()

if (WIN32)
  if (KERIO_TARGET_DIR)
    set_target_properties(gmime PROPERTIES 
      RUNTIME_OUTPUT_DIRECTORY ${KERIO_TARGET_DIR}
      )
  endif()
endif()


#install source file
if (NOT gmimeFiles)
	#remember file list from the first run - subsequent runs might add files generated during build
	file(GLOB_RECURSE gmimeFiles "${CMAKE_CURRENT_SOURCE_DIR}/*")
	set(gmimeFiles ${gmimeFiles} CACHE INTERNAL "")
endif ()
foreach(gmimeFile ${gmimeFiles})
	string(REGEX MATCHALL ^.*/ destDir ${gmimeFile})
	string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" destDir "${destDir}")
	if (NOT destDir MATCHES CVS)
		install(FILES "${gmimeFile}" DESTINATION "${CMAKE_INSTALL_PREFIX}/gmime/${destDir}")
	endif ()
endforeach()
