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

FILE="$1"
NEW_LINE="$2"

if [[ ! -f "$FILE" ]]; then
  echo "❌ File not found: $FILE"
  exit 1
fi

if ! grep -q "^tls-san:" "$FILE"; then
  echo "❌ 'tls-san:' not found in $FILE"
  exit 1
fi

awk -v newline="$NEW_LINE" '
{
  print
  if ($0 ~ /^tls-san:/) {
    print newline
  }
}
' "$FILE" > "${FILE}.tmp" && mv "${FILE}.tmp" "$FILE"

echo "✅ Line added after tls-san:"
echo "   $NEW_LINE"
