#!/bin/bash

# Define the domain name(s) and the output file
DOMAINS=(
    "clutchbbs.com"
    "bbs.erb.pw"
    "bbs.bottomlessabyss.net"
)

OUTPUT_FILE="/mystic/data/whitelist.txt"
[[ ! -f $OUTPUT_FILE ]] && touch $OUTPUT_FILE && echo "Created $OUTPUT_FILE"

# Get the current date and time
CURRENT_DATE=$(date '+%Y-%m-%d %H:%M:%S')

for DOMAIN in ${DOMAINS[@]}; do
    echo "Working with domain: $DOMAIN"

    # Get the IP address of the domain
    IP_ADDRESS=$(dig +short "$DOMAIN" | tail -n1)

    # Check if the IP address was retrieved
    if [ -z "$IP_ADDRESS" ]; then
        echo "Failed to retrieve IP address for $DOMAIN."
        exit 1
    else
        # Check if the IP address is already in the output file
        if ! grep -q "$IP_ADDRESS" "$OUTPUT_FILE"; then
            if ! grep -q "$DOMAIN" "$OUTPUT_FILE"; then
                # Append the date and time and the IP address to the output file
                echo "; $DOMAIN" >> "$OUTPUT_FILE"
                echo "; $CURRENT_DATE" >> "$OUTPUT_FILE"
                echo "$IP_ADDRESS" >> "$OUTPUT_FILE"
                echo "IP address $IP_ADDRESS added to $OUTPUT_FILE."
            else
                sed -i "/$DOMAIN/{n;n;s/.*/$IP_ADDRESS/;}" "$OUTPUT_FILE"
                echo "Updated the IP address for $DOMAIN"
            fi
        else
            echo "IP address $IP_ADDRESS is already in $OUTPUT_FILE."
        fi
    fi
done
