diff --git a/CMakeLists.txt b/CMakeLists.txt index d9ea427..88d723f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,14 +66,14 @@ if (${WinBuild}) # or add_definitions(-DCURL_STATICLIB) # windows config path is same as executable - set(CONFIG_PATH "./iprefresher.cfg") + set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") else () message(STATUS "using nativ gcc toolchain.") set(LIBSUFFIX .so) set(SUFFIX "") # set /etc/ config path - set(CONFIG_PATH "/etc/iprefresher.cfg") + set(CONFIG_PATH "\"/etc/\"") endif () # test compiler settings and enable languages here @@ -159,7 +159,8 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h namespace Version { const std::string VERSION = \"${PROJECT_VERSION}\"; - const std::string ConfigDir = \"${CONFIG_PATH}\"; + const std::string ConfigDir = ${CONFIG_PATH}; + const std::string ConfName = \"iprefresher.cfg\"; const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\"; }" ) @@ -198,6 +199,9 @@ ELSE () set_target_properties(iprefresher PROPERTIES SUFFIX ".exe") install(TARGETS iprefresher DESTINATION .) + # create config directory + install(DIRECTORY DESTINATION "../Common\ Files/iprefresher") + # install .dll dependencies # todo check if files exist... install(FILES /usr/${TOOLCHAIN_PREFIX}/sys-root/mingw/bin/libcurl-4.dll diff --git a/src/Config.cpp b/src/Config.cpp index 32aba2a..217bdc7 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -6,6 +6,7 @@ #include #include #include +#include std::string Config::dynuapikey; std::string Config::domainid; //id of the dynu domain @@ -19,13 +20,38 @@ bool Config::telegramSupport; bool Config::readConfig() { libconfig::Config cfg; try { - cfg.readFile(Version::ConfigDir.c_str()); + cfg.readFile(std::string(Version::ConfigDir + Version::ConfName).c_str()); } catch (const libconfig::FileIOException &fioex) { std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl; + // check if config folder exists + struct stat info{}; + + if (stat(Version::ConfigDir.c_str(), &info) != 0) { + Logger::warning("The config folder doesn't exist. Trying to create it."); + +#ifdef __unix + int check = mkdir(Version::ConfigDir.c_str(), 777); +#else + int check = mkdir(Version::ConfigDir.c_str()); +#endif + + // check if directory is created or not + if (!check) + Logger::message("config directory successfully created. "); + else + Logger::error("unable to create config directory."); + + } else if (info.st_mode & S_IFDIR) { + Logger::debug("config directory exists already"); + } else { + Logger::error("A file exists with the same name as the config dir should be"); + } + + std::ofstream myfile; - myfile.open(Version::ConfigDir); + myfile.open(Version::ConfigDir + Version::ConfName); if (myfile.is_open()) { myfile << Version::SAMPLECONFIG; myfile.close();