1#!/bin/bash
2
3if ! which xmlstarlet > /dev/null
4then
5    echo "You need to have the 'xmlstarlet' command in your path"
6    exit
7fi
8
9apps=$1
10CWD=$(pwd)/
11if [ "$apps" = "" ]
12then
13    echo "Please specify the path to an application, or '--all' to process all applications"
14    exit
15elif [ "$apps" = "--all" ]
16then
17    apps=$ANDROID_BUILD_TOP/packages/apps/*
18fi
19
20BASE=$(pwd)/$(dirname $0)
21
22for app in $apps
23do
24    pushd $app
25    $BASE/findunusedresources -p . | {
26        read LINE NUM
27        while [ "$LINE" != "" ]
28        do
29            if [ "Z$LINE" = "Z-----------------------------------------------------------" ]
30            then
31                # skip
32                true
33            elif [ "$LINE" = "$app" ]
34            then
35                # skip
36                true
37            else
38                # try to find the missing resource
39                find res | grep -w $LINE  | {
40                    read RESLINE
41                    while [ "$RESLINE" != "" ]
42                    do
43                        if [ -f $RESLINE ]
44                        then
45                            echo REMOVING FILE: $RESLINE
46                            git rm $RESLINE > /dev/null
47                        else
48                            echo WARNING unexpected result for $LINE
49                        fi
50                        read RESLINE
51                    done
52                }
53                grep -Rwl $LINE res | {
54                    read RESLINE
55                    while [ "$RESLINE" != "" ]
56                    do
57                        ISSTRING=$(echo "$RESLINE" | grep -w "strings\.xml")
58                        if [ -n "$ISSTRING" ]
59                        then
60                            echo REMOVING STRING $LINE from $RESLINE
61                            xmlstarlet ed -P -S -d "/resources/string[@name='$LINE']" $RESLINE > tf$$
62                            mv tf$$ $RESLINE
63                            git add $RESLINE
64                        else
65                            echo REMOVING $LINE from $RESLINE
66                            xmlstarlet ed -P -S -d "/resources/*[@name='$LINE']" $RESLINE > tf$$
67                            mv tf$$ $RESLINE
68                            git add $RESLINE
69                        fi
70                        read RESLINE
71                    done
72                }
73            fi
74            read LINE NUM
75        done
76    }
77    popd
78done
79echo
80echo "Done."
81echo "Please rebuild the updated applications to make sure that everything still builds."
82echo "After rebuilding, rerun 'findunusedresources' or 'removeunusedresources' to see if any more resources are now unused."
83echo "When you're done, you can 'git commit' the change."
84echo
85