core145: found more urlfilter db files to cleanup
[ipfire-2.x.git] / src / stripper
1 #!/usr/bin/env bash
2
3 dirs=""
4 excludes="/dev /proc /sys /run"
5
6 while [ $# -gt 0 ]; do
7         case "${1}" in
8                 --exclude=*)
9                         excludes="${excludes} ${1#*=}"
10                         ;;
11                 *)
12                         dirs="${dirs} ${1}"
13                         ;;
14         esac
15         shift
16 done
17
18 function _strip() {
19         local file=${1}
20         local strip="${STRIP-strip}"
21
22         local exclude l
23         for exclude in ${excludes}; do
24                 l=${#exclude}
25                 if [ "${file:0:${l}}" = "${exclude}" ]; then
26                         return 0
27                 fi
28         done
29
30         local cmd=( "${strip}" )
31
32         case "$(file -bi ${file})" in
33                 application/x-archive*)
34                         cmd+=( "--strip-debug" "--remove-section=.comment" "--remove-section=.note" )
35                         ;;
36                 *)
37                         cmd+=( "--strip-all" )
38                         ;;
39         esac
40
41         echo "Stripping ${file}..."
42         ${cmd[*]} ${file}
43 }
44
45 for dir in ${dirs}; do
46         # Strip shared objects.
47         find ${dir} -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
48                 | file -N -f - | sed -n -e 's/^\(.*\):[   ]*.*ELF.*, not stripped.*/\1/p' |
49                 while read file; do
50                         _strip ${file} || exit $?
51                 done || exit $?
52
53         # Strip static archives.
54         find ${dir} -name \*.a -a -exec file {} \; \
55                 | grep 'current ar archive' | sed -n -e 's/^\(.*\):[    ]*current ar archive/\1/p' |
56                 while read file; do
57                         _strip ${file} || exit $?
58                 done || exit $?
59 done