first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / buildor / buildor_add_sentinel.sh
1 #!/bin/bash
2
3 # this script adds a sentinel to guard c++ implementation files (*.cpp) from
4 # multiple inclusion.  it will only add the guardian markers if the files
5 # don't already have it, as far as the script can tell.
6 #
7 # for a file called blahblah.cpp, the sentinel lines end up looking like:
8 #
9 #     #ifndef BLAHBLAH_IMPLEMENTATION_FILE
10 #     #define BLAHBLAH_IMPLEMENTATION_FILE
11 #     .......rest of file.......
12 #     #endif
13
14 function add_sentinel {
15   implem_file="$1"
16   echo adding sentinels: $filename
17   # get the basename for the file and strip off the extension.
18   base=$(basename $implem_file | sed -e 's/\.[a-zA-Z0-9_][a-zA-Z0-9_]*$//')
19   # pick a temporary location for the new version of the file.
20   temp_out="$(mktemp "$TMP/zz_buildor_sentinel_$base.XXXXXX")"
21   # if the file actually exists, then we bother operating on it.
22   if [ -f "$implem_file" ]; then
23     # check whether the first few lines have our sentinel lines present.
24     already_guarded=$(head -3 "$implem_file" | sed -n -e 's/#ifndef/foundit/p' )
25     if [ -z "$already_guarded" ]; then
26       # the file isn't already protected in those first few lines, so we'll
27       # go ahead and add the guardian markers.
28 #echo "not guarded, will do: $implem_file"
29       base_caps=$(echo $base | tr a-z A-Z)
30       # the lines below are the first part of the guard which use the
31       # capitalized form of the extensionless file name.
32       echo "#ifndef ${base_caps}_IMPLEMENTATION_FILE" >"$temp_out"
33       echo "#define ${base_caps}_IMPLEMENTATION_FILE" >>"$temp_out"
34       echo "" >>"$temp_out"
35       # stuff in the original file now.  don't want to leave that out.
36       cat "$implem_file" >>"$temp_out"
37       # add the ending sentinel with a helpful comment.
38       echo "" >>"$temp_out"
39       echo "#endif //${base_caps}_IMPLEMENTATION_FILE" >>"$temp_out"
40       echo "" >>"$temp_out"
41       # move the temporary file into place as the new version of the cpp file.
42       mv "$temp_out" "$implem_file"
43     fi
44   fi
45 }
46
47 outfile="$(mktemp "$TMP/zz_buildor_sentinel.XXXXXX")"
48
49 for filename in $*; do 
50
51   # find all the files that match that directory (or filename).
52   find "$filename" -iname "*.cpp" >$outfile
53
54   # iterate on the list we found and add sentinels to every file.
55   while true; do
56     read line_found
57 #echo line is $line_found
58     if [ $? != 0 ]; then break; fi
59     add_sentinel "$line_found"
60   done <$outfile
61   
62 done
63
64 rm "$outfile"
65