# MIT License
#
# Copyright (c) 2025 Jesper Zedlitz <jesper@zedlitz.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import os
import re


generator_regex = re.compile(r'<meta\s+name="generator"\s+content="([^"]+)"', re.IGNORECASE)

generator_info = {}

for filename in os.listdir():
    if filename.endswith(".html"):
        rs = filename.replace('.html','');
        try:
            with open(filename, 'r', encoding='utf-8') as file:
                content = file.read()
                match = generator_regex.search(content)
                if match:
                    generator = match.group(1)
                    if 'WordPress' in generator:
                        generator = 'WordPress'
                    generator_info[rs] = generator
                elif '<meta name="designer" content="Advantic' in content:
                    generator_info[rs] = 'Advantic'
                elif 'link href="https://layout.verwaltungsportal.de/global/' in content:
                    generator_info[rs] = 'verwaltungsportal.de'
                elif '<link rel="dns-prefetch" href="//fonts.nol-is.de" />' in content:
                    generator_info[rs] = 'NOLIS'
                elif ' content="CMS ionas3'  in content:
                    generator_info[rs] = 'CMS ionas3'
                elif 'href="https://strato-editor.com/.cm4all'  in content:
                    generator_info[rs] = 'Strato Editor'
                elif '<link rel="stylesheet" href="/css/teaser.css">'  in content:
                    # Dies ist eine besonders schlechte Heuristik.
                    generator_info[rs] = 'Craft CMS'
                else:
                    generator_info[rs] = "Kein Generator gefunden"
        except Exception as e:
            print(f"Fehler beim Verarbeiten von {filename}: {e}")

print("rs,software")

for rs, generator in generator_info.items():
    generator = generator.replace(',','\\,')
    print(f"{rs},{generator}")
