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

if [ $# -lt 1 ]; then
  echo "Usage: $0 <input_file> [output_file]"
  exit 1
fi

input="$1"
if [ ! -f "$input" ]; then
  echo "Error: File '$input' not found."
  exit 1
fi

if [ -n "${2:-}" ]; then
  output="$2"
elif [[ "$input" == *.* ]]; then
  output="${input%.*}_cleaned.${input##*.}"
else
  output="${input}_cleaned"
fi

tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT

# Remove <u>, <u ...>, and </u> (case-insensitive) without matching tags like <ul>
sed -E 's|</?[uU]([[:space:]][^>]*)?>||g' "$input" > "$tmp"
cat "$tmp" > "$output"

echo "Done! Saved cleaned HTML to $output"
