# CMake-Skript für intelligente Konfigurationsdatei-Installation # Fügt nur fehlende Keys hinzu, ohne bestehende Konfiguration zu überschreiben # Pfade setzen set(CONFIG_FILE "/etc/yourpart/daemon.conf") set(TEMPLATE_FILE "/etc/yourpart/daemon.conf.example") # Prüfe ob Template existiert (wurde von CMake installiert) if(NOT EXISTS "${TEMPLATE_FILE}") # Fallback 1: Versuche Template im Source-Verzeichnis zu finden # CMAKE_CURRENT_LIST_DIR zeigt auf cmake/ während der Installation get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE) set(TEMPLATE_FILE_FALLBACK "${PROJECT_ROOT}/daemon.conf") # Fallback 2: Versuche über CMAKE_SOURCE_DIR (falls verfügbar) if(DEFINED CMAKE_SOURCE_DIR AND EXISTS "${CMAKE_SOURCE_DIR}/daemon.conf") set(TEMPLATE_FILE "${CMAKE_SOURCE_DIR}/daemon.conf") message(STATUS "Verwende Template aus CMAKE_SOURCE_DIR: ${TEMPLATE_FILE}") elseif(EXISTS "${TEMPLATE_FILE_FALLBACK}") set(TEMPLATE_FILE "${TEMPLATE_FILE_FALLBACK}") message(STATUS "Verwende Template aus Source-Verzeichnis: ${TEMPLATE_FILE}") else() message(FATAL_ERROR "Template-Datei nicht gefunden!") message(FATAL_ERROR " Gesucht in: ${TEMPLATE_FILE}") message(FATAL_ERROR " Fallback 1: ${TEMPLATE_FILE_FALLBACK}") if(DEFINED CMAKE_SOURCE_DIR) message(FATAL_ERROR " Fallback 2: ${CMAKE_SOURCE_DIR}/daemon.conf") endif() endif() else() message(STATUS "Verwende installierte Template-Datei: ${TEMPLATE_FILE}") endif() # Prüfe ob Ziel-Verzeichnis existiert if(NOT EXISTS "/etc/yourpart") message(STATUS "Erstelle Verzeichnis /etc/yourpart...") execute_process( COMMAND ${CMAKE_COMMAND} -E make_directory "/etc/yourpart" COMMAND_ERROR_IS_FATAL ANY ) endif() # Prüfe ob Config-Datei existiert if(NOT EXISTS "${CONFIG_FILE}") message(STATUS "Konfigurationsdatei existiert nicht, erstelle neue...") execute_process( COMMAND ${CMAKE_COMMAND} -E copy "${TEMPLATE_FILE}" "${CONFIG_FILE}" COMMAND_ERROR_IS_FATAL ANY ) message(STATUS "Neue Konfigurationsdatei erstellt: ${CONFIG_FILE}") else() message(STATUS "Konfigurationsdatei existiert bereits, prüfe auf fehlende Keys...") # Verwende ein Python-Skript für intelligentes Merging # (CMake hat keine gute Unterstützung für komplexe String-Manipulation) # Erstelle temporäres Python-Skript im Build-Verzeichnis set(MERGE_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/merge-config.py") # Erstelle Python-Skript file(WRITE "${MERGE_SCRIPT}" "#!/usr/bin/env python3 import sys import re import os def merge_config(template_file, config_file): \"\"\"Fügt fehlende Keys aus Template zur Config hinzu, ohne bestehende zu überschreiben\"\"\" # Lese bestehende Config existing_keys = {} existing_lines = [] if os.path.exists(config_file): with open(config_file, 'r') as f: for line in f: existing_lines.append(line.rstrip()) # Extrahiere Key=Value Paare match = re.match(r'^\\s*([^#=]+?)\\s*=\\s*(.+?)\\s*$', line) if match: key = match.group(1).strip() value = match.group(2).strip() existing_keys[key] = value # Lese Template new_keys = {} if not os.path.exists(template_file): print(f'Fehler: Template-Datei {template_file} nicht gefunden!', file=sys.stderr) return False with open(template_file, 'r') as f: for line in f: # Extrahiere Key=Value Paare match = re.match(r'^\\s*([^#=]+?)\\s*=\\s*(.+?)\\s*$', line) if match: key = match.group(1).strip() value = match.group(2).strip() new_keys[key] = value # Füge fehlende Keys hinzu added_count = 0 for key, value in new_keys.items(): if key not in existing_keys: existing_lines.append(f'{key}={value}') print(f'Füge fehlenden Key hinzu: {key}') added_count += 1 # Schreibe aktualisierte Config if added_count > 0: with open(config_file, 'w') as f: for line in existing_lines: f.write(line + '\\n') print(f'{added_count} neue Keys hinzugefügt') else: print('Keine neuen Keys hinzugefügt - Konfiguration ist aktuell') return True if __name__ == '__main__': if len(sys.argv) != 3: print('Verwendung: merge-config.py