#!/usr/bin/env python2.7
################################################################################
##
## Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
## http://www.opensource.org/licenses/mit-license.php
##
################################################################################

from __future__ import print_function
from __future__ import unicode_literals

import os
import sys
import argparse
import re
import fnmatch

REFERENCE = 'eng-USA.h'

def find_localizations(source, single=''):
  '''Finds all [a-z][a-z]-[A-Z][A-Z].h files in the source tree.'''
  found = []
  for path, dirs, files in os.walk(source, topdown=True, onerror=None, followlinks=False):
    matches = map(lambda x: os.path.join(path, x),
                  fnmatch.filter(files, '[a-z][a-z][a-z]-[A-Z][A-Z][A-Z].h'))

    if single:
      # Accept both cases - if user specified es-ES.h or only es-ES
      if not single.endswith('.h'):
        single = '%s.h' % single

      for match in matches:
        if match.endswith(single) or match.endswith(REFERENCE):
          found.append(match)
    else:
      found.extend(matches)

  # Make sure REFERENCE is the first column.
  # NOTE Empty string is always sorted first than any string
  found.sort(key=lambda x: "" if x.endswith(REFERENCE) else x)

  return found

def read_file(translations, file):
  '''Reads all the localized strings from a file.'''
  translations[file] = {}
  with open(file, 'r') as fh:
    for match in re.findall(r'^\s*#define\s+(STRING_[^\s]+)(\s|\\)+"([^"]*)"', fh.read(), re.MULTILINE):
      translations[file][match[0]] = match[2]

def is_present(translations, file, string):
  '''Determines if the string is defined in a translation.'''
  return string in translations[file]

def used_in_source(source, string):
  '''Determines if the string is used in the source.'''
  command = "git grep %s %s | grep -v [a-z][a-z][a-z]-[A-Z][A-Z][A-Z].h >/dev/null 2>&1" % (string, source)
  return True if os.system(command) == 0 else False

def is_translated(translations, file, string):
  '''Determines whether the string is the same in the base version as in the
     translation, indicating work needed.'''
  if file == base:
    return True
  elif string not in translations[base]:
    return True
  elif string not in translations[file]:
    return False
  else:
    return bool(translations[file][string] != translations[base][string])

def main(args):
  '''Processes all the localized files.'''
  errors = 0
  translations = {}

  for file in args.files:
    # Verify all files exist.
    if not os.path.exists(file):
      raise Exception("Localized file '%s' not readable." % file)
    read_file(translations, file)

  strings = set()
  for file in translations:
    for string in translations[file]:
      strings.add(string)

  if len(strings) == 0:
    if not args.quiet:
      print("There are no localized strings found.")
    errors = 1

  # Get length of longest string ID.
  longest_string = len(max(strings, key=len))

  # Display info.
  if not args.quiet:
    print('Scanning in', args.source)
    print()

  # Print header line.
  files = map(lambda x: os.path.basename(x), args.files)

  if not args.quiet:
    print('%-*s' % (longest_string, 'String ID'), end='')
    print(*map(lambda x: '%10s' % x, files), sep='')
    print('-' * longest_string, ' '.join(['---------'] * len(files)))

  for string in sorted(strings):
    # assess status of 'string':
    # - clean

    line = ''
    line_errors = 0
    for file in args.files:
      message = '        '
      if is_present(translations, file, string):
        if is_translated(translations, file, string):
          message = ' [30;42mOk       [0m'
        else:
          message = ' [30;43mTODO     [0m'
      else:
        message = ' [37;41mMissing  [0m'
        line_errors = 1

      line += message

    if args.all or line_errors != 0:
      if args.search:
        if used_in_source(args.source, string):
          if not args.quiet:
            print('%-*s' % (longest_string, string), line, sep='')
        else:
          if not args.quiet:
            print('[37;41m%-*s[0m' % (longest_string, string), line, sep='')
          line_errors = 1
      else:
        if not args.quiet:
          print('%-*s' % (longest_string, string), line, sep='')

    if line_errors:
      errors = 1

  if not args.quiet:
    print('-' * longest_string, ' '.join(['---------'] * len(files)))
    print('%-*s' % (longest_string, 'Total'), end='')
    for file in args.files:
      print('%10d' % len(translations[file]), end='')
    print()

  sys.exit(errors)

if __name__ == "__main__":
  usage="""Utility for checking localized string status across translations."""

  parser = argparse.ArgumentParser(description=usage)
  parser.add_argument('--source', action='store', required=True, help='The source code tree.')
  parser.add_argument('--single', action='store', help='Show only given localization next to reference.')
  parser.add_argument('--all', action='store_true', help='Show all string IDs.')
  parser.add_argument('--search', action='store_true', help='Search source for use.')
  parser.add_argument('--quiet', action='store_true', help='Produces no output.')
  args = parser.parse_args()

  if args.source:
    args.files = find_localizations(args.source)

    if args.single:
      args.files = find_localizations(args.source, args.single)

  base = filter(lambda x: x.endswith(REFERENCE), args.files)[0]

  try:
    main(args)
  except Exception as msg:
    print('Error:', msg)

