407014f3fa4c7525049751c4d5af329c7c3050f5
[update-copyright.git] / update_copyright.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2009 W. Trevor King <wking@drexel.edu>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 # Update copyright information in source code with information from
20 # the bzr repository.  Run from the BE repository root.
21 #
22 # Replaces everything starting with '^# Copyright' and continuing with
23 # '^#' with an auto-generated copyright blurb.  If you want to add
24 # #-commented material after a copyright blurb, please insert a blank
25 # line between the blurb and your comment (as in this file), so the
26 # next run of update_copyright.sh doesn't clobber your comment.
27 #
28 # usage: update_copyright.sh [files ...]
29 #
30 # If no files are given, a list of files to update is generated
31 # automatically.
32
33 set -o pipefail
34
35 if [ $# -gt 0 ]; then
36     FILES="$*"
37 else
38     FILES=`grep -rc "# Copyright" . | grep -v ':0$' | cut -d: -f1`
39 fi
40
41 COPYRIGHT_TEXT="#
42 # This program is free software; you can redistribute it and/or modify
43 # it under the terms of the GNU General Public License as published by
44 # the Free Software Foundation; either version 2 of the License, or
45 # (at your option) any later version.
46 #
47 # This program is distributed in the hope that it will be useful,
48 # but WITHOUT ANY WARRANTY; without even the implied warranty of
49 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50 # GNU General Public License for more details.
51 #
52 # You should have received a copy of the GNU General Public License along
53 # with this program; if not, write to the Free Software Foundation, Inc.,
54 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."
55 # escape newlines and special characters
56 SED_RM_TRAIL_END='s/[\]n$//'         # strip trailing newline escape
57 SED_ESC_SPECIAL='s/\([()/]\)/\\\1/g' # escape special characters
58 ESCAPED_TEXT=`echo "$COPYRIGHT_TEXT" | awk '{printf("%s\\\\n", $0)}' | sed "$SED_RM_TRAIL_END" | sed "$SED_ESC_SPECIAL"`
59
60 # adjust the AUTHORS file
61 AUTHORS=`bzr log | grep '^ *committer\|^ *author' | cut -d: -f2 | sed 's/ <.*//;s/^ *//' | sort | uniq`
62 AUTHORS=`echo "$AUTHORS" | grep -v 'j\^\|wking\|John Doe\|gianluca'` # remove non-names
63 echo "Bugs Everywhere was written by:" > AUTHORS
64 echo "$AUTHORS" >> AUTHORS
65
66 CURRENT_YEAR=`date +"%Y"`
67 TMP=`mktemp BE_update_copyright.XXXXXXX`
68
69 for file in $FILES
70 do
71     # Ignore some files
72     if [ "${file:0:5}" == "./.be" ]; then
73         continue
74     fi
75     if [ "${file:0:6}" == "./.bzr" ]; then
76         continue
77     fi
78     if [ "${file:0:7}" == "./build" ]; then
79         continue
80     fi
81     if [ "$file" == "./COPYING" ]; then
82         continue
83     fi
84     if [ "$file" == "./update_copyright.sh" ]; then
85         continue
86     fi
87     if [ "$file" == "./xml/catmutt" ]; then
88         continue
89     fi
90     echo "Processing $file"
91
92     # Get author history from bzr
93     AUTHORS=`bzr log "$file" | grep "^ *author: \|^ *committer: " | cut -d: -f2 | sed 's/^ *//;s/ *$//' | sort | uniq`
94     if [ $? -ne 0 ]; then
95         continue # bzr doesn't version that file
96     fi
97     ORIG_YEAR=`bzr log "$file" | grep "^ *timestamp: " | tail -n1 | sed 's/^ *//;' | cut -b 16-19`
98
99     # Tweak the author list to make up for problems in the bzr
100     # history, change of email address, etc.
101
102     # Consolidate Chris Ball
103     GREP_OUT=`echo "$AUTHORS" | grep 'Chris Ball <cjb@laptop.org>'`
104     if [ -n "$GREP_OUT" ]; then
105         AUTHORS=`echo "$AUTHORS" | grep -v '^Chris Ball <cjb@thunk.printf.net>$'`
106     fi
107
108     # Consolidate Aaron Bentley
109     AUTHORS=`echo "$AUTHORS" | sed 's/<abentley@panoramicfeedback.com>/and Panometrics, Inc./'`
110     GREP_OUT=`echo "$AUTHORS" | grep 'Aaron Bentley and Panometrics, Inc.'`
111     if [ -n "$GREP_OUT" ]; then
112         AUTHORS=`echo "$AUTHORS" | grep -v '^Aaron Bentley <aaron.bentley@utoronto.ca>$'`
113     fi
114
115     # Consolidate Ben Finney
116     AUTHORS=`echo "$AUTHORS" | sed 's/John Doe <jdoe@example.com>/Ben Finney <ben+python@benfinney.id.au>/'`
117     GREP_OUt=`echo "$AUTHORS" | grep 'Ben Finney <ben+python@benfinney.id.au>'`
118     if [ -n "$GREP_OUT" ]; then
119         AUTHORS=`echo "$AUTHORS" | grep -v '^Ben Finney <benf@cybersource.com.au>$'`
120     fi
121
122     # Consolidate Trevor King
123     AUTHORS=`echo "$AUTHORS" | grep -v "wking <wking@mjolnir>"`
124
125     # Consolidate Gianluca Montecchi
126     AUTHORS=`echo "$AUTHORS" | grep -v "gianluca"`
127
128     # Sort again...
129     AUTHORS=`echo "$AUTHORS" | sort | uniq`
130
131     # Generate new Copyright string
132     if [ "$ORIG_YEAR" == "$CURRENT_YEAR" ]; then
133         DATE_RANGE="$CURRENT_YEAR"
134         DATE_SPACE="    "
135     else
136         DATE_RANGE="${ORIG_YEAR}-${CURRENT_YEAR}"
137         DATE_SPACE="         "
138     fi
139     NUM_AUTHORS=`echo "$AUTHORS" | wc -l`
140     FIRST_AUTHOR=`echo "$AUTHORS" | head -n 1`
141     COPYRIGHT="# Copyright (C) $DATE_RANGE $FIRST_AUTHOR"
142     if [ $NUM_AUTHORS -gt 1 ]; then
143         OTHER_AUTHORS=`echo "$AUTHORS" | tail -n +2`
144         while read AUTHOR; do
145             COPYRIGHT=`echo "$COPYRIGHT\\n#               $DATE_SPACE $AUTHOR"`
146         done < <(echo "$OTHER_AUTHORS")
147     fi
148     COPYRIGHT=`echo "$COPYRIGHT\\n$ESCAPED_TEXT"`
149
150     # Strip old copyright info and replace with tag
151     awk 'BEGIN{incopy==0}{if(match($0, "^# Copyright")>0){incopy=1; print "-xyz-COPYRIGHT-zyx-"}else{if(incopy==0){print $0}else{if(match($0, "^#")==0){incopy=0; print $0}}}}' "$file" > "$TMP"
152
153     # Replace tag in with new string
154     sed -i "s/^-xyz-COPYRIGHT-zyx-$/$COPYRIGHT/" "$TMP"
155     cp "$TMP" "$file"
156 done
157
158 rm -f "$TMP"