forked from plumed/plumed2
/
header.sh
executable file
·141 lines (110 loc) · 3.01 KB
/
header.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# This script adds a LGPL header to all the source files
# If the header is already present, it does not touch the file
# Please run it whenever you add a new file so as to keep copyright info there!
test -d ../.git || {
echo "ERROR: this script should be run from a git repository!"
exit 1
}
DIRS=$1
# remove trailing "/"
DIRS=${DIRS%%/*}
test -z "$DIRS" && DIRS=*
for dir in $DIRS
do
test -d "$dir" || continue
cd $dir
FIX_COPYRIGHT=""
test -f COPYRIGHT && FIX_COPYRIGHT="$(<COPYRIGHT)"
for file in *.c *.cpp *.h *.inc.in
do
test -f "$file" || continue
if test -n "$FIX_COPYRIGHT"
then
COPYRIGHT="$FIX_COPYRIGHT"
echo -n "Custom header for file: $file"
else
years=$(git log --follow -M75% --format=%aD $file |
sort -r -n -k 4 |
awk -v now="$(date +%Y)" '{if(NR==1)last=$4;}END{
# override last with now
last=now
# Notice that the script could be simplified, I leave it as is for reference:
first=$4
if(first=="") print ""
else if(first==last) print first;
else if(first+1==last) print first "," last;
else print first "-" last}')
if test -z "$years" ; then
echo "Error processing file: $file"
continue
fi
COPYRIGHT="\
Copyright (c) $years The plumed team
(see the PEOPLE file at the root of the distribution for a list of names)
See http://www.plumed.org for more information.
This file is part of plumed, version 2.
plumed is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
plumed is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>."
echo -n "LGPL header ($years) for file: $file"
fi
{
plus="+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
cat << EOF
/* $plus
$COPYRIGHT
$plus */
EOF
awk -v plus=$plus 'BEGIN{
inheader=0;
}{
if($1=="/*" && $2==plus) inheader=1;
if(inheader==0) print $0
if($1==plus && $2=="*/") inheader=0;
}' $file
} > $file.tmp
case "$file" in
(*h)
ff="${file//./_}"
guard="__PLUMED_${dir}_${ff}"
awk -v plus=$plus -v guard=$guard '
{
if(past==1){
if($1=="#ifndef"){
line=-10;
} else if($1=="#define" && line==-9){
found=1;
}
else if(found!=1 && NF==0);
else past=2;
}
if(past==0 || past==2) print $0;
if(past==0 && $1==plus && $2=="*/") {
past=1;
print "#ifndef "guard
print "#define "guard
}
if(NF>0)line++;
}END{
if(!found) print "#endif"
}' $file.tmp > $file.tmp2
mv $file.tmp2 $file.tmp
esac
if cmp -s $file $file.tmp ; then
echo
else
cp $file.tmp $file
echo " +++ PATCHED"
git add $file
fi
rm $file.tmp
done
cd -
done