#!/usr/bin/env bash
#
# find_user.sh
#
# Purpose:
#   Locate references to a user (by email) within Terraform configuration files
#   (*.tf*, recursively) so you can offboard and revoke access cleanly.
#
# Usage:
#   ./find_user.sh firstName.lastName@domain.tld
#
# What the script does:
#   1) Searches for the exact full email string
#   2) Searches for the local-part pattern firstname.lastname
#   3) Searches for initials+lastname pattern (first letter of first name + last name)
#   4) Searches for lastname only (if applicable)
#
# Notes:
#   - The searches are performed with egrep -i -r --include='*.tf*' to cover Terraform files (*.tf, *.tfvars, etc.).
#   - The script prints results for each search. If no matches are found for a search, the script continues with the next searches.
#   - Run from the repository root (or adjust the search path as needed).
#
# Example:
#   chmod +x find_user_by_email.sh
#   ./find_user_by_email.sh jane.doe@acme.org
#
# This script is intended for offboarding workflows to quickly identify all places a user might be referenced
# in Terraform configurations so access can be revoked comprehensively.

# Basic input validation and usage
if [[ "$#" -ne 1 ]]; then
  echo "Usage: $0 firstname.lastname@domain.tld" >&2
  echo "Example: $0 jane.doe@acme.org" >&2
  exit 1
fi

EMAIL="$1"

# Quick sanity check: should look like an email
if ! [[ "$EMAIL" == *@* ]]; then
  echo "Error: Provided value does not look like an email (missing @)." >&2
  echo "Usage: $0 firstname.lastname@domain.tld" >&2
  exit 2
fi

# Extract local part and domain
LOCALPART="${EMAIL%@*}"
DOMAINPART="${EMAIL#*@}"

# Derive firstName and lastName from localpart
# If there is no dot, treat the entire localpart as firstName (no lastName)
if [[ "$LOCALPART" == *.* ]]; then
  FIRST_NAME="${LOCALPART%%.*}"
  TEMP_LAST="${LOCALPART#*.}"
  LAST_NAME="${TEMP_LAST%%.*}"
else
  FIRST_NAME="$LOCALPART"
  LAST_NAME=""
fi

# Prepare search patterns
EMAIL_PATTERN="$EMAIL"

# Pattern for firstname.lastname (local-part)
if [[ -n "$FIRST_NAME" && -n "$LAST_NAME" ]]; then
  NAME_PATTERN="$FIRST_NAME.$LAST_NAME"
else
  # Fallback to the email if we can't derive firstname.lastname
  NAME_PATTERN="$EMAIL"
fi

# Pattern for first letter of firstName + lastName
if [[ -n "$FIRST_NAME" && -n "$LAST_NAME" ]]; then
  INITIAL_LAST_PATTERN="${FIRST_NAME:0:1}${LAST_NAME}"
else
  INITIAL_LAST_PATTERN=""
fi

# Informational header
echo "Searching Terraform configuration for references to user: $EMAIL"
echo "Patterns will be searched with case-insensitive matching (-i)."
echo "Search scope: all *.tf* files recursively from $(pwd)"
echo
echo "Pattern 1 (full email): $EMAIL_PATTERN"
echo "Pattern 2 (firstname.lastname): $NAME_PATTERN"
if [[ -n "$INITIAL_LAST_PATTERN" ]]; then
  echo "Pattern 3 (initial + lastname): $INITIAL_LAST_PATTERN"
  echo "Pattern 4 (lastname only): $LAST_NAME"
fi
echo

SEARCH_ROOT="."

# 1) Full email address
echo "== 1) Full email address reference =="
egrep --color=auto -i -r -w -n --include='*.tf*' "$EMAIL_PATTERN" "$SEARCH_ROOT" || true
echo

# 2) Local-part reference (firstname.lastname)
echo "== 2) Local-part reference (firstname.lastname) =="
egrep --color=auto -i -r -w -n --include='*.tf*' "$NAME_PATTERN" "$SEARCH_ROOT" || true
echo

# 3) Initial + Last name reference
if [[ -n "$INITIAL_LAST_PATTERN" ]]; then
  echo "== 3) Initial + Last name reference (first letter of first name + lastname) =="
  egrep --color=auto -i -r -w -n --include='*.tf*' "$INITIAL_LAST_PATTERN" "$SEARCH_ROOT" || true
  echo
else
  echo "== 3) Initial + Last name reference =="
  echo "Skipped (could not derive last name from input)."
  echo
fi

# 4) Last name reference (if applicable)
if [[ -n "$LAST_NAME" ]]; then
  echo "== 4) Last name reference =="
  egrep --color=auto -i -r -w -n --include='*.tf*' "$LAST_NAME" "$SEARCH_ROOT" || true
else
  echo "Skipped (no last name derived from input)."
fi