#!/bin/sh

LANG=C

if [ "$1" == "-h" ]
then
    cat <<- EOH
		    Usage: $0 [-p] [folder]
		      -p option prints out unused resources, otherwise a total count is printed
		      folder option causes only that app folder to be scanned, default is to scan all folders onder apps/
		EOH
    exit
fi

showall=no
if [ "$1" == "-p" ]
then
    showall=yes
    shift
fi

apps=$1
if [ "$apps" == "" ]
then
    apps=$ANDROID_BUILD_TOP/packages/apps/*
fi

for app in $apps
do
    echo '-----------------------------------------------------------'
    if [ "$app" == "." ]
    then
        app=$(pwd)
    fi
    if [ -d $app/res ]
    then
        appname=$(basename $app)
        iappname=$(grep LOCAL_PACKAGE_NAME $app/Android.mk | sed 's/.*:= *//')
        resources=
        for res in $(echo $app/res/* $(find $ANDROID_BUILD_TOP/vendor -type d -wholename $ANDROID_BUILD_TOP/vendor/*/$appname/res | grep overlay))
        do
            resources="$resources $(echo $res | grep -v '\-mcc\|[a-z]*-[a-z][a-z]$\|[a-z]*-[a-z][a-z]-.*')"
        done
        sources=$app/src
        if [ -d $app/tests ]
        then
            sources="$sources $app/tests"
        fi
        if [ -d $app/samples ]
        then
            sources="$sources $app/samples"
        fi

        # find the R.java file that contains all the generated resource identifiers
        rDotJava=$(find $ANDROID_BUILD_TOP/out/target/common/obj/APPS/${iappname}_intermediates/ -name R.java)

        # Simplistically process the content of the file to get the names of all the constants,
        # and try to find a reference to each constant.

        # First take all the input files and concatenate them, removing newlines. This allows us to
        # find expressions that are broken up over multiple lines, i.e. R.drawable.\nsomeconstant
        find $resources $sources $app/AndroidManifest.xml -type f -print |xargs cat | tr -d '\n ' > /tmp/everything$$

        # Now look for each of the constants in the contatenated file.
        for i in $(cat $rDotJava | grep "\w*=0x\d*" | sed 's/ *public static final int //' | sed 's/=0x.*//')
        do
            # Since periods in the names get translated to underscores in R.java, and you can actually
            # refer to such constants from java by using an underscore instead of a period, we also
            # replace all underscores with a pattern that will match periods and underscores.
            p=$(echo $i | sed 's/_/[\\._]/g')
            echo $i $(grep -cw R\\..*\\.$i\\\|@style/$p\\\|@drawable/$p\\\|@anim/$p\\\|@color/$p\\\|@xml/$p\\\|@layout/$p\\\|@menu/$p\\\|@+id/$p\\\|@array/$p\\\|@string/$p\\\|@mipmap/$p\\\|@integer/$p\\\|@dimen/$p\\\|\[a-z\]\*:$p\\\|enumname=\"$p\\\|\<item\>$p\< < /tmp/everything$$)
        done | grep " 0$" | {
            # this block gets as its input a list of constants for which no references were found, one per line
            if [ "$showall" == "yes" ]
            then
                echo $app
                cat
            else
                count=$(wc -l)
                if [ "$count" != "0" ]
                then
                    echo $app: $count unused resources
                fi
            fi
        }
        rm /tmp/everything$$
    fi
done