#!/bin/bash
#
# Migration Script: Move to .example Config Pattern
#
# This script migrates the configuration system to use .example files
# for white-label protection. Run this once per repository.
#
# What it does:
# 1. Removes config files from git tracking (keeps local files)
# 2. Adds .example files to git tracking
# 3. Updates .gitignore
# 4. Creates a commit with these changes
#

set -e  # Exit on error

# Colors
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Config Migration to .example Pattern ${NC}"
echo -e "${BLUE}========================================${NC}\n"

# Check if in git repository
if [ ! -d .git ]; then
    echo -e "${RED}Error: Must be run from repository root${NC}"
    exit 1
fi

# Files to migrate
config_files=(
    "config/themes.php"
    "config/timebank-default.php"
    "config/timebank_cc.php"
)

echo -e "${YELLOW}This script will:${NC}"
echo "1. Remove config files from git tracking (local files remain)"
echo "2. Add .example template files to git"
echo "3. Update .gitignore if needed"
echo -e "\n${YELLOW}Your custom config files will be preserved locally.${NC}\n"

read -p "Continue? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Migration cancelled."
    exit 0
fi

echo -e "\n${BLUE}Step 1: Verifying .example files exist${NC}"
for config_file in "${config_files[@]}"; do
    if [ ! -f "${config_file}.example" ]; then
        if [ -f "$config_file" ]; then
            echo -e "${YELLOW}Creating ${config_file}.example from current file...${NC}"
            cp "$config_file" "${config_file}.example"
        else
            echo -e "${RED}Warning: $config_file doesn't exist, skipping...${NC}"
            continue
        fi
    else
        echo -e "${GREEN}✓ ${config_file}.example exists${NC}"
    fi
done

echo -e "\n${BLUE}Step 2: Removing config files from git tracking${NC}"
for config_file in "${config_files[@]}"; do
    if git ls-files --error-unmatch "$config_file" > /dev/null 2>&1; then
        echo -e "${YELLOW}Removing $config_file from git (keeping local file)...${NC}"
        git rm --cached "$config_file"
        echo -e "${GREEN}✓ Removed $config_file from git tracking${NC}"
    else
        echo -e "${YELLOW}$config_file not tracked in git, skipping...${NC}"
    fi
done

echo -e "\n${BLUE}Step 3: Adding .example files to git${NC}"
for config_file in "${config_files[@]}"; do
    if [ -f "${config_file}.example" ]; then
        git add "${config_file}.example"
        echo -e "${GREEN}✓ Added ${config_file}.example to git${NC}"
    fi
done

echo -e "\n${BLUE}Step 4: Verifying .gitignore${NC}"
if grep -q "/config/themes.php" .gitignore; then
    echo -e "${GREEN}✓ .gitignore already updated${NC}"
else
    echo -e "${YELLOW}Updating .gitignore...${NC}"
    git add .gitignore
    echo -e "${GREEN}✓ Added .gitignore changes${NC}"
fi

echo -e "\n${BLUE}Step 5: Adding enhanced deploy.sh${NC}"
if grep -q "CHECKING CONFIGURATION FILES" deploy.sh; then
    git add deploy.sh
    echo -e "${GREEN}✓ Added deploy.sh changes${NC}"
else
    echo -e "${YELLOW}Warning: deploy.sh doesn't have config checking logic${NC}"
fi

echo -e "\n${BLUE}Step 6: Creating git commit${NC}"
cat > /tmp/migration_commit_msg.txt << 'EOF'
Migrate to .example config pattern for white-label protection

- Add .example template files for themes and platform configs
- Remove actual config files from git tracking (gitignored)
- Update .gitignore to protect custom configurations
- Enhance deploy.sh to copy .example files if configs missing
- Add WHITE_LABEL_CONFIG.md documentation

This ensures white-label installations can customize configs
without git conflicts during deployments.

Config files affected:
- config/themes.php
- config/timebank-default.php
- config/timebank_cc.php

Local config files are preserved. Future git pulls will not
overwrite installation-specific customizations.
EOF

git commit -F /tmp/migration_commit_msg.txt
rm /tmp/migration_commit_msg.txt

echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN}  Migration Complete!${NC}"
echo -e "${GREEN}========================================${NC}\n"

echo -e "${YELLOW}What changed:${NC}"
echo "✓ .example files are now tracked in git (templates)"
echo "✓ Actual config files are gitignored (your customizations)"
echo "✓ deploy.sh will auto-create configs from templates if missing"
echo ""
echo -e "${YELLOW}Next steps:${NC}"
echo "1. Push these changes: git push origin main"
echo "2. On other installations, pull updates: git pull origin main"
echo "3. Their custom configs will be preserved automatically"
echo ""
echo -e "${BLUE}For more info, see: references/BRANDING_CUSTOMIZATION.md${NC}"
