#!/bin/bash

# Check translations for bad strings (MessageFormat rules).
# Currently checks for single apostrophes in MessageFormat translations.
# Also warns about unused translations.

WARNING_MESSAGE="#If you want to use single quotes"

# go to the program directory
cd "`dirname "$0"`"

echo "Checking for bad translations..."

cd ../src/esmska/resources
if ls l10n*.properties >/dev/null; then
    for PROP in l10n*.properties; do
	    BAD=`grep -A1 "${WARNING_MESSAGE}" "$PROP" | #the translation is the next line after warning
	    grep -v "^--$" | #strip grep separator
    	grep -v "^#" | #strip warning messages
    	sed "s/''//g" | #replace correct apostrophes (doubled)
    	grep "'" #find bad apostrophes (single)`
    	if [ -n "$BAD" ]; then #some bad translations found
    	    echo "Bad translations found in ${PROP}:"
    	    echo "$BAD"
    	fi
    done
fi

echo "Checking for unused translations..."

cat l10n.properties | grep -v '^#' | sed -n -r 's/^([^=]+)=.*$/\1/p' | while read KEY; do
    # exceptions
    SKIP=''
    for EXC in 'Tip.' 'DesktopFile.'; do
        if [[ $KEY = $EXC* ]]; then
            SKIP=1
        fi
    done
    if [ -n "$SKIP" ]; then
        continue
    fi

    grep -rq "\"$KEY\"" ../ || echo "WARNING: Unused translation: $KEY"
done

