1# Copyright 2023 Google Inc. All rights reserved.
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6
7#     http://www.apache.org/licenses/LICENSE-2.0
8
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# This script loops over all dot file in parent_dir folder and all subfolders ,
16# then it fixes the copyright headers based on provided file_extension and copyright_template_file
17parent_dir=$1
18file_extension=$2
19copyright_template_file=$3
20
21default_year_pattern="YYYY"
22
23#find all files that doesn't contain copyright word with specific  extension
24all_file_names=`grep -riL "copyright" $parent_dir --include \*.$file_extension `
25
26#loop over list of file names
27for file_name in $all_file_names
28do
29   #extract file creation date
30   creation_date=`git log --follow --format=%as --date default $file_name | tail -1`
31   # extract file creation year from the date
32   year=`echo $creation_date | awk -F\- '{print $1}'`
33   echo $file_name   $year
34
35   #read input template file
36   cat $copyright_template_file >> copyright_temp_file;
37   #replace the "YYYY" from template with proper extracted year
38   sed -i -e "s/$default_year_pattern/$year/g" copyright_temp_file
39
40
41   #echo $copyright_temp_file
42   #append modified copyright header to file with no copyright
43   cat $file_name >> copyright_temp_file;
44   cp copyright_temp_file $file_name;
45   rm copyright_temp_file;
46done