Home | History | Annotate | Line # | Download | only in util
      1  1.1  christos #!/usr/bin/env bash
      2  1.1  christos #
      3  1.1  christos # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
      4  1.1  christos #
      5  1.1  christos # Licensed under the OpenSSL license (the "License").  You may not use
      6  1.1  christos # this file except in compliance with the License.  You can obtain a copy
      7  1.1  christos # in the file LICENSE in the source distribution or at
      8  1.1  christos # https://www.openssl.org/source/license.html
      9  1.1  christos 
     10  1.1  christos 
     11  1.1  christos myname="$(basename $0)"
     12  1.1  christos 
     13  1.1  christos this_year="$(date '+%Y')"
     14  1.1  christos some_year="[12][0-9][0-9][0-9]"
     15  1.1  christos year_range="(${some_year})(-${some_year})?"
     16  1.1  christos 
     17  1.1  christos copyright_owner="The OpenSSL Project"
     18  1.1  christos copyright="Copyright .*${year_range} .*${copyright_owner}"
     19  1.1  christos 
     20  1.1  christos # sed_script:
     21  1.1  christos #   for all lines that contain ${copyright} : {
     22  1.1  christos #     replace years yyyy-zzzz (or year yyyy) by yyyy-${this_year}
     23  1.1  christos #     replace repeated years yyyy-yyyy by yyyy
     24  1.1  christos #   }
     25  1.1  christos sed_script="/${copyright}/{ s/${year_range}/\1-${this_year}/ ; s/(${some_year})-\1/\1/ }"
     26  1.1  christos 
     27  1.1  christos function usage() {
     28  1.1  christos 	cat >&2 <<EOF
     29  1.1  christos usage: $myname [-h|--help] [file|directory] ...
     30  1.1  christos 
     31  1.1  christos Updates the year ranges of all OpenSSL copyright statements in the given
     32  1.1  christos files or directories. (Directories are traversed recursively.)
     33  1.1  christos EOF
     34  1.1  christos }
     35  1.1  christos 
     36  1.1  christos if [ $# -eq 0 ]; then
     37  1.1  christos 	usage
     38  1.1  christos 	exit 0
     39  1.1  christos fi
     40  1.1  christos 
     41  1.1  christos 
     42  1.1  christos for arg in "$@"; do
     43  1.1  christos 	case $arg in
     44  1.1  christos 		-h|--help)
     45  1.1  christos 			usage
     46  1.1  christos 			exit 0
     47  1.1  christos 			;;
     48  1.1  christos 		-*)
     49  1.1  christos 			echo -e "illegal option: $arg\n" >& 2
     50  1.1  christos 			usage
     51  1.1  christos 			exit 1
     52  1.1  christos 			;;
     53  1.1  christos 		*)
     54  1.1  christos 			if [ -f "$arg" ]; then
     55  1.1  christos 				sed -E -i "${sed_script}" "$arg"
     56  1.1  christos 			elif [ -d "$arg" ]; then
     57  1.1  christos 				find "$arg" -name '.[a-z]*' -prune -o -type f -exec sed -E -i "${sed_script}" {} +
     58  1.1  christos 			else
     59  1.1  christos 				echo "$arg: no such file or directory" >&2
     60  1.1  christos 			fi
     61  1.1  christos 			;;
     62  1.1  christos 	esac
     63  1.1  christos done
     64