Synvael/tools/add-license-headers.sh

60 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
# Prepend SPDX license identifiers to every Rust and Lua source file.
# Idempotent: files already containing an `SPDX-License-Identifier:` line are skipped.
# Run from the repository root: `./tools/add-license-headers.sh`
set -euo pipefail
readonly LICENSE_ID="AGPL-3.0-only"
readonly RUST_HEADER="// SPDX-License-Identifier: ${LICENSE_ID}"
readonly LUA_HEADER="-- SPDX-License-Identifier: ${LICENSE_ID}"
# Directories to scan, relative to the repository root.
readonly RUST_DIRS=("crates")
readonly LUA_DIRS=("assets/scripts" "mods")
added=0
skipped=0
prepend_if_missing() {
local file="$1"
local header="$2"
if grep -q "SPDX-License-Identifier:" "$file"; then
skipped=$((skipped + 1))
return
fi
# Use a temp file in the same directory so the rename is atomic on the same filesystem.
local tmp
tmp=$(mktemp "${file}.XXXXXX")
{
printf '%s\n\n' "$header"
cat "$file"
} >"$tmp"
mv "$tmp" "$file"
added=$((added + 1))
}
scan_dir() {
local dir="$1"
local ext="$2"
local header="$3"
[[ -d "$dir" ]] || return 0
while IFS= read -r -d '' file; do
prepend_if_missing "$file" "$header"
done < <(find "$dir" -type f -name "*.${ext}" -print0)
}
for dir in "${RUST_DIRS[@]}"; do
scan_dir "$dir" "rs" "$RUST_HEADER"
done
for dir in "${LUA_DIRS[@]}"; do
scan_dir "$dir" "lua" "$LUA_HEADER"
done
printf 'License headers added: %d, already present: %d\n' "$added" "$skipped"