update-copyright-years.sh 990 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. ## run this script onece a year
  3. set -euo pipefail
  4. THIS_YEAR="$(date +'%Y')"
  5. fix_copyright_year() {
  6. local file="$1"
  7. if [[ $file != *.erl ]] && \
  8. [[ $file != *.ex ]] && \
  9. [[ $file != *.hrl ]] && \
  10. [[ $file != *.proto ]]; then
  11. ## Ignore other file
  12. return 0
  13. fi
  14. local copyright_line
  15. copyright_line="$(head -2 "$file" | grep -E "Copyright\s\(c\)\s.+\sEMQ" || true)"
  16. local begin_year
  17. begin_year="$(echo "$copyright_line" | sed -E 's#%% Copyright \(c\) (20..).*#\1#g')"
  18. if [ "$begin_year" = "$THIS_YEAR" ]; then
  19. ## new file added this year
  20. return 0
  21. fi
  22. if [ -z "$copyright_line" ]; then
  23. ## No Copyright info, it is not intended to add one from this script
  24. echo "Ignored $file"
  25. return 0
  26. fi
  27. sed -E "s#(%% Copyright \(c\) 20..)(-20.. | )(.*)#\1-$THIS_YEAR \3#g" -i "$file"
  28. }
  29. while read -r file; do
  30. fix_copyright_year "$file"
  31. done < <(git ls-files)