#!/bin/bash

# Database and table details
DATABASE="user_data"
TABLE="user_records"
ORIGIN="BreachCompilation"
CLICKHOUSE_CLIENT="clickhouse-client --port 19000 --user default --password blackCapybara"


# Function to process a single file
process_file() {
  local file="$1"
  echo "Processing file: $file"

  while IFS=":" read -r username password; do
    # Check if both username and password are not empty
    if [[ -n "$username" && -n "$password" ]]; then
      # Escape single quotes for ClickHouse insertion
      escaped_username=$(printf '%s\n' "$username" | sed "s/'/'\\\\''/g")
      escaped_password=$(printf '%s\n' "$password" | sed "s/'/'\\\\''/g")

      # Construct the ClickHouse insert command
      insert_command="INSERT INTO ${DATABASE}.${TABLE} (username, password, origin) VALUES ('${escaped_username}', '${escaped_password}', '${ORIGIN}')"

      # Execute the insert command using clickhouse-client
      $CLICKHOUSE_CLIENT --query "$insert_command"

      # Check the exit code of clickhouse-client.  Important for error handling
      if [[ $? -ne 0 ]]; then
        echo "Error inserting record from file: $file. Username: '$username', Password: '$password'. Skipping this record." >&2
        continue  # Skip to the next record
      fi
    fi
  done < "$file"
  return 0 # Indicate success
}


# Find all files in the current directory and subdirectories
find . -type f -print0 | while IFS= read -r -d $'\0' file; do
  process_file "$file"
done

echo "Finished processing all files."
