#!/usr/bin/env python3

# Script to check all Axpo AS entries in the WHOIS database

import os
import sys
import re
from datetime import datetime

# Ensure the AS database directory exists
if not os.path.exists("db/as"):
    print("AS database directory not found!")
    sys.exit(1)

# Get all AS files
as_files = os.listdir("db/as/")

# Function to check if an AS entry belongs to Axpo
def is_axpo_as(file_path):
    with open(file_path, "r") as f:
        content = f.read()
        # Check if "Axpo" is in the organization field
        if re.search(r"Organization:\s+.*Axpo.*", content, re.IGNORECASE):
            return True
    return False

# Find all Axpo AS entries
axpo_as_entries = []
for as_file in as_files:
    file_path = os.path.join("db/as", as_file)
    if is_axpo_as(file_path):
        axpo_as_entries.append(as_file)

# Print results
if axpo_as_entries:
    print(f"Found {len(axpo_as_entries)} Axpo AS entries:")
    for as_id in sorted(axpo_as_entries):
        # Read the organization name
        with open(os.path.join("db/as", as_id), "r") as f:
            content = f.read()
            org_match = re.search(r"Organization:\s+(.*)", content)
            org_name = org_match.group(1) if org_match else "Unknown"
            
            # Read the ISD
            isd_match = re.search(r"ISD:\s+(.*)", content)
            isd = isd_match.group(1) if isd_match else "Unknown"
            
            print(f"AS {as_id} - {org_name} (ISD: {isd})")
else:
    print("No Axpo AS entries found.")
