#!/bin/sh
#    update-copyright    - Simple tool to update the Copyright lines
#                          in all files checked into git
#
#    Copyright (C) 2018- OpenVPN Inc
#    Copyright (C) 2018-2022 David Sommerseth <davids@openvpn.net>
#
#    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception

# Basic shell sanity
set -eu

# Simple argument control
if [ $# -ne 1 ]; then
    echo "Usage: $0 <New Copyright Year>"
    exit 1
fi

# Only update Copyright lines with these owners
# The 'or' operator is GNU sed specific, and must be \|
UPDATE_COPYRIGHT_LINES="OpenVPN Inc\|@openvpn\.net\\|unstable\.cc\|gmail\.com\|@fox-it\.com"
COPY_YEAR="$1"

cd "$(git rev-parse --show-toplevel)"
for file in $(git ls-files | grep -v deps/);
do
    echo -n "Updating $file ..."
    # The first sed operation covers 20xx-20yy copyright lines,
    # The second sed operation changes 20xx -> 20xx-20yy
    sed -e "/$UPDATE_COPYRIGHT_LINES/s/\(Copyright (C) 20..-\)\(20..\)[[:blank:]]\+/\1$COPY_YEAR /" \
        -e "/$UPDATE_COPYRIGHT_LINES/s/\(Copyright (C) \)\(20..\)[[:blank:]]\+/\1\2-$COPY_YEAR /" \
        -e "/$UPDATE_COPYRIGHT_LINES/s/\(Copyright (C) $COPY_YEAR\)\(-$COPY_YEAR\)[[:blank:]]\+/\1      /" \
        -i $file
    echo " Done"
done
echo
echo "** All files updated with $COPY_YEAR as the ending copyright year"
echo
exit 0
