cmake_minimum_required(VERSION 2.4.3 FATAL_ERROR)

# creates variables sandboxlib_SOURCE_DIR and sandboxlib_BINARY_DIR
project(sandboxlib)
message("sandboxlib binary dir: ${sandboxlib_BINARY_DIR}. source: ${sandboxlib_SOURCE_DIR}")

# sets various variables to be used by pkgconfig and cmake amongst others
set(sandboxlib_MAJOR_VERSION "0")
set(sandboxlib_MINOR_VERSION "1")
set(sandboxlib_PATCH_VERSION "0")
set(sandboxlib_PACKAGE_DESCRIPTION "Package created to test building and running of cppunit tests")
set(sandboxlib_PACKAGE_NAME "libsandbox")
set(sandboxlib_PACKAGE_VENDOR "Sandbox Ltd")

subdirs(src)

include(TestCXXAcceptsFlag)

# other possibly useful warning flags:
# -Wfloat-equal -Weffc++ -Winline -Wno-unused
foreach(flag -Wall -W -Wwrite-strings -Wundef -O2)
	check_cxx_accepts_flag("${flag}" FLAG${flag})
	if(FLAG${flag})
		set(CMAKE_CXX_FLAGS "${flag} ${CMAKE_CXX_FLAGS}")
	endif(FLAG${flag})
endforeach(flag)

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

include(FindCppUnit)

if(CPPUNIT_FOUND)
	message(STATUS "CppUnit found")
	message("CPPUNIT_LIBRARIES = " ${CPPUNIT_LIBRARIES})
	ENABLE_TESTING()
    ADD_TEST(sandboxlib-tests ${CMAKE_CTEST_COMMAND}
    --build-and-test
    "${sandboxlib_SOURCE_DIR}/tests"
    "${sandboxlib_BINARY_DIR}/tests"
    --build-generator ${CMAKE_GENERATOR}
    --build-project sandboxlib-tests 
    --build-makeprogram ${CMAKE_MAKE_MAKEPROGRAM}
    --build-two-config
    --test-command sandboxlib-tests)

else(CPPUNIT_FOUND)
	message(STATUS "CppUnit not found, unit testing won't work")
endif(CPPUNIT_FOUND)

# (perhaps) TODO: In this version exec_prefix, libdir, and includedir
# are always subdirectories of prefix. This restriction didn't exist
# while using autoconf.
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libsandbox.pc.cmake ${sandboxlib_BINARY_DIR}/libsandbox.pc @ONLY)
install(FILES ${sandboxlib_BINARY_DIR}/libsandbox.pc DESTINATION lib/pkgconfig/)

message("CURSRCDIR ${CMAKE_CURRENT_SOURCE_DIR}")

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${sandboxlib_PACKAGE_DESCRIPTION})
set(CPACK_PACKAGE_VENDOR ${sandboxlib_PACKAGE_VENDOR})
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
set(CPACK_PACKAGE_VERSION_MAJOR "0")
set(CPACK_PACKAGE_VERSION_MINOR "1")
set(CPACK_PACKAGE_VERSION_PATCH "0")
#set(CPACK_INSTALL_CMAKE_PROJECTS "lib")
#set(CPACK_PACKAGE_EXECUTABLES "lua" "lua")

# TODO check why adding TBZ2 here doesn't influence command "make package_source"
set(CPACK_SOURCE_GENERATOR "TGZ")
#SET(CPACK_GENERATOR "TBZ2;TGZ")
if(EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake")
	include(CPack)
endif(EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake")
