#!/bin/bash

if [ "$1" == "-h" ]
then
    cat <<- EOH
		    Usage: $0 [-p] [folder]
		      -p option prints out unused strings, 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
    if [ -d $app/res ]
    then
        pushd $app > /dev/null
        # Two sed's were needed because the | operator is not supported on the mac
        for i in $(grep -Rs "\(string\|plurals\) name=" res | sed 's/.*string name=\"//' | sed 's/.*plurals name=\"//'|sed 's/".*$//'|sort -u)
        do
            echo $i $(grep -Rws R.plurals.$i\\\|R.string.$i\\\|@string/$i .|wc -l)
        done | grep ' 0$' | {
            if [ "$showall" == "yes" ]
            then
                echo $app
                cat
            else
                count=$(wc -l)
                if [ "$count" != "0" ]
                then
                    echo $app: $count unused strings
                fi
            fi
        }
        popd $app > /dev/null
    fi
done