#
# CMake project file for UNRES 
#
cmake_minimum_required(VERSION 2.8)
   
project(UNRESPACK Fortran C)

set(UNRES_MAJOR 4)
set(UNRES_MINOR 1)
set(UNRES_PATCH 0)
set(UNRES_VERSION ${UNRES_MAJOR}.${UNRES_MINOR}.${UNRES_PATCH})

#======================================
# MACROS
#======================================

# Get system date
MACRO (TODAY RESULT)
    IF (WIN32)
        EXECUTE_PROCESS(COMMAND "date" "/T" OUTPUT_VARIABLE ${RESULT})
        string(REGEX REPLACE "(..)/(..)/..(..).*" "\\3\\2\\1" ${RESULT} ${${RESULT}})
    ELSEIF(UNIX)
        EXECUTE_PROCESS(COMMAND "date" OUTPUT_VARIABLE ${RESULT})
        string(REGEX REPLACE "(...) (...) (.+) (..:..:..) (.+) (....).*" "\\1 \\2 \\3 \\4 \\5 \\6" ${RESULT} ${${RESULT}})
    ELSE (WIN32)
        MESSAGE(SEND_ERROR "date not implemented")
        SET(${RESULT} 000000)
    ENDIF (WIN32)
ENDMACRO (TODAY)

# foramt variables used in cinfo.f 
MACRO (CINFO_FORMAT FN VN VD)
# 50 znakow
# 73 w calej linii
#        write(iout,*)'INSTALL_DIR = /users/software/mpich-1.2.7p1_int...'
    string(LENGTH "${VN}" VNLEN)
    string(LENGTH "${VD}" VDLEN)
    set(STR "${VN} ${VD}")
    string(LENGTH "${STR}" SUMA)
    math(EXPR STRLEN 50-${VNLEN})
#    message("lancuch=${STRLEN}") 
# Fit in one line?
# No.
    if(SUMA GREATER 50)	
        string(SUBSTRING "${STR}" 0 50 STR_OUT) 
#        message("        write(iout,*)'${VAR} = ${STR_OUT}'") 
        file(APPEND ${FN} "       write(iout,*)'${STR_OUT}'\n")
        math(EXPR STRLEN ${SUMA}-50)
        string(SUBSTRING "${STR}" 50 ${STRLEN} STR)
        string(LENGTH "${STR}" STRLEN)
        while(STRLEN GREATER 48)
#            message("Przycinam lancuch")
            string(SUBSTRING "${STR}" 0 48 STR_OUT)  
            file(APPEND ${FN} "       write(iout,*)'  ${STR_OUT}'\n")
            math(EXPR STRLEN ${STRLEN}-49)
            string(SUBSTRING "${STR}" 49 ${STRLEN} STR)
            string(LENGTH "${STR}" STRLEN)
        endwhile(STRLEN GREATER 48)   
        file(APPEND ${FN} "       write(iout,*)'  ${STR}'\n")
#        MESSAGE("DLUGOSC = ${VNLEN}; DLUGOSCD = ${VDLEN}; SUMA=${SUMA}; ${VAR} = ${${VAR}} " )
# Yes
    else(SUMA GREATER 50)
	file(APPEND ${FN} "       write(iout,*)'${STR}'\n")
    endif(SUMA GREATER 50)
ENDMACRO (CINFO_FORMAT)

# Some MPI wrappers pass double include paths
# This macro fixes broken by semicolon occurence in path
MACRO (FIX_DBL_INCLUDE RESULT)
  string(REPLACE ";" " -I" ${RESULT} "${${RESULT}}")
ENDMACRO (FIX_DBL_INCLUDE)

#======================================
# CTest stuff
#======================================A

include(CTest)
enable_testing()
 

#======================================
# Fortran compilers stuff
#======================================
# Get the compiler name
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)

# Altough cmake enables you to set some compiler definitions it seems by default they are ignored.                 
# This is a workaround to pass compiler definitions (preprocesor flags) to fortran compilers like ifort
SET(CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER>  ${CMAKE_START_TEMP_FILE} <FLAGS> <DEFINES> -c <SOURCE> -o <OBJECT> ${CMAKE_END_TEMP_FILE}")

   
# make sure that the default is a RELEASE
if (NOT CMAKE_BUILD_TYPE)
  set (CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo." FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "None" "Debug" "Release" "RelWithDebInfo")
endif (NOT CMAKE_BUILD_TYPE)


if (CMAKE_BUILD_TYPE STREQUAL "Release")
  # Set makefile verbosity off for Release builds
  set( CMAKE_VERBOSE_MAKEFILE 0 )
else()
  # Set makefile verbosity on for other builds
  set( CMAKE_VERBOSE_MAKEFILE 1 )
endif (CMAKE_BUILD_TYPE STREQUAL "Release")

# Default Install Path

set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/bin" CACHE PATH "Binary install directory " FORCE)

#=======================================  
# Set the varous build variables 
#=======================================

# Set force field
if (NOT UNRES_FF)
  set(UNRES_MD_FF "GAB" CACHE STRING "Choose the force field, options are: GAB E0LL2Y NEWCORR" )
  set_property(CACHE UNRES_MD_FF PROPERTY STRINGS "GAB" "E0LL2Y NEWCORR")
endif (NOT UNRES_FF)

# Use of MPI library (default ON)
option(UNRES_WITH_MPI "Choose whether or not to use MPI library" ON )

option(UNRES_NEWGRAD "Choose whether or not to use NEWGRAD" OFF )


#=================================
# MPI stuff
#=================================

# Note for the future - use find package to get MPI 
find_package(MPI QUIET)

if (MPI_Fortran_FOUND)
  message("MPI found")
  FIX_DBL_INCLUDE(MPI_Fortran_INCLUDE_PATH)
  message(MPI_Fortran_INCLUDE_PATH)
  message(${MPI_Fortran_INCLUDE_PATH})
  message(MPI_Fortran_LIBRARIES)
  message(${MPI_Fortran_LIBRARIES})
  option(UNRES_SRUN "Use srun instead of mpiexec ?" OFF )
  option(UNRES_MPIRUN "Use mpirun instead of mpiexec ?" OFF )
else()
  message("MPI not found - disabling MPI compile flags ")
  set ( UNRES_WITH_MPI "OFF")
endif(MPI_Fortran_FOUND)	

#======================================
# Detect system architecture
#=======================================

if( CMAKE_SIZEOF_VOID_P EQUAL 4 )
  set(architektura "32")
else (CMAKE_SIZEOF_VOID_P EQUAL 4)
  set(architektura "64")
endif( CMAKE_SIZEOF_VOID_P EQUAL 4 )

message("Detected ${architektura}-bit architecture")	

#=======================================
# Find other libraries
#=======================================

#=======================================
# Add source files
#=======================================     


# order is important 
# the most independent ones should go first

add_subdirectory(source/xdrf)
add_subdirectory(source/unres)
add_subdirectory(source/wham)
add_subdirectory(source/cluster)
