|
#!/bin/bash |
|
# File Protection Workflow - Apply custom licensing headers to your code |
|
|
|
# Configuration - Edit these values or create .protection-config |
|
if [[ -f ".protection-config" ]]; then |
|
source .protection-config |
|
else |
|
COMPANY_NAME="Your Company Name" |
|
YEAR=$(date +%Y) |
|
AUTHOR_NAME="Your Name" |
|
EMAIL="[email protected]" |
|
fi |
|
|
|
# Define your protection template |
|
create_protection_template() { |
|
cat > .license-template << 'EOF' |
|
# Copyright (c) YEAR COMPANY_NAME. All rights reserved. |
|
# Author: AUTHOR_NAME <EMAIL> |
|
# |
|
# PROPRIETARY AND CONFIDENTIAL |
|
# This software is the confidential and proprietary information of COMPANY_NAME. |
|
# You shall not disclose such confidential information and shall use it only |
|
# in accordance with the terms of the license agreement you entered into. |
|
# |
|
# Unauthorized copying, distribution, or use is strictly prohibited. |
|
# For licensing inquiries: EMAIL |
|
EOF |
|
} |
|
|
|
# Create dual license template (for your "moonshot" projects) |
|
create_dual_license_template() { |
|
cat > .dual-license-template << 'EOF' |
|
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
# Copyright (c) YEAR COMPANY_NAME <EMAIL> |
|
# |
|
# 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 |
|
# by the Free Software Foundation, either version 3 of the License, or |
|
# (at your option) any later version. |
|
# |
|
# COMMERCIAL LICENSE AVAILABLE |
|
# For proprietary use, commercial licenses are available. |
|
# Free for: Personal use, startups <$1M revenue, nonprofits |
|
# Contact EMAIL for commercial licensing terms. |
|
EOF |
|
} |
|
|
|
# Function to detect file type and return appropriate comment syntax |
|
get_comment_syntax() { |
|
local file="$1" |
|
local ext="${file##*.}" |
|
|
|
case "$ext" in |
|
"py"|"sh"|"bash"|"zsh"|"rb"|"pl"|"yml"|"yaml"|"conf") |
|
echo "#" |
|
;; |
|
"js"|"jsx"|"ts"|"tsx"|"java"|"c"|"cpp"|"h"|"hpp"|"cs"|"go"|"rs"|"php"|"swift") |
|
echo "//" |
|
;; |
|
"html"|"xml"|"svg") |
|
echo "<!-- -->" |
|
;; |
|
"css"|"scss"|"sass") |
|
echo "/* */" |
|
;; |
|
"sql") |
|
echo "--" |
|
;; |
|
*) |
|
echo "#" # Default to hash comments |
|
;; |
|
esac |
|
} |
|
|
|
# Function to apply license header to a single file |
|
apply_license_to_file() { |
|
local file="$1" |
|
local license_type="$2" # "proprietary" or "dual" |
|
local temp_file=$(mktemp) |
|
|
|
# Skip if file already has license |
|
if head -n 5 "$file" | grep -q "Copyright\|SPDX-License-Identifier"; then |
|
echo "⚠️ License already exists in $file - skipping" |
|
return |
|
fi |
|
|
|
local comment_style=$(get_comment_syntax "$file") |
|
local template_file=".${license_type}-license-template" |
|
|
|
# Create the header with proper comment syntax |
|
case "$comment_style" in |
|
"#") |
|
sed "s/^# //g; s/YEAR/$YEAR/g; s/COMPANY_NAME/$COMPANY_NAME/g; s/AUTHOR_NAME/$AUTHOR_NAME/g; s/EMAIL/$EMAIL/g" "$template_file" | sed 's/^/# /' > "$temp_file" |
|
;; |
|
"//") |
|
sed "s/^# //g; s/YEAR/$YEAR/g; s/COMPANY_NAME/$COMPANY_NAME/g; s/AUTHOR_NAME/$AUTHOR_NAME/g; s/EMAIL/$EMAIL/g" "$template_file" | sed 's/^/\/\/ /' > "$temp_file" |
|
;; |
|
"<!-- -->") |
|
echo "<!--" > "$temp_file" |
|
sed "s/^# //g; s/YEAR/$YEAR/g; s/COMPANY_NAME/$COMPANY_NAME/g; s/AUTHOR_NAME/$AUTHOR_NAME/g; s/EMAIL/$EMAIL/g" "$template_file" >> "$temp_file" |
|
echo "-->" >> "$temp_file" |
|
;; |
|
"/* */") |
|
echo "/*" > "$temp_file" |
|
sed "s/^# //g; s/YEAR/$YEAR/g; s/COMPANY_NAME/$COMPANY_NAME/g; s/AUTHOR_NAME/$AUTHOR_NAME/g; s/EMAIL/$EMAIL/g" "$template_file" | sed 's/^/ * /' >> "$temp_file" |
|
echo " */" >> "$temp_file" |
|
;; |
|
"--") |
|
sed "s/^# //g; s/YEAR/$YEAR/g; s/COMPANY_NAME/$COMPANY_NAME/g; s/AUTHOR_NAME/$AUTHOR_NAME/g; s/EMAIL/$EMAIL/g" "$template_file" | sed 's/^/-- /' > "$temp_file" |
|
;; |
|
esac |
|
|
|
# Add blank line and prepend to original file |
|
echo "" >> "$temp_file" |
|
cat "$file" >> "$temp_file" |
|
mv "$temp_file" "$file" |
|
|
|
echo "✅ Applied $license_type license to $file" |
|
} |
|
|
|
# Function to process directory recursively |
|
protect_directory() { |
|
local dir="$1" |
|
local license_type="$2" |
|
local pattern="$3" |
|
|
|
echo "🔒 Protecting files in $dir with $license_type license..." |
|
|
|
find "$dir" -type f -name "$pattern" | while read -r file; do |
|
# Skip hidden files, node_modules, .git, etc. |
|
if [[ "$file" =~ /\. ]] || [[ "$file" =~ node_modules ]] || [[ "$file" =~ \.git/ ]]; then |
|
continue |
|
fi |
|
|
|
apply_license_to_file "$file" "$license_type" |
|
done |
|
} |
|
|
|
# Main workflow functions |
|
setup_protection() { |
|
echo "🚀 Setting up file protection workflow..." |
|
|
|
# Create license templates |
|
create_protection_template |
|
create_dual_license_template |
|
|
|
# Replace placeholders in templates |
|
sed -i "s/YEAR/$YEAR/g; s/COMPANY_NAME/$COMPANY_NAME/g; s/AUTHOR_NAME/$AUTHOR_NAME/g; s/EMAIL/$EMAIL/g" .license-template .dual-license-template |
|
|
|
echo "✅ Protection templates created!" |
|
echo "📝 Edit .license-template and .dual-license-template to customize" |
|
} |
|
|
|
# Protect current project with proprietary license |
|
protect_proprietary() { |
|
local patterns=("*.py" "*.js" "*.jsx" "*.ts" "*.tsx" "*.java" "*.c" "*.cpp" "*.h" "*.hpp" "*.go" "*.rs" "*.php" "*.rb" "*.swift") |
|
|
|
for pattern in "${patterns[@]}"; do |
|
protect_directory "." "proprietary" "$pattern" |
|
done |
|
} |
|
|
|
# Protect current project with dual license (AGPL + Commercial) |
|
protect_dual() { |
|
local patterns=("*.py" "*.js" "*.jsx" "*.ts" "*.tsx" "*.java" "*.c" "*.cpp" "*.h" "*.hpp" "*.go" "*.rs" "*.php" "*.rb" "*.swift") |
|
|
|
for pattern in "${patterns[@]}"; do |
|
protect_directory "." "dual" "$pattern" |
|
done |
|
} |
|
|
|
# Git pre-commit hook to auto-protect new files |
|
install_git_hook() { |
|
cat > .git/hooks/pre-commit << 'HOOK_EOF' |
|
#!/bin/bash |
|
# Auto-apply license headers to new files |
|
|
|
# Get list of staged files |
|
staged_files=$(git diff --cached --name-only --diff-filter=A) |
|
|
|
for file in $staged_files; do |
|
if [[ -f "$file" ]] && [[ "$file" =~ \.(py|js|jsx|ts|tsx|java|c|cpp|h|hpp|go|rs|php|rb|swift)$ ]]; then |
|
# Check if file lacks license header |
|
if ! head -n 5 "$file" | grep -q "Copyright\|SPDX-License-Identifier"; then |
|
echo "⚠️ New file $file lacks license header!" |
|
echo "Run: ./protect-files.sh apply-to-file $file [proprietary|dual]" |
|
exit 1 |
|
fi |
|
fi |
|
done |
|
HOOK_EOF |
|
|
|
chmod +x .git/hooks/pre-commit |
|
echo "✅ Git pre-commit hook installed - will check new files for license headers" |
|
} |
|
|
|
# Command line interface |
|
case "$1" in |
|
"setup") |
|
setup_protection |
|
;; |
|
"proprietary") |
|
protect_proprietary |
|
;; |
|
"dual") |
|
protect_dual |
|
;; |
|
"install-hook") |
|
install_git_hook |
|
;; |
|
"apply-to-file") |
|
if [[ -z "$2" ]] || [[ -z "$3" ]]; then |
|
echo "Usage: $0 apply-to-file <filename> <proprietary|dual>" |
|
exit 1 |
|
fi |
|
apply_license_to_file "$2" "$3" |
|
;; |
|
*) |
|
echo "File Protection Workflow" |
|
echo "" |
|
echo "Usage: $0 <command>" |
|
echo "" |
|
echo "Commands:" |
|
echo " setup - Create license templates" |
|
echo " proprietary - Apply proprietary licenses to all code files" |
|
echo " dual - Apply AGPL+Commercial dual license to all files" |
|
echo " install-hook - Install git pre-commit hook to check new files" |
|
echo " apply-to-file - Apply license to specific file" |
|
echo "" |
|
echo "Examples:" |
|
echo " $0 setup" |
|
echo " $0 dual" |
|
echo " $0 apply-to-file src/main.py proprietary" |
|
;; |
|
esac |