|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +TOOL=$(basename "$0") |
| 5 | +HELP=$(cat <<HELP |
| 6 | +${TOOL} - Just bumps copyright year all over the sources. |
| 7 | + It ain't much but it's honest work. |
| 8 | +
|
| 9 | + -- |
| 10 | +
|
| 11 | + Soon her eye fell upon a little glass box lying underneath |
| 12 | + the table. She opened it and found in it a very small |
| 13 | + cake, on which the words "EAT ME" were beautifully marked |
| 14 | + in currants. |
| 15 | +
|
| 16 | + - Alice's Adventures in Wonderland. |
| 17 | +HELP |
| 18 | +) |
| 19 | + |
| 20 | +# Parse CLI options. |
| 21 | +OPTIONS=$(getopt -o pqh -n "$TOOL" -- "$@") |
| 22 | +eval set -- "$OPTIONS" |
| 23 | +while true; do |
| 24 | + case "$1" in |
| 25 | + --) shift; break;; |
| 26 | + -p) PRETEND=1; shift;; |
| 27 | + -h) printf "%s\n" "${HELP}"; |
| 28 | + exit 0;; |
| 29 | + esac |
| 30 | +done |
| 31 | + |
| 32 | +PRETEND=${PRETEND:-} |
| 33 | + |
| 34 | +# Do not proceed if there are some CLI arguments left. Everything |
| 35 | +# should be parsed before this line. Refer to the help message in |
| 36 | +# a funny way. |
| 37 | +if [ $# -ne 0 ]; then |
| 38 | + echo "RUN ME" |
| 39 | + exit 1; |
| 40 | +fi |
| 41 | + |
| 42 | +overwrite() { |
| 43 | + # Check if we're running in term. |
| 44 | + if [ -t 1 ]; then |
| 45 | + echo -e "\r\033[1A\033[0K$@" |
| 46 | + else |
| 47 | + echo -e "$@" |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +makereplace() { |
| 52 | + # XXX: This function uses "upvalues" ($CURYEAR and |
| 53 | + # $NEWYEAR) to make the function more convenient in use. |
| 54 | + # Hence, do not call this function prior to these upvalues |
| 55 | + # definition. |
| 56 | + if [ -z $CURYEAR -o -z $NEWYEAR ]; then |
| 57 | + echo "FATAL: Either CURYEAR or NEWYEAR are undefined" && exit 1 |
| 58 | + fi |
| 59 | + REGEXP="$(printf "$1" $CURYEAR)" |
| 60 | + REPLACEMENT="$(printf "$1" $NEWYEAR)" |
| 61 | + echo -E "s/$REGEXP/$REPLACEMENT/" |
| 62 | +} |
| 63 | + |
| 64 | +# 0. Greeting. |
| 65 | +TODAY=$(date --date='today' --iso-8601) |
| 66 | +echo "Hello! Today is $TODAY. Let's do some janitorial maintenance!" |
| 67 | +NEWYEAR=$(date +"%Y") |
| 68 | +CURYEAR=$(($NEWYEAR-1)) |
| 69 | +echo "* Current year is $CURYEAR. Bumping to $NEWYEAR." |
| 70 | +echo "** Starting" |
| 71 | + |
| 72 | +GITROOT=$(git rev-parse --show-toplevel) |
| 73 | + |
| 74 | +overwrite "** Completed: 0%" |
| 75 | + |
| 76 | +# 1.1. Update copyright notice **almost** everywhere... |
| 77 | +PATTERN=$(makereplace 'Copyright (C) 2020-%d LuaVela Authors.') |
| 78 | +find $GITROOT -type f | xargs -I{} sed -i "$PATTERN" {} |
| 79 | + |
| 80 | +overwrite "** Completed: 99%" |
| 81 | + |
| 82 | +# 1.2. ...except docs/conf.py due to another pattern... |
| 83 | +PATTERN=$(makereplace '2020-%d LuaVela Authors,') |
| 84 | +sed -i "$PATTERN" "$GITROOT/docs/conf.py" |
| 85 | + |
| 86 | +overwrite "** Completed: 100%" |
| 87 | + |
| 88 | +# 1.3. ...and COPYRIGHT file for the same reason. |
| 89 | +PATTERN=$(makereplace 'Copyright 2020-%d LuaVela Authors.') |
| 90 | +sed -i "$PATTERN" "$GITROOT/COPYRIGHT" |
| 91 | + |
| 92 | +overwrite "** Completed!" |
| 93 | + |
| 94 | + |
| 95 | +# 2. Commit everything, if not pretend. |
| 96 | +if [ $PRETEND ]; then |
| 97 | + cat <<PRETEND |
| 98 | +You're almost done: changes are applied, but not committed. |
| 99 | +PRETEND |
| 100 | + exit 0 |
| 101 | +fi |
| 102 | + |
| 103 | +echo "* Commit the changes..." |
| 104 | +if ! git branch --show-current | grep 'bump-copyright-year$' >/dev/null ; then |
| 105 | + cat <<WRONGBRANCH |
| 106 | +Oops, looks like you're commiting to the wrong branch. I hope not |
| 107 | +to the master one. It's better to checkout somewhere else like this: |
| 108 | +>>> git checkout -b ${USER}/bump-copyright-year |
| 109 | +WRONGBRANCH |
| 110 | + exit 1 |
| 111 | +fi; |
| 112 | +git commit -a -s -m 'Bump copyright date' |
| 113 | +echo "You're all set, just push it to the server! Happy New Year :)" |
0 commit comments