Dash:
[t2-trunk.git] / scripts / Create-CopyPatch
blob5196e906b4db24353f3723060970e8f581c52a27
1 #!/bin/bash
2 # --- T2-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by scripts/Create-CopyPatch.
4 #
5 # T2 SDE: scripts/Create-CopyPatch
6 # Copyright (C) 2004 - 2014 The T2 SDE Project
7 # Copyright (C) 1998 - 2003 ROCK Linux Project
8 #
9 # More information can be found in the files COPYING and README.
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; version 2 of the License. A copy of the
14 # GNU General Public License can be found in the file COPYING.
15 # --- T2-COPYRIGHT-NOTE-END ---
17 copynote=`mktemp`
18 copynotepatch=`mktemp`
19 rocknote=`mktemp`
20 oldfile=`mktemp`
21 newfile=`mktemp`
22 tmpfile=`mktemp`
24 cat << EOT > $copynote
25 T2 SDE: @@FILENAME@@
26 @@COPYRIGHT@@
28 This Copyright note is generated by scripts/Create-CopyPatch,
29 more information can be found in the files COPYING and README.
31 EOT
33 cp $copynote $copynotepatch
35 cat << EOT >> $copynote
36 This program is free software; you can redistribute it and/or modify
37 it under the terms of the GNU General Public License version 2.
38 EOT
40 cat << EOT >> $copynotepatch
41 This patch file is dual-licensed. It is available under the license the
42 patched project is licensed under, as long as it is an OpenSource license
43 as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
44 of the GNU General Public License version 2 as used by the T2 SDE.
45 EOT
47 echo "Creating copy.patch ..." >&2
49 usesvn=
50 if [ "$1" = "-svn" ]; then
51 usesvn=1; shift
54 if [ $# = 0 ]; then
55 set architecture/. misc/. package/. scripts/. target/.
56 else
57 # check if file or package name was given
58 files=""
59 for i; do
60 if [ -f $i -o -d $i ]; then
61 files="$files ${i#./}"
62 elif [ -d package/*/$i ]; then
63 for each in `echo package/*/$i`; do
64 [[ $each = *~ ]] && continue
65 files="$files $each/."
66 done
67 else
68 echo Cannot find \'$i\', ignoring. 1>&2
70 done
71 set -- $files
72 [ $# = 0 ] && exit
75 bash scripts/xfind.sh $* -type f ! -name "*~" \
76 ! -name Create-CopyPatch | sed 's,/\./,/,g' |
77 while read filename; do
78 # determine the comment mode first
79 mode=none
80 case "$filename" in
81 *.cache) mode=skip ;;
82 */Makefile|*.sh|*.pl|*.in|*.hlp|*.conf) mode=sh ;;
83 *.cron|*.postinstall|*.init) mode=sh ;;
84 *.h|*.c|*.cc|*.cpp|*.lex|*.y|*.spec|*.tcx|*.tmpl|*.tcc) mode=c ;;
85 *.lua) mode=lua ;;
86 *.desc) mode=asci ;;
87 *scripts/[A-Z][a-z-]*|*/parse-config*) mode=sh ;;
88 *patch|*diff|*patch.*) mode=sh ;;
89 *m4) mode=m4 ;;
90 esac
92 #echo "Mode type: $mode"
94 grep -q -- '--- NO-\(T2\|ROCK\)-COPYRIGHT-NOTE ---' "$filename" &&
95 continue
97 case "$mode" in
98 skip) continue ;;
99 none)
100 if head -n 1 "$filename" | grep -q '^#!'; then
101 mode=sh
102 else
103 case "`sed -n 's/^ *\([^ ]\+\) .*COPYRIGHT-NOTE-BEGIN .*/\1/p' "$filename"`" in
104 "#") mode=sh ;;
105 "*") mode=c ;;
106 esac
109 if [ "$mode" == none ]; then
110 echo "Unknown type for $filename" 1>&2
111 continue
113 esac
115 # make a copy in the case we have no matching conditional below
116 sed 's,ROCK-COPYRIGHT-NOTE,T2-COPYRIGHT-NOTE,g' "$filename" > $oldfile
118 tag=$( sed -n -e '/^\(.*\)--- \(T2\|ROCK\)-COPYRIGHT-NOTE-BEGIN ---.*/{s//\1/;p;q;}' "$filename" )
119 thisyear=`date +%Y`
121 if [ -n "$tag" ]; then
122 # has a note, catch copyrights
123 oldcopyright=`sed -e '/--- T2-COPYRIGHT-NOTE-BEGIN ---/,/--- T2-COPYRIGHT-NOTE-END ---/!d' \
124 -e '/.*\(Copyright (C) .*\)/!d;s//\1/;' \
125 -e 's/Clifford Wolf/ROCK Linux Project/' \
126 $oldfile`
127 else
128 oldcopyright=
131 if echo "$oldcopyright" | grep -q 'T2 SDE'; then
132 # if has T2 copyright, renew if necesary
133 since=$( echo "$oldcopyright" | sed -n -e 's,.* (C) \([^ ]*\) .*T2 SDE.*,\1,p' )
135 if [ $since -lt $thisyear ]; then
136 copyright=`echo "$oldcopyright" | sed -e \
137 "s,.*T2 SDE.*,Copyright (C) $since - $thisyear The T2 SDE Project,"`
138 else
139 copyright="$oldcopyright"
141 else
142 # else, add one...
143 if [ "$usesvn" = 1 ]; then
144 since=`TZ=UTC svn log -q "$filename" 2> /dev/null |
145 grep -a '^r' | tail -n 1 | cut -d'|' -f3 | cut -c2-5`
147 # but until 2003 we were ROCK Linux, so
148 if [ ${since:-2004} -lt 2004 ]; then
149 oldcopyright="Copyright (C) $since - 2003 ROCK Linux Project"
150 since=2004
152 else
153 since=$thisyear
156 # prepare a copyright tag
157 if [ ${since:-1} -lt $thisyear ]; then
158 copyright="Copyright (C) ${since:-ESVNTO} - $thisyear The T2 SDE Project"
159 else
160 copyright="Copyright (C) $thisyear The T2 SDE Project"
162 copyright="$copyright${oldcopyright:+\\n$oldcopyright}"
165 action='i'
166 pretag= posttag=
167 if [ -z "$tag" ]; then
168 # doesn't have a note
169 if head -n 1 "$filename" | grep -a -q '^#!'; then
170 action='a'
172 case "$mode" in
174 tag="# "
176 asci)
177 tag="[COPY] "
180 pretag='/*' posttag=' */'
181 tag=' * '
184 tag="dnl "
186 lua)
187 tag="-- "
189 esac
191 # insert one
192 sed -i "1 $action\\
193 ${pretag:+$pretag\\
194 }$tag--- T2-COPYRIGHT-NOTE-BEGIN ---\\
195 $tag--- T2-COPYRIGHT-NOTE-END ---\\
196 ${posttag:+$posttag\\
197 }" $oldfile
200 mangled_filename=`echo "$filename" |
201 sed 's,package/\([^/]*\)/\(.*\),package/*/\2,'`
203 #echo BEFORE
204 #cat $oldfile
206 if [ "$tag" ]; then
207 # implant T2 copy note
209 grep -a -B 100000 -- '--- T2-COPYRIGHT-NOTE-BEGIN ---' $oldfile
211 if [ "$filename" != "${filename%/*.diff}" -o \
212 "$filename" != "${filename%/*.patch}" -o \
213 "$filename" != "${filename%/*.patch.*}" ]; then
214 sed -e "s,@@FILENAME@@,$mangled_filename,; \
215 s,@@COPYRIGHT@@,${copyright//
216 /\\n},;" $copynotepatch
217 else
218 sed -e "s,@@FILENAME@@,$mangled_filename,; \
219 s,@@COPYRIGHT@@,${copyright//
220 /\\n},;" $copynote
222 # we need a separated sed call because $rockcopyright adds a new line
223 } | sed -e "s,^,$tag,"
225 grep -a -A 100000 -- '--- T2-COPYRIGHT-NOTE-END ---' $oldfile
226 } > $newfile
228 # create the difference
229 if ! cmp -s $oldfile $newfile; then
230 echo "Creating patch for $filename." >&2
231 diff -ua "./$filename" $newfile |
232 sed -e "2 s,$newfile,./$filename,"
234 else
235 echo "WARNING: No Copyright tags in $filename found!" >&2
237 done
239 rm -f $copynote $copynotepatch $newfile