#!/bin/bash

# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
    echo "Sum up all websites located in the EU"
    echo "Usage: $0 <geolocation.csv>"
    exit 1
fi

CSV_FILE="../eu_countries.csv"
SEARCH_FILE="$1"
TOTAL_COUNT=0

# Read each line from the CSV file
while IFS=, read -r SEARCH_TERM SEARCH_TERM2; do
    # Search for the term in the second file and count matches
    MATCH_COUNT=$(grep -F "$SEARCH_TERM" "$SEARCH_FILE" | awk '{print $1}')
    # If MATCH_COUNT is empty or unset, set it to zero
    MATCH_COUNT=${MATCH_COUNT:-0}

    echo "Found $MATCH_COUNT websites for the country $SEARCH_TERM2 ($SEARCH_TERM) in $SEARCH_FILE" 
    # Add the count to the total
    TOTAL_COUNT=$((TOTAL_COUNT + MATCH_COUNT))
done < "$CSV_FILE"

# Output the total count of matches
echo "Total websites in the EU: $TOTAL_COUNT"

