#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#***********************************************************************
# This file is part of OpenMolcas.                                     *
#                                                                      *
# OpenMolcas is free software; you can redistribute it and/or modify   *
# it under the terms of the GNU Lesser General Public License, v. 2.1. *
# OpenMolcas is distributed in the hope that it will be useful, but it *
# is provided "as is" and without any express or implied warranties.   *
# For more details see the full text of the license in the file        *
# LICENSE or in <http://www.gnu.org/licenses/>.                        *
#                                                                      *
# Copyright (C) 2017,2020,2024, Ignacio Fdez. Galván                   *
#***********************************************************************

'''
This script checks that the OpenMolcas header exist and that the copyright
has the right format.
'''

import sys
import re
import argparse
from codecs import getwriter
from locale import getpreferredencoding
from io import open
from os.path import isfile, join, relpath, splitext
from os import walk

try:
  from colorama import init, Fore, Back, Style
except ImportError:
  def init(*args, **kwargs):
    pass
  class Dummy(object):
    pass
  Fore = Dummy()
  Fore.RED = ''
  Fore.BLUE = ''
  Back = Dummy()
  Back.RED = ''
  Style = Dummy()
  Style.RESET_ALL = ''

# Define command-line arguments
parser = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog))
parser.add_argument('-p', '--print', help='print copyright information found in files', action='store_true')
parser.add_argument('-r', '--report', help='check and report some formatting problems', action='store_true')
parser.add_argument('-n', '--no-copyright', help='report files with no copyright information', action='store_true')
parser.add_argument('-a', '--authors', help='list all authors with their contributed files', action='store_true')
parser.add_argument('root', help='filename or tree to check')
args = vars(parser.parse_args())
if (not (args['print'] or args['report'] or args['no_copyright'] or args['authors'])):
  parser.error('At least one of the optional arguments is required')

# Use colors in terminal, not if output is redirected to file
if sys.stdout.isatty():
  init()
else:
  init(strip=True)

files = []
if (isfile(args['root'])):
  files.append(args['root'])
else:
  for dname, dirs, fnames in walk(args['root']):
    for fname in fnames:
      files.append(join(dname, fname))

rc = 0

authors = {}

for filename in sorted(files):

  copyright = []
  if (isfile(filename)):
    if (filename == args['root']):
      relfile = filename
    else:
      relfile = relpath(filename, args['root'])
    with open(filename, 'r', encoding='utf-8') as f:
      header = False
      copy = False
      blank = False
      blank_error = False
      try:
        for l in f:
          prev_blank = blank
          blank = (l.find(70*' ') >= 0)
          if (l.find('This file is part of OpenMolcas') > 0):
            header = True
          elif (header and re.search(r'copyright', l, re.IGNORECASE)):
            copy = True
            blank_error = not prev_blank
          elif (header and (l.find(70*'*') >= 0)):
            break
          if (copy):
            match = re.match(r'^.\s*(Copyright \(C\))?\s*(([\d,-]*?),)?\s+(.*?)\s*\*$', l)
            if (match):
              year = match.group(3)
              name = match.group(4)
              if (year is None):
                year = '(?)'
              copyright.append([year, name])
              aname = re.sub(r'\s*\(.*', '', name)
              if (aname in authors):
                authors[aname].append(relfile)
              else:
                authors[aname] = [relfile]
            if (((not match) or blank_error) and args['report']):
              print('{0}*** {1}: Format error ***{2}'.format(Back.RED, relfile, Style.RESET_ALL))
              print(l)
              rc = 1
        if (not header and args['report']):
          if (splitext(filename)[1] in ['.f', '.F', '.f90', '.F90', '.fh', '.c', '.h', '.cmake', '.py', '.ascii', '.tbl', '.vim']):
            print('{0}*** {1}: No header ***{2}'.format(Back.RED, relfile, Style.RESET_ALL))
            rc = 1
      except UnicodeDecodeError:
        if (args['report']):
          if (splitext(filename)[1] not in ['.png', '.jpg']):
            print('{0}*** Problem decoding {1} ***{2}'.format(Back.RED, relfile, Style.RESET_ALL))
            rc = 1

  # Write all copyrights
  if (args['print']):
    fmt = '© {0}{{0}}{1}, {2}{{1}}{1}'.format(Fore.RED, Style.RESET_ALL, Fore.BLUE)
    if (len(copyright) > 0):
      print('··· {0} ···'.format(relfile))
    for item in copyright:
      print(fmt.format(item[0],item[1]))

  # Find issues
  if (args['no_copyright']):
    if (len(copyright) == 0):
      print('{0}*** {1}: No copyright ***{2}'.format(Back.RED, relfile, Style.RESET_ALL))
  if (args['report']):
    names = set([])
    for item in copyright:
      if (re.search('[\d,]', item[1])):
        print('{0}*** {1}: Name error? ***{2}'.format(Back.RED, relfile, Style.RESET_ALL))
        print(item[1])
        rc = 1
      if (item[1] in names):
        print('{0}*** {1}: Duplicate name ***{2}'.format(Back.RED, relfile, Style.RESET_ALL))
        print(item[1])
        rc = 1
      else:
        names.add(item[1])

# Print authors and files
if (args['authors']):
  for k in sorted(authors.keys()):
    print('\n==================')
    print(k)
    print('------------------')
    for f in (authors[k]):
      print(f)

sys.exit(rc)
