Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar
[simgrid.git] / tools / internal / update_copyright_header
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2014-2019. 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 get_dates() {
31     local file=$1
32     local date
33     sed -n '/Copyright.*SimGrid/{
34               s/.*(c) \([[:digit:], -]*\).*/\1/
35               s/[, ]\+/\n/g
36               p
37             }' "$file" \
38     | while read date; do
39         case "$date" in
40             "")
41                 ;;
42             *-*)
43                 seq ${date/-/ }
44                 ;;
45             *)
46                 echo $date
47                 ;;
48         esac
49     done
50     git log --format=%ad "$file" | cut -d\  -f5 | uniq
51 }
52
53 format_dates() {
54     local first
55     local last
56     local next
57     read first
58     last=$first
59     while read next; do
60         if [ $next -eq $((last + 1)) ]; then
61             last=$next
62         else
63             if [ $first -eq $last ]; then
64                 printf '%d, ' $first
65             else
66                 printf '%d-%d, ' $first $last
67             fi
68             first=$next
69             last=$first
70         fi
71     done
72     if [ $first -eq $last ]; then
73         printf '%d\n' $first
74     else
75         printf '%d-%d\n' $first $last
76     fi
77 }
78
79 tmp_head=$(mktemp)
80 tmp_copy=$(mktemp)
81 tmp_foot=$(mktemp)
82 trap "rm -f \"$tmp_head\" \"$tmp_copy\" \"$tmp_foot\"" EXIT
83
84 for file; do
85     echo "########## $file ##########"
86
87     if [ ! -f "$file" ]; then
88         echo "!!! skip"
89         continue
90     fi
91
92     if grep -q "Copyright.*SimGrid" $file ; then
93         if head -n 1 "$file" | grep -q '^#!'; then
94             script=1
95         else
96             script=0
97         fi
98
99         ### 1. create new template
100         dates=$(get_dates "$file" | sort -u | format_dates)
101         sed "s/(c) [[:digit:], -]*\./(c) $dates./" "$template" > "$tmp_copy"
102         printf '\n' >> "$tmp_copy"
103
104         # fix comments for scripts
105         if [ $script = 1 ]; then
106             sed -i 's!^..!#!;s! *\*/!!' "$tmp_copy"
107         fi
108
109         ### 2. copy file body
110         if grep -q 'Copyright.*SimGrid' "$file"; then
111             sed '/Copyright.*SimGrid/,$d' "$file" > "$tmp_head"
112             sed -i '${\!^/\* *$!d}' "$tmp_head"
113             sed '1,/the terms of the license/d' "$file" > "$tmp_foot"
114         elif [ $script = 1 ]; then
115             head -n 1 "$file" > "$tmp_head"
116             tail -n +2  "$file" > "$tmp_foot"
117             printf '\n' >> "$tmp_head"
118         else
119             :> "$tmp_head"
120             cp "$file" "$tmp_foot"
121         fi
122         sed -i '1{\!^ *\*/!d};/[^[:space:]]/,$!d' "$tmp_foot"
123
124         ### 3. concatenate new template and file body into $file
125 #        cat "$tmp_head"
126 #        cat "$tmp_copy"
127 #        cat "$tmp_foot"
128         cat "$tmp_head" "$tmp_copy" "$tmp_foot" > $file
129     else
130         echo "Pass: there is no SimGrid Copyright header."
131     fi ; #
132 done
133
134 cat <<EOF
135
136 All files processed.
137
138 *** DO NOT FORGET TO DOUBLE CHECK CHANGES BEFORE DOING ANY COMMIT! ***
139
140 EOF