#!/usr/bin/env bash
set -euo pipefail

files=(
  "all_videos.txt"
  "clips.txt"
  "highlights.txt"
  "past_broadcasts.txt"
  "uploads.txt"
)

declare -A locations

for f in "${files[@]}"; do
  while IFS= read -r url; do
    [[ -z "$url" ]] && continue
    if [[ -z "${locations[$url]:-}" ]]; then
      locations[$url]="$f"
    else
      # avoid listing the same file twice
      if ! [[ ",${locations[$url]}," == *",$f,"* ]]; then
        locations[$url]+=", $f"
      fi
    fi
  done < "$f"
done

for url in "${!locations[@]}"; do
  # split on comma+space to count distinct files
  IFS=', ' read -r -a parts <<< "${locations[$url]}"
  if (( ${#parts[@]} > 1 )); then
    echo "$url found in: ${locations[$url]}"
  fi
done
