#!/bin/sh
for retry in `seq 20`; do
  output="`qhelpgenerator "$@" 2>&1`"
  errors="`echo "$output"|grep "Error in line .*: Opening and ending tag mismatch."`"
  if test -z "$errors"; then
    echo "$output"
    exit
  fi
  if echo "$1"|grep -q '\.qhp'; then
    file="$1"
  elif echo "$2"|grep -q '\.qhp'; then
    file="$2"
  elif echo "$3"|grep -q '\.qhp'; then
    file="$3"
  fi
  for i in `echo "$errors"|tac|sed 's/^.* \([0-9]\+\):.*$/\1/'`; do
    sed -i "${i}d" "$file"
  done
done
qhelpgenerator "$@"
