chiark / gitweb /
Rename pwx to pwx_local as a backup.
[elogind.git] / pwx_local / cleanup_headers.sh
1 #!/bin/bash
2
3 src_files=( $(find -type f -name '*.c') )
4 hdr_files=( $(find -type f -name '*.h') )
5
6 # Is this a meson build?
7 isMeson=0
8 if [[ -f meson.build ]]; then
9         isMeson=1
10 fi
11
12 for hdr in $(find -type f -name '*.h') ; do
13         h_dir="$(basename $(dirname $hdr))"
14         h_file="$(basename $hdr .h)"
15
16         # Is it listed in the Makefile.am or meson.build?
17         if [[ 1 -eq $isMeson ]]; then
18                 if [[ 0 -lt $(grep -c "$h_dir/${h_file}.c" meson.build) ]] || \
19                    [[ 0 -lt $(grep -c "$h_dir/${h_file}.h" meson.build) ]]; then
20                         # It is needed.
21                         continue 1
22                 fi
23         else
24                 if [[ 0 -lt $(grep -c "$h_dir/${h_file}.c" Makefile.am) ]] || \
25                    [[ 0 -lt $(grep -c "$h_dir/${h_file}.h" Makefile.am) ]]; then
26                         # It is needed.
27                         continue 1
28                 fi
29         fi
30
31         # Is it included in any source files?
32         for src in "${src_files[@]}" ; do
33                 is_inc=$(grep -P '^#include' $src | grep -c "${h_file}.h")
34                 if [[ 0 -lt $is_inc ]]; then
35                         # it is indeed
36                         continue 2
37                 fi
38         done
39
40         # Is it included in any header files?
41         for src in "${hdr_files[@]}" ; do
42
43                 # If we already removed $src, skip it
44                 [ -f "$src" ] || continue 1
45
46                 is_inc=$(grep '#include' $src | grep -c "${h_file}.h")
47                 if [[ 0 -lt $is_inc ]]; then
48                         # it is indeed
49                         continue 2
50                 fi
51         done
52
53         # As it was not included, remove it.
54         git rm $hdr
55 done