cmake_minimum_required(VERSION 2.6)

project( MathGL )
set(CMAKE_VERBOSE_MAKEFILE ON)
option(enable-double "Enable double precision in MathGL library")
option(enable-all "Enable all features")
option(enable-langall "Enable all language interfaces")
option(enable-pthread "Enable POSIX threads support")
option(enable-gsl "Enable gsl support")
option(enable-jpeg "Enable jpeg support")
option(enable-u3d "Enable u3d support")
option(enable-pdf "Enable pdf support")
option(enable-gif "Enable gif support")
option(enable-hdf4 "Enable hdf4 support")
option(enable-hdf5 "Enable hdf5 support")
option(enable-hdf5_18 "Enable hdf5 1.8 support")
option(enable-glut "Enable glut support")
option(enable-fltk "Enable fltk widget")
option(enable-wx "Enable wxWidget widget")
option(enable-qt "Enable Qt4 widget")
option(enable-python "Enable python interface")
option(enable-octave "Enable octave interface")
option(enable-doc "Enable documentation building")
if(enable-double)
    set(use_double 1)
endif(enable-double)
if(enable-all OR enable-gsl)
    set(use_gsl 1)
endif(enable-all OR enable-gsl)
if(enable-all OR enable-pthread)
    set(use_pthread 1)
endif(enable-all OR enable-pthread)
if(enable-all OR enable-jpeg)
    set(use_jpeg 1)
endif(enable-all OR enable-jpeg)
if(enable-all OR enable-u3d OR enable-pdf)
    set(use_u3d 1)
endif(enable-all OR enable-u3d OR enable-pdf)
if(enable-all OR enable-pdf)
    set(use_pdf 1)
endif(enable-all OR enable-pdf)
if(enable-all OR enable-gif)
    set(use_gif 1)
endif(enable-all OR enable-gif)
if(enable-all OR enable-hdf4)
    set(use_hdf4 1)
endif(enable-all OR enable-hdf4)
if(enable-all OR enable-hdf5)
    set(use_hdf5 1)
endif(enable-all OR enable-hdf5)
if(enable-hdf5_18)
    set(use_hdf5_18 1)
endif(enable-hdf5_18)
if(enable-all OR enable-glut)
    set(use_glut 1)
endif(enable-all OR enable-glut)
if(enable-all OR enable-fltk)
    set(use_fltk 1)
endif(enable-all OR enable-fltk)
if(enable-all OR enable-wx)
    set(use_wx 0)
endif(enable-all OR enable-wx)
if(enable-all OR enable-qt)
    set(use_qt 1)
endif(enable-all OR enable-qt)
if(enable-all OR enable-doc)
    set(use_doc 1)
endif(enable-all OR enable-doc)
if(enable-langall OR enable-python)
    set(use_python 1)
endif(enable-langall OR enable-python)
if(enable-langall OR enable-octave)
    set(use_octave 1)
endif(enable-langall OR enable-octave)

include_directories( ${MathGL_SOURCE_DIR}/include )

include(FindOpenGL)
if(NOT OPENGL_FOUND)
	message(SEND_ERROR "Couldn't find OpenGL libraries.")
endif(NOT OPENGL_FOUND)

include(FindPNG)
if(NOT PNG_FOUND)
	message(SEND_ERROR "Couldn't find PNG libraries.")
endif(NOT PNG_FOUND)

find_library(GSL_LIB gsl)
find_library(GSL_CBLAS_LIB gslcblas)
find_path(GSL_INCLUDE_DIR gsl/gsl_fft_complex.h)
if(NOT GSL_LIB OR NOT GSL_CBLAS_LIB OR NOT GSL_INCLUDE_DIR)
	if(use_gsl)
		message(SEND_ERROR "${GSL_LIB}")
		message(SEND_ERROR "${GSL_CBLAS_LIB}")
		message(SEND_ERROR "${GSL_INCLUDE_DIR}")
		message(SEND_ERROR "Couldn't find GSL libraries.")
	endif(use_gsl)
endif(NOT GSL_LIB OR NOT GSL_CBLAS_LIB OR NOT GSL_INCLUDE_DIR)

include(FindThreads)
if(NOT CMAKE_USE_PTHREADS_INIT)
	if(use_pthread)
		message(SEND_ERROR "Couldn't find POSIX threads libraries.")
	endif(use_pthread)
endif(NOT CMAKE_USE_PTHREADS_INIT)

if(use_u3d)
	find_library(U3D_LIB IDTF)
	if(NOT U3D_LIB)
		message(STATUS "Couldn't find U3D libraries.")
	endif(NOT U3D_LIB)
	find_path(U3D_INCLUDE_DIR u3d/SceneConverterLib.h)
	if(NOT U3D_INCLUDE_DIR)
		message(STATUS "Couldn't find U3D headers.")
	endif(NOT U3D_INCLUDE_DIR)
	message(STATUS "Found U3D libraries at: ${U3D_LIB}")
	message(STATUS "Found U3D headers: ${U3D_INCLUDE_DIR}")
endif(use_u3d)

if(use_pdf)
	find_library(HPDF_LIB hpdf)
	if(NOT HPDF_LIB)
		message(STATUS "Couldn't find libHaru.")
	endif(NOT HPDF_LIB)
	find_path(HPDF_INCLUDE_DIR hpdf_u3d.h)
	if(NOT HPDF_INCLUDE_DIR)
		message(STATUS "Couldn't find headers of u3d-enabled version of libHaru.")
	endif(NOT HPDF_INCLUDE_DIR)
	message(STATUS "Found libHaru library at: ${HPDF_LIB}")
	message(STATUS "Found libHaru headers: ${HPDF_INCLUDE_DIR}")
endif(use_pdf)

include(FindJPEG)
if(NOT JPEG_FOUND)
	if(use_jpeg)
		message(SEND_ERROR "Couldn't find JPEG libraries.")
	endif(use_jpeg)
endif(NOT JPEG_FOUND)

include(FindGIF)
if(NOT GIF_FOUND)
	if(use_gif)
		message(SEND_ERROR "Couldn't find GIF libraries.")
	endif(use_gif)
endif(NOT GIF_FOUND)

find_library(HDF4_LIB df mfhdf)
find_path(HDF4_INCLUDE_DIR hdf/mfhdf.h)
if(NOT HDF4_LIB OR NOT HDF4_INCLUDE_DIR)
	if(use_hdf4)
		message(SEND_ERROR "${HDF4_LIB}")
		message(SEND_ERROR "${HDF4_INCLUDE_DIR}")
		message(SEND_ERROR "Couldn't find HDF4 libraries.")
	endif(use_hdf4)
endif(NOT HDF4_LIB OR NOT HDF4_INCLUDE_DIR)

find_library(HDF5_LIB hdf5)
find_path(HDF5_INCLUDE_DIR hdf5.h)
if(NOT HDF5_LIB OR NOT HDF5_INCLUDE_DIR)
	if(use_hdf5 OR use_hdf5_18)
		message(SEND_ERROR "${HDF5_LIB}")
		message(SEND_ERROR "${HDF5_INCLUDE_DIR}")
		message(SEND_ERROR "Couldn't find HDF5 libraries.")
	endif(use_hdf5 OR use_hdf5_18)
endif(NOT HDF5_LIB OR NOT HDF5_INCLUDE_DIR)

include(FindGLUT)
if(NOT GLUT_FOUND)
	if(use_glut)
		message(SEND_ERROR "Couldn't find GLUT libraries.")
	endif(use_glut)
endif(NOT GLUT_FOUND)

include(FindFLTK)
if(NOT FLTK_FOUND)
	if(use_fltk)
		message(SEND_ERROR "Couldn't find FLTK libraries.")
	endif(use_fltk)
endif(NOT FLTK_FOUND)

FIND_PACKAGE(wxWidgets COMPONENTS base core gl)
if(NOT wxWidgets_FOUND)
	if(use_wx)
		message(SEND_ERROR "Couldn't find wxWidgets libraries.")
	endif(use_wx)
endif(NOT wxWidgets_FOUND)

FIND_PACKAGE(Qt4)
if(NOT QT4_FOUND)
	if(use_qt)
		message(SEND_ERROR "Couldn't find Qt4 libraries.")
	endif(use_qt)
endif(NOT QT4_FOUND)

include(FindPythonInterp)
if(NOT PYTHONINTERP_FOUND)
	if(use_python)
		message(SEND_ERROR "Couldn't find python interpreter.")
	endif(use_python)
endif(NOT PYTHONINTERP_FOUND)
include(FindPythonLibs)
if(NOT PYTHONLIBS_FOUND)
	if(use_python)
		message(SEND_ERROR "Couldn't find python development libraries.")
	endif(use_python)
endif(NOT PYTHONLIBS_FOUND)

include(FindSWIG)
if(NOT SWIG_FOUND)
	if(use_python)
		message(SEND_ERROR "Couldn't find swig needed for interfaces compiling.")
	endif(use_python)
endif(NOT SWIG_FOUND)

find_path(OCTAVE_INCLUDE_DIR octave/oct.h)
if(NOT OCTAVE_INCLUDE_DIR)
	if(use_octave)
		message(SEND_ERROR "Couldn't find octave/oct.h needed for octave interfaces compiling.")
	endif(use_octave)
endif(NOT OCTAVE_INCLUDE_DIR)

find_program(oct_prog octave)
if(NOT oct_prog)
	if(use_octave)
		message(SEND_ERROR "Couldn't find octave needed for octave interfaces compiling.")
	endif(use_octave)
endif(NOT oct_prog)

find_program(oct_mk mkoctfile)
if(NOT oct_mk)
	if(use_octave)
		message(SEND_ERROR "Couldn't find mkoctfile needed for octave interfaces compiling.")
	endif(use_octave)
endif(NOT oct_mk)
#execute_process(
#	COMMAND ${oct_prog} -q --eval \'strcat\(octave_config_info\("canonical_host_type"\), "-", octave_config_info\("api_version"\)\)'|sed -e 's/ans = //'
#	OUTPUT_VARIABLE OCTAVE_ARCH
#	)
#message(SEND_ERROR "${OCTAVE_ARCH}")

find_program(findmi makeinfo)
if(NOT findmi)
	if(use_doc)
		message(SEND_ERROR "Couldn't find makeinfo needed for documentation building.")
	endif(use_doc)
endif(NOT findmi)
find_program(findth texi2html)
if(NOT findth)
	if(use_doc)
		message(SEND_ERROR "Couldn't find texi2html needed for documentation building.")
	endif(use_doc)
endif(NOT findth)
find_program(findtp texi2pdf)
if(NOT findtp)
	if(use_doc)
		message(SEND_ERROR "Couldn't find texi2pdf needed for documentation building.")
	endif(use_doc)
endif(NOT findtp)

if(UNIX)
        add_definitions(-DNO_COLOR_ARRAY)
endif(UNIX)

if(WIN32)
#        add_definitions(-DWIN32 -D_MSC_VER)
        add_definitions(-DWIN32)
endif(WIN32)

if(NOT WIN32)
set(MGL_DATA_PATH "${CMAKE_INSTALL_PREFIX}/share/mathgl")
set(MGL_DOC_PATH "${CMAKE_INSTALL_PREFIX}/share/doc/mathgl")
set(MGL_FONT_PATH "${MGL_DATA_PATH}/fonts")
install(DIRECTORY fonts/ DESTINATION ${MGL_FONT_PATH}
                        PATTERN ".svn" EXCLUDE
                        PATTERN "*.vfm")
endif(NOT WIN32)

add_subdirectory( mgl )
add_subdirectory( widgets )
add_subdirectory( utils )
add_subdirectory( examples )
add_subdirectory( include )
add_subdirectory( lang )
add_subdirectory( texinfo )
