#!/usr/bin/env python

# This script checks and can optionally update MOOSE source files.
# You should always run this script without the "-u" option
# first to make sure there is a clean dry run of the files that should
# be updated

import os, string, re
from optparse import OptionParser

global_ignores = ['contrib', '.svn', '.git', 'libmesh']
moose_paths = ['framework', 'unit', 'examples', 'test', 'tutorials']

copyright_header = \
"""/****************************************************************/
/*               DO NOT MODIFY THIS HEADER                      */
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*           (c) 2010 Battelle Energy Alliance, LLC             */
/*                   ALL RIGHTS RESERVED                        */
/*                                                              */
/*          Prepared by Battelle Energy Alliance, LLC           */
/*            Under Contract No. DE-AC07-05ID14517              */
/*            With the U. S. Department of Energy               */
/*                                                              */
/*            See COPYRIGHT for full restrictions               */
/****************************************************************/
"""

lgpl_header = \
"""/****************************************************************/
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*          All contents are licensed under LGPL V2.1           */
/*             See LICENSE for full restrictions                */
/****************************************************************/
"""

global_options = {}

def fixupHeader():
    for dirpath, dirnames, filenames in os.walk(os.getcwd() + "/../../"):

        # Don't traverse into ignored directories
        for ignore in global_ignores:
            if ignore in dirnames:
                dirnames.remove(ignore)

        #print dirpath
        #print dirnames
        for file in filenames:
            suffix = os.path.splitext(file)
            if suffix[-1] == '.C' or suffix[-1] == '.h':
                checkAndUpdate(os.path.abspath(dirpath + '/' + file))


def checkAndUpdate(filename):
    f = open(filename)
    text = f.read()
    f.close()

    # Use the copyright header for framework files, use the lgpl header
    # for everything else
    header = lgpl_header
    for dirname in moose_paths:
        if (string.find(filename, dirname) != -1):
            header = copyright_header
            break

    # Check (exact match only)
    if (string.find(text, header) == -1):
        # print the first 10 lines or so of the file
        if global_options.update == False: # Report only
            print filename + ' does not contain an up to date header'
            if global_options.verbose == True:
                print '>'*40, '\n', '\n'.join((text.split('\n', 10))[:10]), '\n'*5
        else:
            # Update
            f = open(filename + '~tmp', 'w')
            f.write(header)

            # Make sure any previous header version is removed
            text = re.sub(r'^/\*+/$.*^/\*+/$', '', text, flags=re.S | re.M)

            f.write(text)
            f.close()
            os.rename(filename + '~tmp', filename)

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option("-u", "--update", action="store_true", dest="update", default=False)
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False)
    (global_options, args) = parser.parse_args()
    fixupHeader()

@@ -345,1228 +345,245 @@
 h'%5D%0A
-moose_paths = %5B'framework', 'unit', 'examples', 'test', 'tutorials'%5D%0A%0Acopyright_header = %5C%0A%22%22%22/****************************************************************/%0A/*               DO NOT MODIFY THIS HEADER                      */%0A/* MOOSE - Multiphysics Object Oriented Simulation Environment  */%0A/*                                                              */%0A/*           (c) 2010 Battelle Energy Alliance, LLC             */%0A/*                   ALL RIGHTS RESERVED                        */%0A/*                                                              */%0A/*          Prepared by Battelle Energy Alliance, LLC           */%0A/*            Under Contract No. DE-AC07-05ID14517              */%0A/*            With the U. S. Department of Energy               */%0A/*                                                              */%0A/*            See COPYRIGHT for full restrictions               */%0A/****************************************************************/%0A%22%22%22%0A%0Algpl_header = %5C%0A%22%22%22/****************************************************************/%0A/* MOOSE - Multiphysics Object Oriented Simulation Environment  */%0A/*                                                              */%0A/*          All contents are l
+%0Aunified_header = %5C%0A%22%22%22//* This file is part of the MOOSE framework%0A//* https://www.mooseframework.org%0A//*%0A//* All rights reserved, see COPYRIGHT for full restrictions%0A//* https://github.com/idaholab/moose/blob/master/COPYRIGHT%0A//*%0A//* L
 icen
@@ -601,160 +601,90 @@
 GPL 
-V
 2.1
-           */%0A/*             See LICENSE for full restrictions                */%0A/****************************************************************/%0A
+, please see LICENSE for details%0A//* https://www.gnu.org/licenses/lgpl-2.1.html
 %22%22%22%0A
@@ -1417,158 +1417,22 @@
 r = 
-lgpl_header%0A    for dirname in moose_paths:%0A        if (string.find(filename, dirname) != -1):%0A            header = copyright_header%0A            break
+unified_header
 %0A%0A  
@@ -1844,86 +1844,139 @@
   # 
-Update%0A            f = open(filename + '~tmp', 'w')%0A            f.write(header
+Make sure any previous C-style header version is removed%0A            text = re.sub(r'%5E/%5C*+/$.*%5E/%5C*+/$', '', text, flags=re.S %7C re.M
 )%0A%0A 
@@ -1990,32 +1990,37 @@
     # Make sure 
+that 
 any previous hea
@@ -2020,33 +2020,77 @@
 ous 
-header version is
+C++-style header (with extra character)%0A            # is also
  removed
 %0A   
@@ -2077,32 +2077,33 @@
  is also removed
+.
 %0A            tex
@@ -2119,54 +2119,252 @@
 b(r'
+(?:
 %5E/
+/
 %5C*
-+/$.*%5E/%5C*+/$', '', text, flags=re.S %7C re.M
+.*%5Cn)*', '', text, flags=re.M)%0A%0A            # Now cleanup extra blank lines%0A            text = re.sub(r'%5E%5Cs*%5Cn$', '', text, flags=re.M)%0A%0A            # Update%0A            f = open(filename + '~tmp', 'w')%0A            f.write(header + '%5Cn'
 )%0A%0A 

Change fixup_headers script