From 64017b645419b864999233a7d390425e571c458d Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Mon, 4 Aug 2025 11:34:58 +0300 Subject: Updated copyright script to allow changing the header --- tools/copyright.sh | 70 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 14 deletions(-) (limited to 'tools') diff --git a/tools/copyright.sh b/tools/copyright.sh index 0c6a517..c74579f 100755 --- a/tools/copyright.sh +++ b/tools/copyright.sh @@ -5,8 +5,8 @@ # Get current year CURRENT_YEAR=$(date +%Y) -HEADER="// Eko: A terminal based social media platform -// Copyright (C) $CURRENT_YEAR Kyren223 +LICENSE_HEADER_TEMPLATE="// Eko: A terminal-native social media platform +// Copyright (C) {YEARS} Kyren223 // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published @@ -54,10 +54,39 @@ while IFS= read -r file; do continue fi - head_block=$(head -n 10 "$file") - copyright_line=$(echo "$head_block" | grep -E 'Copyright \(C\) [0-9]{4}') - if [[ -z "$copyright_line" ]]; then + head_block=$(head -n 20 "$file") + license_block=$(echo "$head_block" | sed -n '/Copyright (C) [0-9]\{4\}/,/gnu.org\/licenses\/>/p') + copyright_line=$(echo "$license_block" | grep -E 'Copyright \(C\) [0-9]{4}') + + START_YEAR="" + END_YEAR="" + if [[ -n "$copyright_line" ]]; then + # Parse years + if [[ "$copyright_line" =~ Copyright\ \(C\)\ ([0-9]{4})-([0-9]{4}) ]]; then + START_YEAR="${BASH_REMATCH[1]}" + END_YEAR="${BASH_REMATCH[2]}" + elif [[ "$copyright_line" =~ Copyright\ \(C\)\ ([0-9]{4}) ]]; then + START_YEAR="${BASH_REMATCH[1]}" + fi + fi + + # Determine what the correct copyright line should be + if [[ -z "$START_YEAR" ]]; then + YEARS="$CURRENT_YEAR" + elif [[ "$START_YEAR" == "$CURRENT_YEAR" ]]; then + YEARS="$CURRENT_YEAR" + else + YEARS="$START_YEAR-$CURRENT_YEAR" + fi + + HEADER="${LICENSE_HEADER_TEMPLATE//\{YEARS\}/$YEARS}" + + existing_header=$(sed -n '1,/https:\/\/www\.gnu\.org\/licenses\/>/p' "$file") + # existing_header_trimmed=$(echo "$existing_header" | sed 's/[[:space:]]*$//') + # new_header_trimmed=$(echo "$HEADER" | sed 's/[[:space:]]*$//') + + if [[ -z "$license_block" ]]; then MODIFIED=true if $CHECK_MODE; then echo "Missing copyright: $file" @@ -65,19 +94,32 @@ while IFS= read -r file; do { echo "$HEADER"; echo; cat "$file"; } > "$file.tmp" && mv "$file.tmp" "$file" echo "Added license header to $file" fi - continue - fi - - # Extract the starting year - start_year=$(echo "$copyright_line" | sed -E 's/.*Copyright \(C\) ([0-9]{4})(-[0-9]{4})?.*/\1/') - - if [[ "$start_year" != "$CURRENT_YEAR" ]]; then + elif [[ "$existing_header" != "$HEADER" ]]; then MODIFIED=true if $CHECK_MODE; then echo "Needs update: $file" else - sed -i "1,10s/Copyright (C) $start_year/Copyright (C) $start_year-$CURRENT_YEAR/" "$file" - echo "Updated year in $file" + awk -v header="$HEADER" ' + BEGIN {skipping=1} + { + if (NR == 1) { + print header + next + } + + if (skipping) { + if ($0 ~ //) { + skipping = 0 + next + } else { + next + } + } + + print + } + ' "$file" > "$file.tmp" && mv "$file.tmp" "$file" + echo "Updated license in $file" fi fi done < <(find . -type f -name "*.go") -- cgit v1.3.1