#!/bin/bash
#
# This script (which relies on ebrowse)
# builds a set of keywords composed of
# your C++ classnames.  These keywords
# will then be highlighted in emacs.
# This only works for class names which
# begin with a capital letter!
# Here is the format for adding c/c++ keywords..
#(font-lock-add-keywords 'c++-mode
# '( ("\\<\\(real\\|foo\\|bar\\)\\>" . font-lock-type-face) ) )


# Set the output filename
elisp_file=keywords.el
compiled_elisp_file=keywords.elc

# Set the location of the files to check
files_location="../../../s3/include/*.h ../../include/*/*.h"

# Write the elisp file header
echo    "(provide 'keywords)" > $elisp_file
echo    "(font-lock-add-keywords 'c++-mode " >> $elisp_file
echo -n " '( (\"\\\\<\\\\(Real\\\\|Complex\\\\|Number\\\\|" >> $elisp_file

# Get the class list and declare an array of class names
class_list=`/usr/bin/ebrowse --very-verbose --output-file=/dev/null $files_location`
counter=0
declare -a class_array

# Get the enum list
enum_list=`grep enum $files_location -h | grep "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]" | grep -v include | grep "{" | cut -d" " -f 4`

# Get the namespace list
namespace_list=`grep namespace $files_location -h | grep "{" | cut -d" " -f 2`
namespace_list=$namespace_list" libMeshPrivateData"

# Throw the classes into the class_array.
for i in $class_list $enum_list $namespace_list;
  do
    name=`echo $i | grep "^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]" | grep -v include`

    if test "x$name" != "x";
      then
	class_array[$counter]=$name
        counter=$[$counter+1]
    fi
  done

sentinel=$[$counter-1]

# Go through the array, put the names into the file
for j in $(seq 0 $sentinel);
  do
    if test $j == $sentinel;
      then
	echo -n "${class_array[$j]}\\\\)\\\\>\" " >> $elisp_file

      else
        echo -n "${class_array[$j]}\\\\|" >> $elisp_file
    fi
  done


# Add the footer
echo " . font-lock-type-face) ) )" >> $elisp_file

# Make the compiled version of this elisp using
# emacs' batch mode:
emacs --batch --no-site-file -f batch-byte-compile $elisp_file

# Move the file we just created to our personal .elisp dir
mv $elisp_file $compiled_elisp_file ~/.elisp/

