Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
only proceed files with an existing SimGrid copyright header
[simgrid.git] / tools / internal / update_copyright_header
1 #!/bin/bash
2
3 # Copyright (c) 2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 if [ "$1" = "-t" ]; then
10     template=$2
11     shift 2
12 else
13     template=$(git rev-parse --show-toplevel)/COPYRIGHT.template
14 fi
15
16 if [ $# -eq 0 ]; then
17     cat >&2 <<EOF
18 Usage: $0 [-t COPYRIGHT.template] files...
19 EOF
20     exit 1
21 fi
22
23 if [ ! -r "$template" ]; then
24     printf 'File not found: %s\n' "$template" >&2
25     exit 1
26 fi
27
28 printf 'Using template: %s\n' "$template"
29
30 now=$(date +%Y)
31
32 get_dates() {
33     local file=$1
34     local date
35     sed -n '/Copyright.*SimGrid/{
36               s/.*(c) \([[:digit:], -]*\).*/\1/
37               s/[, ]\+/\n/g
38               p
39             }' "$file" \
40     | while read date; do
41         case "$date" in
42             "")
43                 ;;
44             *-*)
45                 seq ${date/-/ }
46                 ;;
47             *)
48                 echo $date
49                 ;;
50         esac
51     done
52     git log --format=%ad "$file" | cut -d\  -f5 | uniq
53     echo $now
54 }
55
56 format_dates() {
57     local first
58     local last
59     local next
60     read first
61     last=$first
62     while read next; do
63         if [ $next -eq $((last + 1)) ]; then
64             last=$next
65         else
66             if [ $first -eq $last ]; then
67                 printf '%d, ' $first
68             else
69                 printf '%d-%d, ' $first $last
70             fi
71             first=$next
72             last=$first
73         fi
74     done
75     if [ $first -eq $last ]; then
76         printf '%d\n' $first
77     else
78         printf '%d-%d\n' $first $last
79     fi
80 }
81
82 tmp_head=$(mktemp)
83 tmp_copy=$(mktemp)
84 tmp_foot=$(mktemp)
85 trap "rm -f \"$tmp_head\" \"$tmp_copy\" \"$tmp_foot\"" EXIT
86
87 for file; do
88     echo "########## $file ##########"
89
90     if [ ! -f "$file" ]; then
91         echo "!!! skip"
92         continue
93     fi
94
95     if grep -q "Copyright.*SimGrid" $file ; then 
96         if head -n 1 "$file" | grep -q '^#!'; then
97             script=1
98         else
99             script=0
100         fi
101
102         ### 1. create new template
103         dates=$(get_dates "$file" | sort -u | format_dates)
104         sed "s/(c) [[:digit:], -]*\./(c) $dates./" "$template" > "$tmp_copy"
105         printf '\n' >> "$tmp_copy"
106
107         # fix comments for scripts
108         if [ $script = 1 ]; then
109             sed -i 's!^..!#!;s! *\*/!!' "$tmp_copy"
110         fi
111
112         ### 2. copy file body
113         if grep -q 'Copyright.*SimGrid' "$file"; then
114             sed '/Copyright.*SimGrid/,$d' "$file" > "$tmp_head"
115             sed -i '${\!^/\* *$!d}' "$tmp_head"
116             sed '1,/the terms of the license/d' "$file" > "$tmp_foot"
117         elif [ $script = 1 ]; then
118             head -n 1 "$file" > "$tmp_head"
119             tail -n +2  "$file" > "$tmp_foot"
120             printf '\n' >> "$tmp_head"
121         else
122             :> "$tmp_head"
123             cp "$file" "$tmp_foot"
124         fi
125         sed -i '1{\!^ *\*/!d};/[^[:space:]]/,$!d' "$tmp_foot"
126
127         ### 3. concatenate new template and file body into $file
128 #        cat "$tmp_head"
129 #        cat "$tmp_copy"
130 #        cat "$tmp_foot"
131         cat "$tmp_head" "$tmp_copy" "$tmp_foot" > $file
132     else
133         echo "Pass: there is no SimGrid Copyright header."
134     fi ; # 
135 done
136
137 cat <<EOF
138
139 All files processed.
140
141 *** DO NOT FORGET TO DOUBLE CHECK CHANGES BEFORE DOING ANY COMMIT! ***
142
143 EOF