#!/bin/bash
set -e -o pipefail
: ${TERM:="xterm"}
HEADER[1]="^\/{71}$"
HEADER[2]="^\/\/ Copyright \(c\) [0-9]{4} VMware, Inc\. All Rights Reserved\.$"
HEADER[3]="^\/\/ SPDX\-License\-Identifier\: Apache\-2\.0$"
HEADER[4]="^\/{71}$"
ERR=false
FAIL=false
for file in $(git ls-files | grep "\.go$" | grep -v vendor/ | grep -v mocks/); do
echo -n "Header check: $file... "
for count in $(seq 1 ${#HEADER[@]}); do
if [[ ! $(sed ${count}q\;d ${file}) =~ ${HEADER[$count]} ]]; then
ERR=true
fi
done
if [ $ERR == true ]; then
if [[ $# -gt 0 && $1 =~ [[:upper:]fix] ]]; then
cat ./scripts/copyright-header.txt ${file} > ${file}.new
mv ${file}.new ${file}
echo "$(tput setaf 3)FIXING$(tput sgr0)"
git add ${file}
ERR=false
else
echo "$(tput setaf 1)FAIL$(tput sgr0)"
>&2 echo "File ${file} is missing the copyright header"
ERR=false
FAIL=true
fi
else
echo "$(tput setaf 2)OK$(tput sgr0)"
fi
done
[ $FAIL == true ] && exit 1 || exit 0