#!/usr/bin/env bash

# Arrays for directories and files to ignore
dirs=()
ignore=()

# Parse arguments
while [ "$#" -gt 0 ]; do
	case "$1" in
	-i | --ignore)
		shift

		# Collect filenames until the next option
		while [ "$#" -gt 0 ] && [[ "$1" != -* ]]; do
			ignore+=("$1")
			shift
		done
		;;
	-*)
		echo "Unknown option: $1"
		exit 1
		;;
	*)
		dirs+=("$1")
		shift
		;;
	esac
done

# Check if at least one directory is provided
if [ "${#dirs[@]}" -lt 1 ]; then
	echo
	echo "Usage:"
	echo "  $0 <dir1> [dir2 ...] [-i|--ignore <file1> [file2 ...]]"
	echo
	echo "Tip:"
	echo "  \".\" and \"..\" work as current and parent dir, respectively."
	echo
	exit 1
fi

# Loop over each directory argument
for dir in "${dirs[@]}"; do
	# Remove trailing slash if any
	dir="${dir%/}"

	# Check if the directory exists
	if [ ! -d "$dir" ]; then
		echo "Directory '$dir' does not exist. Skipping."
		continue
	fi

	echo "==== Processing directory: $dir ===="

	# Loop over all files in the directory
	for f in "$dir"/*; do
		# Check if it's a file (not a subdirectory)
		if [ -f "$f" ]; then
			filename="${f##*/}"

			# Skip ignored files
			if [[ " ${ignore[*]} " == *" $filename "* ]]; then
				continue
			fi

			echo
			echo -e "\033[32m================ Start file '$f' ================\033[0m"
			echo
			cat "$f"
			echo
			echo -e "\033[31m================ End file '$f' ================\033[0m"
			echo
		fi
	done
done
