1#!/bin/bash
2
3read -r -d '' new_license <<'EOF'
4//
5// SPDX-License-Identifier: Zlib
6//
7// This siHAL software is provided 'as-is', without any express or implied
8// warranty. In no event will the authors be held liable for any damages
9// arising from the use of this software.
10//
11// Permission is granted to anyone to use this software for any purpose,
12// including commercial applications, and to alter it and redistribute it
13// freely, subject to the following restrictions:
14//
15// 1. The origin of this software must not be misrepresented; you must not
16//    claim that you wrote the original software. If you use this software
17//    in a product, an acknowledgment in the product documentation would be
18//    appreciated but is not required.
19// 2. Altered source versions must be plainly marked as such, and must not be
20//    misrepresented as being the original software.
21// 3. This notice may not be removed or altered from any source distribution.
22EOF
23
24set -euo pipefail
25
26source="./tmp/si32Hal"
27destination="./si32Hal"
28
29if [ -d "$destination" ]; then
30    rm -r "$destination"
31fi
32
33cp -r "$source" "$destination"
34
35# These have an incompatible license but we don't need them because we have
36# modules/hal/cmsis.
37rm -r "$destination/CPU"
38
39# These have an incompatible license but we don't need them.
40rm "$destination"/*/linker_*
41rm "$destination"/*/startup_*
42
43new_license_escaped=$(echo "$new_license" | sed -e 's/[\/&]/\\&/g' | perl -pe 's/\n/\\r\\n/')
44
45replace_copyright() {
46    local path="$1"
47
48    perl -i -0p \
49        -e '$s = s/(^\/\/[-]+\r\n)\/\/ Copyright \(c\) (201\d) by Silicon Laboratories.*?\r\n(?:\/\/.*?\r\n)*?(^\/\/[-]+\r\n)/\1\/\/ Copyright \2 (c) Silicon Laboratories Inc.\r\n'"$new_license_escaped"'\3/smg;' \
50        -e 'END { die "No copyright header found" if $s != 1 }' \
51        "$path"
52}
53
54export -f replace_copyright
55export new_license_escaped
56fd . si32Hal/ --type file -x bash -c 'replace_copyright "$0"'
57