Add /etc/mail/spamassassin to prune prefixes
[pkgusr] / usr / bin / grep_all_regular_files_for
1 #!/bin/bash
2 ## Originally...
3 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
6
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 #  Many updates/tweaks --SY.
9
10 # The following list should contain the mount points of all
11 # filesystems that are to be scanned as a space-separated list within
12 # parentheses. / will usually be in this list and if you have /usr on
13 # a separate partition, it will also be in this list. Mount points
14 # whose filesystems are special, such as procfs or sysfs must not be
15 # in this list. While a simple find on those special filesystems
16 # should be harmless, operations such as "-exec grep something" are
17 # NOT SAFE and may have HARMFUL SIDE-EFFECTS, especially when
18 # performed as root.
19
20 ## Bastard settings
21 # fs_to_scan=(/ /opt /usr /var)
22
23 fs_to_scan=(/)
24
25 ## NOTE: if any of the directories listed in fs_to_scan contain
26 ## non-UNIX filesystems (MS-DOS, CD-ROM etc) you need to set $NOLEAF
27 ## here to `-noleaf'.  But only do so if you really need to as it
28 ## comes with a significant slow down on the find.
29 # NOLEAF='-noleaf'
30 NOLEAF=
31
32 # Files with a path prefix found in the following list are ignored. As
33 # the main function of this script is to help you find files that
34 # contain hardwired paths to /tools or other unwanted references to
35 # your build system, you will usually prune any directories that don't
36 # contain files of interest, such as /tools (whose files naturally
37 # refer to /tools) and your package users' home directories (which may
38 # also test positive if you have unpacked and configured sources lying
39 # around).
40 #
41 # NOTE: If a directory you want to prune is on a separate filesystem
42 # (separate from those listed in fs_to_scan) you don't need to list it
43 # here because of the -xdev option used in the find command.
44
45 ## Bastard settings
46 # prune_prefixes=(\
47 #       /{,*/{,*/}}lost+found \
48 #       /root \
49 #       /opt/pgsql/data \
50 #       /opt/sql-ledger/{spool,templates,users,css} \
51 #       /etc/apache/ssl.key \
52 #       /etc/audisp/plugins.d \
53 #       /etc/cups/ssl \
54 #       /etc/firewall \
55 #       /etc/mail/spamassassin \
56 #       /etc/polkit-1/rules.d \
57 #       /etc/skel \
58 #       /etc/ssl/private \
59 #       /etc/sudoers.d \
60 #       /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
61 #       /var/log \
62 #       /usr/lib/pkgusr \
63 #       /usr/share/polkit-1/rules.d \
64 #       /var/tmp \
65 #       /var/{cache,chroot,db,run,snmp,spool} \
66 #       /var/lib/{sshd,nfs,spamassassin,pulse,machines}) #NO TRAILING SLASHES!!!!
67
68 prune_prefixes=(\
69         /home \
70         /usr/lib/pkgusr \
71         /usr/src \
72         /dev \
73         /tools) #NO TRAILING SLASHES!!!
74
75 if [ $# -lt 1 -o "$1" = "--help" ]; then
76     echo 1>&2 
77     echo 1>&2 'USAGE: '"${0##*/}"' <grep-commands>'
78     echo 1>&2 
79     echo 1>&2 '  grep -l <grep-commands> -- <file>'
80     echo 1>&2 '  will be executed for each *regular file* <file>'
81     echo 1>&2 '  ATTENTION! If you override the -l switch with a switch that makes grep'
82     echo 1>&2 '  output all individual matches rather than just the matching files,'
83     echo 1>&2 '  then DO NOT redirect output to a file that is in a directory that will be'
84     echo 1>&2 '  scanned, or you risk creating an endless loop that will cause your'
85     echo 1>&2 '  output file to grow till your disk is full.'
86     echo 1>&2 
87     exit 1
88 fi
89
90 # suppress ugly debug output from shell
91 trap ':' SIGPIPE
92
93 # construct find commands that match the prune_prefixes. Each prefix will be
94 # matched as -path <prefix> -or -path <prefix>/*
95 # so that the directory itself and all subdirectories are matched.
96 y=(\( -false)
97 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1)) 
98 do
99     y[${#y[@]}]='-or'
100     y[${#y[@]}]=-path
101     y[${#y[@]}]="${prune_prefixes[$i]}"
102     y[${#y[@]}]='-or'
103     y[${#y[@]}]=-path
104     y[${#y[@]}]="${prune_prefixes[$i]}/*"
105 done
106 y[${#y[@]}]=')'
107
108 cmd_pre=(-type f -exec grep -l)
109 cmd_post=(-- {} \;)
110
111 # In the following find command, the part
112 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
113 # is responsible for preventing the files that match prune_prefixes from
114 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
115 # -prune has no effect and is always false when -depth is used (which someone
116 # might do in the future).
117 # The -true before "$@" ensures that -depth can be passed as 1st parameter
118 # of $cmd_pre (should someone change it in the future).
119 find "${fs_to_scan[@]}" -xdev $NOLEAF \
120     -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
121     -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)