#!/bin/bash

# ============================================
# Add copyright header to all .c and .h files
# ============================================

AUTHOR="Luna Kernel Team"
EMAIL="lkx9873@gmail.com"
COPYRIGHT="Copyright (C) 2026 Luna Kernel Team"
LICENSE="SPDX-License-Identifier: GPL-2.0"

GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m'

generate_header() {
    local filename=$(basename "$1")
    cat <<EOF
// $LICENSE
/*
 * $filename
 * $COPYRIGHT
 * Author: $AUTHOR <$EMAIL>
 */
EOF
}

has_header() {
    head -5 "$1" | grep -q "SPDX-License-Identifier"
}

process_file() {
    local file="$1"
    
    if has_header "$file"; then
        echo -e "${YELLOW}[SKIP]${NC} $file (header exists)"
        return 0
    fi
    
    local header=$(generate_header "$file")
    local content=$(cat "$file")
    
    echo "$header" > "$file.tmp"
    echo "$content" >> "$file.tmp"
    mv "$file.tmp" "$file"
    
    echo -e "${GREEN}[ADD]${NC} $file"
}

main() {
    echo -e "${GREEN}Adding copyright headers...${NC}"
    echo ""
    
    local files=$(find . -type f \( -name "*.c" -o -name "*.h" \) -not -path "./build/*" -not -path "./.git/*")
    
    local count=0
    for file in $files; do
        process_file "$file"
        ((count++))
    done
    
    echo ""
    echo -e "${GREEN}Done! Processed $count files${NC}"
}

main
