cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(libsodium LANGUAGES C)

OPTION(BUILD_STATIC "Build static library" ON)
OPTION(BUILD_SHARED "Build shared library" ON)

if (IOS)
	set(BUILD_SHARED OFF)
endif (IOS)

function(read_value var_name match_regex replace_regex lines)
  message(STATUS "Looking for ${var_name}")
	string(REGEX MATCH "${match_regex}" line ${lines})
	if (line)
	  string(REGEX REPLACE "${replace_regex}" "\\1" ${var_name} ${line})
		message(STATUS "Looking for ${var_name} - found ${${var_name}}")
		set(${var_name} ${${var_name}} PARENT_SCOPE)
	else (line)
	  message(FATAL_ERROR "Looking for ${var_name} - failed")
	endif (line)
endfunction(read_value)

file(STRINGS "${CMAKE_SOURCE_DIR}/configure" lines)
read_value(SODIUM_VERSION_STRING "VERSION='[0-9.]+'" "^VERSION='([0-9.]+)'$" "${lines}")
read_value(SODIUM_LIBRARY_VERSION_MAJOR "SODIUM_LIBRARY_VERSION_MAJOR=[0-9]+"
           "^SODIUM_LIBRARY_VERSION_MAJOR=([0-9]+)$" "${lines}")
read_value(SODIUM_LIBRARY_VERSION_MINOR "SODIUM_LIBRARY_VERSION_MINOR=[0-9]+"
           "^SODIUM_LIBRARY_VERSION_MINOR=([0-9]+)$" "${lines}")
read_value(SODIUM_LIBRARY_MINIMAL_DEF "SODIUM_LIBRARY_MINIMAL_DEF=\"[^\"]*\""
					 "^ *SODIUM_LIBRARY_MINIMAL_DEF=\"([^\"]*)\"$" "${lines}")

set(SODIUM_INCLUDE "${CMAKE_SOURCE_DIR}/src/libsodium/include/sodium")
set(TMP_INCLUDE "${CMAKE_BINARY_DIR}/include")
set(VERSION_H_IN "${SODIUM_INCLUDE}/version.h.in")
set(VERSION_H_IN_TMP "${TMP_INCLUDE}/version.h.in")
set(VERSION_H "${TMP_INCLUDE}/version.h")
message(STATUS "Generating version.h")
file(STRINGS ${VERSION_H_IN} lines)
foreach(line ${lines})
	string(REPLACE "#define SODIUM" "#cmakedefine SODIUM" l0 "${line}")
	string(REPLACE "@VERSION@" "@SODIUM_VERSION_STRING@" l1 "${l0}")
	string(REPLACE "@SODIUM_LIBRARY_MINIMAL_DEF@" "${SODIUM_LIBRARY_MINIMAL_DEF}" l2 "${line}")
	if (NOT "${line}" STREQUAL "${l1}")
		file(APPEND ${VERSION_H_IN_TMP} "${l1}\n")
	elseif (NOT "${line}" STREQUAL "${l2}")
		file(APPEND ${VERSION_H_IN_TMP} "${l2}\n")
	else ()
		file(APPEND ${VERSION_H_IN_TMP} "${line}\n")
	endif ()
endforeach(line)
configure_file(${VERSION_H_IN_TMP} ${VERSION_H} @ONLY)
message(STATUS "Generating version.h - done")

add_definitions(-DCONFIGURED)
include_directories(${SODIUM_INCLUDE} ${TMP_INCLUDE})
install(DIRECTORY ${SODIUM_INCLUDE} DESTINATION include FILES_MATCHING PATTERN "*.h")
install(FILES "${CMAKE_SOURCE_DIR}/src/libsodium/include/sodium.h" DESTINATION include)
install(FILES ${VERSION_H} DESTINATION include/sodium)

file(GLOB_RECURSE SRCS src/*.c)

if (BUILD_STATIC)
	add_library(sodium_static STATIC ${SRCS})
	set_target_properties(sodium_static PROPERTIES OUTPUT_NAME sodium)
	install(TARGETS sodium_static ARCHIVE DESTINATION lib)
endif (BUILD_STATIC)

if (BUILD_SHARED)
	add_library(sodium_shared SHARED ${SRCS})
	set_target_properties(sodium_shared PROPERTIES OUTPUT_NAME sodium)
	set_target_properties(sodium_shared PROPERTIES VERSION ${SODIUM_VERSION_STRING})
	install(TARGETS sodium_shared LIBRARY DESTINATION lib)
endif (BUILD_SHARED)
