#!/bin/sh



find $* -name "*.H" -type f | \
while read line
do

documentedNamespaceNClass=$(grep -A 5 -e "^Class" $line | sed -e "/^Class *$/{
N
:loop
/.*:: *$/{
N
s/^ *\(.*\) *\n *\(.*\) */\1\2/
}
t loop
s/Class *\n *\(.*\) */\1/
}" | head -n 1)

  # Skip if empty
  [ -z "$documentedNamespaceNClass" ] && continue

  #echo $documentedNamespaceNClass

  namespacePath=$(grep -e "^namespace *\w*$" $line | sed -e 's=namespace *=::=g')
  namespacePath=$(echo $namespacePath | sed -e 's=^::==' -e 's= ==g')
  #echo $namespacePath

  className=$(grep -e "^class *\w*$"  $line | tail -1 | sed -e 's=class *==g')
  #echo $className

  [ -z "$className" ] && continue

  correctNamespaceNClass=${namespacePath}::${className}
  correctMangledName=$(echo $correctNamespaceNClass | sed -e 's=Foam::==g' -e 's=::=_=g' -e 's=$=_H=')
  #echo $correctMangledName


  if [ "$documentedNamespaceNClass" != "$correctNamespaceNClass" ]
  then

    echo "Class description change in '$line': $documentedNamespaceNClass --> $correctNamespaceNClass"
    sed -i -e \
              "/^Class/,/^$/{ /^Class/{p;a\
              \    ${correctNamespaceNClass}
              }; /^$/p; d }" $line

  fi

done
