#!/usr/bin/env bash

# Based on /windows/src/test/i18n-check-unused-strings
#
# This script finds all references to strings in strings.xml in order to make sure that old
# strings do not proliferate over time. The script generates the following files:
#
# * strings.txt: A plain-text list of all string identifiers found in strings.xml.
# * summary.md: A summary of number of matches for each string found.
# * details.md: List of every reference to each string found
#
# Note that you should review the results with care; some strings may not ever be found,
# because they may be metadata such as "author of translation", or they may have been
# added as part of upcoming functionality; they may also be found in temporary files and
# build artifacts. Because of this, and because the script takes a little while to run,
# I'm not adding the script to the standard build at this time.
#
# Note: strings that aren't expected to be found generally should have `translateable="false"`` so they don't get
# translated in crowdin.
#

set -e # die on non-zero exit code
set -u # die on undefined variables

## START STANDARD BUILD SCRIPT INCLUDE
# adjust relative paths as necessary
THIS_SCRIPT="$(greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null || readlink -f "${BASH_SOURCE[0]}")"
. "$(dirname "$THIS_SCRIPT")/../../resources/build/build-utils.sh"
## END STANDARD BUILD SCRIPT INCLUDE

# Preparation

echo "# List of localized string usage" > detail.md
echo "Collected at: $(date)" >> detail.md
echo >> detail.md

echo "# Summary of localized string usage" | tee summary.md
echo "Collected at: $(date)" | tee -a summary.md
echo | tee -a summary.md
echo " Count | String | Flag" | tee -a summary.md
echo "-------|--------|------" | tee -a summary.md

# Lazy extract all identifiers from strings.xml into local strings.txt
# Note that this relies on the XML following a single line per string pattern
# (up to the name attribute, anyway) but this is good enough for now.

grep -oP '<string.+name="(.+?)"' \
  "$KEYMAN_ROOT/android/KMEA/app/src/main/res/values/strings.xml" "$KEYMAN_ROOT/android/KMAPro/kMAPro/src/main/res/values/strings.xml" | grep -oP 'name="(.+?)"' | awk -F[\"] '{print $2}' > strings.txt

while IFS= read -r string; do
  echo "## $string" >> detail.md
  count=$(grep -Inr \
    --exclude=strings.xml \
    --exclude-dir=utility \
    "$string" \
    "$KEYMAN_ROOT/android/KMAPro/kMAPro/src/main/res/layout" "$KEYMAN_ROOT/android/KMAPro/kMAPro/src/main/res/menu" "$KEYMAN_ROOT/android/KMAPro/kMAPro/src/main/java" \
    "$KEYMAN_ROOT/android/KMEA/app/src/main/res/layout" "$KEYMAN_ROOT/android/KMEA/app/src/main/java" | tee -a detail.md | wc -l)
  echo >> detail.md

  # Write summary
  if [ $count -eq 0 ]; then
    echo "**0** | **$string** | **Not found**" | tee -a summary.md
  else
    echo "$count | $string" | tee -a summary.md
  fi

done < strings.txt
