# If libutempter is not available on the system, we build
# kwrited as an executable that is installed with setuid root
# (or setgid utmp) in order to be able to login successfully
# (and thus be useful, as kwrited does not work at all if it
# cannot login to the terminal device that it opens).
# This is required (at least) on Debian and its derivatives and
# opensolaris (afaict).

# This check is not very nice... what we should really check is
# whether libkpty was built with utempter support or not.
# This is the same check that libkpty does, so, assuming that
# kdebase/workspace is being built on the same system that libkpty was
# built, this should tell us if likpty was built with utempter or not.
check_library_exists(utempter addToUtmp "" HAVE_ADDTOUTEMP)
check_include_files(utempter.h HAVE_UTEMPTER_H)
if (NOT HAVE_ADDTOUTEMP OR NOT HAVE_UTEMPTER_H)
    set(BUILD_AS_EXECUTABLE 1)
endif (NOT HAVE_ADDTOUTEMP OR NOT HAVE_UTEMPTER_H)

configure_file(config-kwrited.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kwrited.h)

if (BUILD_AS_EXECUTABLE)
    kde4_add_executable(kwrited kwrited.cpp)
    target_link_libraries(kwrited  ${KDE4_KDEUI_LIBS} ${KDE4_KPTY_LIBS})
    install(TARGETS kwrited  ${INSTALL_TARGETS_DEFAULT_ARGS} )
    install(CODE "
               set(KWRITED_PATH \"\$ENV{DESTDIR}${BIN_INSTALL_DIR}/kwrited\")

               # On Debian we setgid instead of setuid.
               # Feel free to add checks here for other distros as well, if needed.
               if (EXISTS /etc/debian_version)
                   set(SETGID 1)
                   set(SETGID_GROUP utmp)
               endif (EXISTS /etc/debian_version)

               if (SETGID)
                   execute_process(COMMAND sh -c \"chgrp \${SETGID_GROUP} '\${KWRITED_PATH}' && chmod g+s '\${KWRITED_PATH}'\")
               else (SETGID)
                   execute_process(COMMAND sh -c \"chown root '\${KWRITED_PATH}' && chmod u+s '\${KWRITED_PATH}'\")
               endif(SETGID)
           ")

    install(FILES kwrited-autostart.desktop DESTINATION ${AUTOSTART_INSTALL_DIR})
else (BUILD_AS_EXECUTABLE)
    kde4_add_plugin(kded_kwrited kwrited.cpp)
    target_link_libraries(kded_kwrited  ${KDE4_KDEUI_LIBS} ${KDE4_KPTY_LIBS})
    install(TARGETS kded_kwrited  DESTINATION ${PLUGIN_INSTALL_DIR} )

    install(FILES kwrited.desktop DESTINATION ${SERVICES_INSTALL_DIR}/kded)
endif (BUILD_AS_EXECUTABLE)

# The .notifyrc file is common
install(FILES kwrited.notifyrc DESTINATION ${DATA_INSTALL_DIR}/kwrited)
