#    Copyright (C) 2013 Ahmet Öztürk (aoz_2@yahoo.com)
#
#    This file is part of Lifeograph.
#
#    Lifeograph is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Lifeograph 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Lifeograph.  If not, see <http://www.gnu.org/licenses/>.


cmake_minimum_required( VERSION 2.6 )
project( Lifeograph )

include( ${PROJECT_SOURCE_DIR}/cmake/definitions.cmake )

add_definitions( -Wall -DHAVE_CONFIG_H )

# DEBUG BUILD
if( ${CMAKE_BUILD_TYPE} MATCHES Debug )
    set( DEBUG_BUILD 1 )
else()
    set( DEBUG_BUILD 0 )
endif()

# PACKAGES
find_package( PkgConfig REQUIRED )

find_program( INTLTOOL "intltool-merge" )
if( ${INTLTOOL} MATCHES "INTLTOOL-NOTFOUND" )
    message( "Intltool package not found. It needs to be installed for building Lifeograph" )
    return()
endif()

find_library( GCRYPT gcrypt )
if( ${GCRYPT} MATCHES "GCRYPT-NOTFOUND" )
    message( "Gcrypt library not found. It needs to be installed for building Lifeograph" )
    return()
endif()

pkg_check_modules( GTKMM REQUIRED gtkmm-3.0>=3.4 )
pkg_check_modules( ENCHANT REQUIRED enchant )

# CONFIGURATION HEADER FILE
configure_file( ${PROJECT_SOURCE_DIR}/config.h.in ${LOCAL_INCL_DIR}/config.h )

# add path of config.h
include_directories( ${LOCAL_INCL_DIR} )

include_directories( ${GTKMM_INCLUDE_DIRS} ${ENCHANT_INCLUDE_DIRS} )

#link_directories( ${GTKMM_LIBRARY_DIRS} ${ENCHANT_LIBRARY_DIRS} )

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUILD_DIR} )

# TRANSLATION FILE OPTIONS INVOKED MANUALLY WHEN REQUIRED
add_custom_command( OUTPUT ${pot_file}
                    COMMAND intltool-update -p -g ${PACKAGE_NAME}
                    DEPENDS ${SOURCE_FILES}
                    WORKING_DIRECTORY ${po_dir} )

add_custom_target( update-translations
                   COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/msgmerge.cmake
                   DEPENDS ${pot_file}
                   WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )

# TRANSLATION FILE OPTIONS DURING BUILD
# based on Remmina code at https://github.com/FreeRDP/Remmina/blob/master/cmake/GETTEXT.cmake
set( GETTEXT_MSGFMT_EXECUTABLE msgfmt )
set( mo_files )

if( NOT EXISTS ${MO_DIR} )
    file( MAKE_DIRECTORY ${MO_DIR} )
endif()

file( GLOB po_files ${po_dir}/*.po )
foreach( po_file ${po_files} )
    get_filename_component( lang ${po_file} NAME_WE )
    set( mo_file ${MO_DIR}/${lang}.mo )
    add_custom_command( OUTPUT ${mo_file}
                        COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${mo_file} ${po_file}
                        DEPENDS ${po_file} )
    install( FILES ${mo_file}
             DESTINATION share/locale/${lang}/LC_MESSAGES
             RENAME ${PACKAGE_NAME}.mo )
    set( mo_files ${mo_files} ${mo_file} )
endforeach()

add_custom_command( OUTPUT ${desktop_file}
                    COMMAND intltool-merge -d -u -q po ${desktop_file}.in ${desktop_file}
                    DEPENDS ${desktop_file}.in )

add_custom_target( format-translations ALL DEPENDS ${mo_files} ${desktop_file} )

# TARGETS
add_executable( ${PACKAGE_NAME} ${SOURCE_FILES} )

target_link_libraries( ${PACKAGE_NAME} ${GTKMM_LIBRARIES} ${ENCHANT_LIBRARIES} ${GCRYPT} )

# CUSTOM TARGETS
if( ${CMAKE_BUILD_TYPE} MATCHES Debug )
# BACKUP
    add_custom_target( BACKUP ALL
                       COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/backup.cmake
                       DEPENDS ${BUILD_DIR}/${PACKAGE_NAME}
                       WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )

# rev_no.h and build_time.h
    add_custom_target( TARGET_STAMP_HEADERS
                       COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/rev_no.cmake
                       WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )

	add_dependencies( ${PACKAGE_NAME} TARGET_STAMP_HEADERS )
endif()

# INSTALL
install( TARGETS ${PACKAGE_NAME} DESTINATION bin )

install( FILES lifeograph.appdata.xml DESTINATION share/appdata )

install( FILES icons/lifeograph-48.png DESTINATION share/pixmaps RENAME lifeograph.png )
install( FILES icons/lifeograph-48.png DESTINATION share/icons/hicolor/48x48/apps RENAME lifeograph.png )
install( FILES icons/lifeograph-32.png DESTINATION share/icons/hicolor/32x32/apps RENAME lifeograph.png )
install( FILES icons/lifeograph-24.png DESTINATION share/icons/hicolor/24x24/apps RENAME lifeograph.png )
install( FILES icons/lifeograph-22.png DESTINATION share/icons/hicolor/22x22/apps RENAME lifeograph.png )
install( FILES icons/lifeograph-16.png DESTINATION share/icons/hicolor/16x16/apps RENAME lifeograph.png )
install( FILES icons/scalable/lifeograph.svg DESTINATION share/icons/hicolor/scalable/apps )

#install( FILES lifeograph.desktop DESTINATION share/applications )

install( FILES icons/category_tags-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/category_tags-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/chapter-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/chapter-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/diary-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/diary-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/entry-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/entry-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/favorite-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/favorite-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/filter-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/filter_remove-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/filter_symbolic-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/gear-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/month-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/month-32.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/tag-16.png DESTINATION share/${PACKAGE_NAME}/icons )
install( FILES icons/tag-32.png DESTINATION share/${PACKAGE_NAME}/icons )

install( FILES ui/lifeograph.ui DESTINATION share/${PACKAGE_NAME}/ui )

install( DIRECTORY diaries/ DESTINATION share/${PACKAGE_NAME}/diaries )

# xdg: mime info
install( CODE "execute_process( COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/xdg.cmake
								WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )" )

# UNINSTALL
add_custom_target( uninstall
                   COMMAND ${CMAKE_COMMAND} -DDESTDIR=${CMAKE_INSTALL_PREFIX}
                   -P ${PROJECT_SOURCE_DIR}/cmake/uninstall.cmake
                   WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )

