#!/bin/bash
#
# Copyright 2005, 2012 Zuza Software Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Finds problems with accesskeys in Mozilla DTD files
#
# Mozilla uses a system of naming such that:
#   something.label
#   something.accesskey
# can create something in the UI with a label and accelerator/mnemonic.
#
# The Translate Toolkit uses this alignment to convert the seperate lable and
# accesskey into one entry with the accelerator preceded by an ampersand.
#
# This tool tries to find problems with this alignment. These could include:
#  * Missing .label entry
#  * Strangely named accesskeys
#

function header()
{
    echo
    echo "### $1 ###"
}

header "Find .acecsskey items and report those which don't have a corresponding .label in DTD"
for accesskeysuffix in accesskey accessKey akey
do
    for d in `find . -name "*.dtd"`
    do
        for accesskeyprefix in `grep "[.]${accesskeysuffix} " $d | sed 's/^.* \([^ ]*\).'"${accesskeysuffix}"'.*$/\1/'`
        do
            otheroptions=`grep "[ ]${accesskeyprefix}[.]" $d | sed 's/^.* '"${accesskeyprefix}"'[.]\([^ ]*\).*$/\1/'`
            echo $d $accesskeyprefix: $otheroptions | egrep -v '\<(label|title)\>'
        done
    done
done

header "Report all non-standard .akey, etc suffixes in DTD"
for accesskeysuffix in akey access
do
    for d in `find . -name "*.dtd"`
    do
        for accesskeyprefix in `grep "[.]${accesskeysuffix} " $d | sed 's/^.* \([^ ]*\).'"${accesskeysuffix}"'.*$/\1/'`
        do
            otheroptions=`grep "[ ]${accesskeyprefix}[.]" $d | sed 's/^.* '"${accesskeyprefix}"'[.]\([^ ]*\).*$/\1/'`
            echo $d $accesskeyprefix: $otheroptions
        done
    done
done

header "Find .accesskey items and report those which don't have a corresponding .label in .properties"
for accesskeysuffix in accesskey accessKey akey
do
    for d in `find . -name "*.properties"`
    do
        for accesskeyprefix in `grep "[._]${accesskeysuffix}.*=" $d | sed 's/^[ ]*\([^ ]*\)[._]'"${accesskeysuffix}"'.*$/\1/'`
        do
            otheroptions=`grep "^[ ]*${accesskeyprefix}[._]" $d | sed 's/^[ ]*'"${accesskeyprefix}"'[._]\([^= ]*\).*$/\1/'`
            echo $d $accesskeyprefix: $otheroptions | egrep -v '\<(label|title)\>'
        done
    done
done

header "Report all non-standard .akey, etc suffixes in .properties"
for accesskeysuffix in akey access
do
    for d in `find . -name "*.properties§"`
    do
        for accesskeyprefix in `grep "[._]${accesskeysuffix}.*=" $d | sed 's/^[ ]*\([^ ]*\)[._]'"${accesskeysuffix}"'.*$/\1/'`
        do
            otheroptions=`grep "^[ ]*${accesskeyprefix}[._]" $d | sed 's/^[ ]*'"${accesskeyprefix}"'[._]\([^= ]*\).*$/\1/'`
            echo $d $accesskeyprefix: $otheroptions
        done
    done
done
