#!/usr/bin/env bash

set -e

# Arrays to hold targets and ignored names
targets=()
ignore=()

# Parse arguments
while [[ $# -gt 0 ]]; do
	case "$1" in
	-i | --ignore)
		shift
		while [[ $# -gt 0 && ! "$1" =~ ^- ]]; do
			ignore+=("$1")
			shift
		done
		;;
	*)
		targets+=("$1")
		shift
		;;
	esac
done

# If no targets provided, prompt interactively
if [[ ${#targets[@]} -eq 0 ]]; then
	read -rp "Files to search for (space-separated): " -a targets
	read -rp "Directories to ignore (space-separated, optional): " -a ignore
fi

# Build find command
cmd=(find .)

# Add name patterns for targets
if [[ ${#targets[@]} -gt 0 ]]; then
	target_expr=()
	for t in "${targets[@]}"; do
		target_expr+=(-name "$t" -o)
	done
	# Remove last -o
	unset 'target_expr[${#target_expr[@]}-1]'
	cmd+=('(' "${target_expr[@]}" ')')
fi

# Run find and filter ignored names with grep if provided
if [[ ${#ignore[@]} -gt 0 ]]; then
	ignore_pattern=$(printf "|%s" "${ignore[@]}")
	ignore_pattern=${ignore_pattern:1} # remove leading |
	"${cmd[@]}" 2>/dev/null | grep -Ev "$ignore_pattern"
else
	"${cmd[@]}" 2>/dev/null
fi
