1#!/bin/bash
2# Copyright 2015 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17set -e
18
19cd `dirname $0`/../..
20
21function process_dir {
22  base_dir=$1
23  prefix=$2
24  comment_language=$3
25  (
26    cd $base_dir
27    find . -name "*.h" | while read f ; do
28      guard=${prefix}_`echo ${f#*/} | tr '[:lower:]/.-' '[:upper:]___'`
29      if [ "$comment_language" = "c++" ] ; then
30        comment="// $guard"
31      else
32        comment="/* $guard */"
33      fi
34      awk '
35        BEGIN {
36          guard = "'${guard}'";
37          comment_language = "'${comment_language}'";
38        }
39        prev ~ /^#ifndef/ && !got_first_ifndef {
40          got_first_ifndef = 1;
41          prev = "#ifndef " guard;
42        }
43        prev ~ /^#define/ && !got_first_define {
44          got_first_define = 1;
45          prev = "#define " guard;
46        }
47        NR > 1 { print prev; }
48        { prev = $0; }
49        END {
50          if (prev ~ /^#endif/) {
51            if (comment_language ~ /^c$/) {
52              print "#endif  /* " guard " */";
53            } else if (comment_language ~ /^c\+\+$/) {
54              print "#endif  // " guard;
55            } else {
56              print "ERROR: unknown comment language: " comment_language;
57            }
58          } else {
59            print "ERROR: file does not end with #endif";
60          }
61        }
62      ' "${f}" > "${f}.rewritten"
63      mv "${f}.rewritten" "${f}"
64    done
65  )
66}
67
68process_dir include/grpc GRPC c
69process_dir include/grpc++ GRPCXX c++
70process_dir src/core GRPC_INTERNAL_CORE c
71process_dir src/cpp GRPC_INTERNAL_CPP c++
72process_dir src/compiler GRPC_INTERNAL_COMPILER c++
73process_dir test/core GRPC_TEST_CORE c
74process_dir test/cpp GRPC_TEST_CPP c++
75process_dir examples GRPC_EXAMPLES c++
76