Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge 'master' into mc
[simgrid.git] / tools / 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 head -n 1 "$file" | grep -q '^#!'; then
96         script=1
97     else
98         script=0
99     fi
100
101     ### 1. create new template
102     dates=$(get_dates "$file" | sort -u | format_dates)
103     sed "s/(c) [[:digit:], -]*\./(c) $dates./" "$template" > "$tmp_copy"
104     printf '\n' >> "$tmp_copy"
105
106     # fix comments for scripts
107     if [ $script = 1 ]; then
108         sed -i 's!^..!#!;s! *\*/!!' "$tmp_copy"
109     fi
110
111     ### 2. copy file body
112     if grep -q 'Copyright.*SimGrid' "$file"; then
113         sed '/Copyright.*SimGrid/,$d' "$file" > "$tmp_head"
114         sed -i '${\!^/\* *$!d}' "$tmp_head"
115         sed '1,/the terms of the license/d' "$file" > "$tmp_foot"
116     elif [ $script = 1 ]; then
117         head -n 1 "$file" > "$tmp_head"
118         tail -n +2  "$file" > "$tmp_foot"
119         printf '\n' >> "$tmp_head"
120     else
121         :> "$tmp_head"
122         cp "$file" "$tmp_foot"
123     fi
124     sed -i '1{\!^ *\*/!d};/[^[:space:]]/,$!d' "$tmp_foot"
125
126     ### 3. concatenate new template and file body into $file
127 #    cat "$tmp_head"
128 #    cat "$tmp_copy"
129 #    cat "$tmp_foot"
130     cat "$tmp_head" "$tmp_copy" "$tmp_foot" > $file
131 done
132
133 cat <<EOF
134
135 All files processed.
136
137 *** DO NOT FORGET TO DOUBLE CHECK CHANGES BEFORE DOING ANY COMMIT! ***
138
139 EOF