include (FindPkgConfig)
pkg_check_modules (GTKLIBS REQUIRED gtk+-3.0)
if(ENABLE_THUNARX)
	pkg_check_modules (THUNARX REQUIRED thunarx-3)
endif()

set (LOCALE_DIR ${CMAKE_INSTALL_PREFIX}/share/locale)
set (UI_FILE ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/${PROJECT_NAME}.ui)
configure_file (config.h.cmake config.h)
include_directories (${CMAKE_BINARY_DIR}/src)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -rdynamic")

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_executable (${PROJECT_NAME} ${PROJECT_NAME}.c)
target_link_libraries (${PROJECT_NAME} ${GTKLIBS_LDFLAGS})
include_directories (${GTKLIBS_INCLUDE_DIRS} ${THUNARX_INCLUDE_DIRS})

if(ENABLE_THUNARX)
	include_directories (${THUNARX_INCLUDE_DIRS})
	add_library (thunar-sgsearch STATIC thunar-sgsearch.c)
	target_link_libraries (thunar-sgsearch ${GTKLIBS_LDFLAGS} ${THUNARX_LDFLAGS})
	add_library (thunar-sgsearch-plugin SHARED thunar-sgsearch-plugin.c)
	target_link_libraries (thunar-sgsearch-plugin thunar-sgsearch ${GTKLIBS_LDFLAGS} ${THUNARX_LDFLAGS})
endif()

install (TARGETS ${PROJECT_NAME} DESTINATION bin)
install (FILES ${PROJECT_NAME}.ui DESTINATION share/${PROJECT_NAME})
install (FILES ${PROJECT_NAME}.desktop DESTINATION share/applications)
if (EXISTS "${${PROJECT_NAME}_SOURCE_DIR}/src/${PROJECT_NAME}.png")
	install (FILES ${PROJECT_NAME}.png DESTINATION share/pixmaps)
	add_custom_target (icons ALL)
	foreach (WXH 16x16 24x24 32x32 48x48 64x64)
		add_custom_command (TARGET icons COMMAND mkdir -p ${${PROJECT_NAME}_BINARY_DIR}/${WXH})
		add_custom_command (TARGET icons COMMAND convert -resize ${WXH} ${${PROJECT_NAME}_SOURCE_DIR}/src/${PROJECT_NAME}.png ${${PROJECT_NAME}_BINARY_DIR}/${WXH}/${PROJECT_NAME}.png)
		install (FILES ${${PROJECT_NAME}_BINARY_DIR}/${WXH}/${PROJECT_NAME}.png DESTINATION share/icons/hicolor/${WXH}/apps/)
	endforeach()
endif()
if (EXISTS "${${PROJECT_NAME}_SOURCE_DIR}/src/${PROJECT_NAME}.svg")
	install (FILES ${PROJECT_NAME}.svg DESTINATION share/icons/hicolor/scalable/apps/)
endif()
if(ENABLE_THUNARX)
	install (TARGETS thunar-sgsearch-plugin DESTINATION ${CMAKE_INSTALL_LIBDIR}/thunarx-3)
endif()

add_custom_target (uninstall)
add_custom_command (TARGET uninstall COMMAND xargs rm -f < ${CMAKE_BINARY_DIR}/install_manifest.txt)
add_custom_command (TARGET uninstall COMMAND rmdir ${CMAKE_INSTALL_PREFIX}/doc/${PROJECT_NAME}-${PROJECT_VERSION})
add_custom_command (TARGET uninstall COMMAND rmdir ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME})
if(ENABLE_THUNARX)
	add_custom_command (TARGET uninstall COMMAND rmdir --ignore-fail-on-non-empty ${CMAKE_INSTALL_PREFIX}/lib/thunarx-3)
endif()
