#!/bin/bash

# SSH Key Generation Script for Gitea Deployment
# This script generates SSH keys and helps you set them up

set -e

echo "================================================"
echo "SSH Key Setup for Gitea Deployment"
echo "================================================"
echo ""

# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

SSH_KEY_PATH="$HOME/.ssh/gitea_deploy_key"

# Check if key already exists
if [ -f "$SSH_KEY_PATH" ]; then
    echo -e "${YELLOW}Warning: SSH key already exists at $SSH_KEY_PATH${NC}"
    read -p "Do you want to overwrite it? (y/n): " overwrite
    if [ "$overwrite" != "y" ]; then
        echo "Using existing key."
    else
        rm -f "$SSH_KEY_PATH" "$SSH_KEY_PATH.pub"
        echo "Generating new SSH key..."
        ssh-keygen -t ed25519 -C "gitea-deploy-key" -f "$SSH_KEY_PATH" -N ""
    fi
else
    echo "Generating new SSH key..."
    ssh-keygen -t ed25519 -C "gitea-deploy-key" -f "$SSH_KEY_PATH" -N ""
fi

echo -e "\n${GREEN}✓ SSH key generated successfully!${NC}\n"

# Display private key for Gitea
echo -e "${YELLOW}=== PRIVATE KEY (for Gitea Secrets) ===${NC}"
echo -e "${BLUE}Copy this ENTIRE content for your Gitea secret:${NC}\n"
cat "$SSH_KEY_PATH"
echo ""

# Display public key for server
echo -e "\n${YELLOW}=== PUBLIC KEY (for Server) ===${NC}"
echo -e "${BLUE}Copy this content to add to your server's ~/.ssh/authorized_keys:${NC}\n"
cat "$SSH_KEY_PATH.pub"
echo ""

# Ask if user wants to deploy to server now
echo -e "\n${YELLOW}=== Deploy Public Key to Server ===${NC}"
read -p "Do you want to add the public key to a server now? (y/n): " deploy_now

if [ "$deploy_now" = "y" ]; then
    read -p "Enter SSH username: " ssh_user
    read -p "Enter server IP/hostname: " ssh_host
    
    echo -e "\nAdding public key to $ssh_user@$ssh_host..."
    
    # Copy public key to server
    ssh-copy-id -i "$SSH_KEY_PATH.pub" "$ssh_user@$ssh_host" 2>/dev/null || \
    ssh "$ssh_user@$ssh_host" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" < "$SSH_KEY_PATH.pub"
    
    echo -e "\n${GREEN}✓ Public key added to server!${NC}"
    
    # Test connection
    echo -e "\nTesting SSH connection..."
    if ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no "$ssh_user@$ssh_host" "echo 'Connection successful!'" 2>/dev/null; then
        echo -e "${GREEN}✓ SSH connection test successful!${NC}"
    else
        echo -e "${YELLOW}⚠ SSH connection test failed. Please check your server configuration.${NC}"
    fi
fi

# Summary
echo -e "\n${GREEN}=== Setup Complete! ===${NC}"
echo -e "\n${YELLOW}Next Steps:${NC}"
echo "1. Copy the PRIVATE KEY above and add it to Gitea Secrets as:"
echo "   • DEPLOY_SSH_KEY (for dev)"
echo "   • PROD_DEPLOY_SSH_KEY (for production)"
echo ""
echo "2. If you didn't deploy the public key yet, manually add it to your server:"
echo "   ssh user@server"
echo "   echo '$(cat "$SSH_KEY_PATH.pub")' >> ~/.ssh/authorized_keys"
echo ""
echo "3. The key files are saved at:"
echo "   Private: $SSH_KEY_PATH"
echo "   Public: $SSH_KEY_PATH.pub"
echo ""
