#!/usr/bin/env python
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Add license header to source files.

If the file doesn't have the license header, add it with the appropriate comment
style.
"""

import argparse
import sys


apache2 = """{comment} Copyright 2018 Google LLC
{comment}
{comment} Licensed under the Apache License, Version 2.0 (the "License");
{comment} you may not use this file except in compliance with the License.
{comment} You may obtain a copy of the License at
{comment}
{comment}     https://www.apache.org/licenses/LICENSE-2.0
{comment}
{comment} Unless required by applicable law or agreed to in writing, software
{comment} distributed under the License is distributed on an "AS IS" BASIS,
{comment} WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
{comment} See the License for the specific language governing permissions and
{comment} limitations under the License."""

def Main():
    parser = argparse.ArgumentParser()
    parser.add_argument("file")
    parser.add_argument("--style", choices=["c", "hash"], required=True)
    args = parser.parse_args()
    header = "/*\n" if args.style == "c" else ""
    header += apache2.format(comment=" *" if args.style == "c" else "#")
    header += "\n */" if args.style == "c" else ""
    header += "\n\n"
    with open(args.file, "r") as f:
        contents = f.read()
        if header in contents:
            return
    with open(args.file, "w") as f:
        f.write(header)
        f.write(contents)

if __name__ == "__main__":
    sys.exit(Main())

@@ -742,16 +742,42 @@
 rgparse%0A
+import datetime%0Aimport re%0A
 import s
@@ -806,36 +806,38 @@
 ment%7D Copyright 
-2018
+%7Byear%7D
  Google LLC%0A%7Bcom
@@ -1707,32 +1707,77 @@
  == %22c%22 else %22%22%0A
+    year = str(datetime.datetime.now().year)%0A
     header += ap
@@ -1831,16 +1831,27 @@
 else %22#%22
+, year=year
 )%0A    he
@@ -1916,16 +1916,80 @@
  %22%5Cn%5Cn%22%0A
+    header_regex = re.escape(header).replace(year, r%22%5Cd%5Cd%5Cd%5Cd%22)%0A
     with
@@ -2059,17 +2059,31 @@
  if 
-header in
+re.search(header_regex,
  con
@@ -2087,16 +2087,17 @@
 contents
+)
 :%0A      

Fix license script to allow any year.