import os
import requests
import sys
import re

# file in which fqdn server configs are deposit
config_path = '{{nginx.directories.http.servers}}'

# Initialize the error counter
error_counter = 0

# Regex pattern to match domain.tld or *.domain.tld
pattern = re.compile(r"^(?:[\w-]+\.)*[\w-]+\.[\w-]+\.conf$")

# Iterate over each file in the configuration directory
for filename in os.listdir(config_path):
    if filename.endswith('.conf') and pattern.match(filename):
        # Extract the domain and subdomain from the filename
        domain = filename.replace('.conf', '')
        parts = domain.split('.')
        
        # Prepare the URL and expected status codes
        url = f"{{ web_protocol }}://{domain}"
        
        # Default: Expect status code 200 or 302 for a domain
        expected_statuses = [200,302]
        
        redirected_domains = [domain['source'] for domain in {{redirect_domain_mappings}}]
        redirected_domains.append("{{domains.mailu}}")

        # Determine expected status codes based on the domain
        if domain == '{{domains.listmonk}}':
            expected_statuses = [404]
        elif parts[0] == 'www' or domain in redirected_domains:
            expected_statuses = [301]
        elif domain == '{{domains.yourls}}':
            expected_statuses = [403]

        try:
            # Send a HEAD request to get only the response header
            response = requests.head(url)

            # Check if the status code matches the expected statuses
            if response.status_code in expected_statuses:
                print(f"{domain}: OK")
            else:
                print(f"{domain}: ERROR: Expected {expected_statuses}. Got {response.status_code}.")
                error_counter += 1
        except requests.RequestException as e:
            # Handle exceptions for requests like connection errors
            print(f"{domain}: error due to {e}")
            error_counter += 1

if error_counter > 0:
    print(f"Warning: {error_counter} domains responded with an unexpected https status code.")

# Exit the script with the number of errors as the exit code
sys.exit(error_counter)
