check if config file exists with a post script (during package installation process)

This commit is contained in:
Lukas Heiligenbrunner 2020-05-01 16:47:22 +02:00
parent 2505e2cbf4
commit ae9aa1d423

View File

@ -10,16 +10,16 @@
cmake_minimum_required(VERSION 3.13)
project(iprefresher DESCRIPTION "Dynu ip refresher")
set(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
set(LIB_METHOD STATIC) #SHARED / STATIC
set(PROJECT_VERSION 1.3.1-dev)
SET(CMAKE_BUILD_TYPE Release) # manually SET build type (Release / Debug)
SET(LIB_METHOD STATIC) #SHARED / STATIC
SET(PROJECT_VERSION 1.3.1-dev)
option(BUILD_DOC "Build documentation" ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# config libs
message(STATUS "Config of Libraries")
@ -59,6 +59,17 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h
"\#pragma once\n#include <string>\nnamespace Version {\n const std::string VERSION = \"${PROJECT_VERSION}\";\n}"
)
#read sample config
FILE(READ ${CMAKE_SOURCE_DIR}/config/iprefresher.cfg SAMPLECONFIG)
# generate post script for checking if configuration already exists
FILE(WRITE ${CMAKE_SOURCE_DIR}/postinstall.sh
"#!/bin/bash
if [ ! -f /etc/iprefresher.cfg ]; then
cat > /etc/iprefresher.cfg <<- EOM
${SAMPLECONFIG}EOM
fi"
)
add_library(api ${LIB_METHOD}
src/api/API.cpp
@ -75,7 +86,7 @@ add_library(logger ${LIB_METHOD}
)
set(SOURCE
SET(SOURCE
src/main.cpp
src/IPRefresher.cpp
src/Config.cpp)
@ -86,16 +97,9 @@ add_executable(iprefresher ${SOURCE})
target_link_libraries(iprefresher api logger ${CURL_LIBRARIES} config++)
# INSTALL to SYSTEM #
set(CMAKE_INSTALL_PREFIX "/")
SET(CMAKE_INSTALL_PREFIX "/")
# install binaries
install(TARGETS iprefresher DESTINATION usr/bin)
# install config file if it doesn't exist
install(CODE "MESSAGE(STATUS \"Copy iprefresher.cfg to ${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg\")
IF (NOT EXISTS ${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/config/iprefresher.cfg
${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg)
ENDIF (NOT EXISTS ${CMAKE_INSTALL_PREFIX}etc/iprefresher.cfg)")
# install systemd service and enable it
install(FILES service/iprefresher.service DESTINATION lib/systemd/system)
@ -104,7 +108,7 @@ IF (UNIX)
message(STATUS "config of Package build")
SET(CPACK_DEB_COMPONENT_INSTALL 1)
SET(CPACK_OUTPUT_FILE_PREFIX packages)
set(CPACK_PACKAGING_INSTALL_PREFIX "/") # no prefix for package structure
SET(CPACK_PACKAGING_INSTALL_PREFIX "/") # no prefix for package structure
FIND_PROGRAM(RPMBUILD_EXECUTABLE rpmbuild)
FIND_PROGRAM(DEB_EXECUTABLE dpkg)
@ -114,8 +118,9 @@ IF (UNIX)
#check if rpm build is possible
if (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
message(STATUS "found rpm build executeable --> able to build rpm")
set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/lib/systemd/system" "/lib/systemd" "/lib" "/usr/local/bin" "/usr/local") # --> needed to not override existing folders
SET(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinstall.sh")
SET(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/lib/systemd/system" "/lib/systemd" "/lib" "/usr/local/bin" "/usr/local") # --> needed to not override existing folders
else (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
message(STATUS "not found rpm build tools --> not building rpm")
endif (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
@ -123,7 +128,8 @@ IF (UNIX)
#check if deb build is possible
if (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
message(STATUS "found deb build tools --> able to build deb")
set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
SET(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/postinstall.sh")
else (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
message(STATUS "not found deb build tools --> not building deb")
endif (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
@ -139,7 +145,8 @@ IF (UNIX)
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}")
SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner <lukas.heiligenbrunner@gmail.com>")
SET(CPACK_PACKAGE_SECTION "games")
set(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1), libconfig (>= 1.5.0)")
SET(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1), libconfig (>= 1.5.0)")
SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinstall.sh")
INCLUDE(CPack)
@ -155,9 +162,9 @@ if (BUILD_DOC)
message(STATUS "config of DOxygen build")
find_package(Doxygen)
if (DOXYGEN_FOUND)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# SET input and output files
SET(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
SET(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)