#
# Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code 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
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
 
import os
from datetime import datetime

# example:
# " * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved."
# " * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved."
def process_line(line):
    years = [int(s) for s in line.replace(",", " ").split() if s.isdigit()]
    minYear = years[0]
    maxYear = minYear
    if len(years) > 1:
        maxYear = years[1]
    year = datetime.now().year
    if year == maxYear:
        print("OK")
    else:
        print("change:")
        print(line, end='')
        print("to:")
        print(f" * Copyright (c) {minYear}, {year}, Oracle and/or its affiliates. All rights reserved.")

def process_file(filename):
    print(f"\nchecking {filename} ...")
    file = open(filename, 'r')
    found = False
    for line in file.readlines():
        if "Copyright (c)" in line and "Oracle" in line:
            found = True
            break
    if found:
        process_line(line)
    else:
        print("ERROR: missing \"Copyright (c) Oracle\" header ?!")
      
                
files = os.popen("git diff --name-status master").read()
# example:
# "M       src/hotspot/cpu/aarch64/compressedKlass_aarch64.cpp"
# "M       src/hotspot/os/bsd/gc/z/zPhysicalMemoryBacking_bsd.cpp"
# "M       src/hotspot/os/linux/os_linux.cpp"

for f in files.split("\n"):
    for filename in f.split("\t"):
        if len(filename) > 1:
            process_file(filename)
