#!/usr/bin/env bash set -Eeuo pipefail # ===================================================== # BotKonzept Setup Script # ===================================================== # This script sets up the complete BotKonzept platform # including database, n8n workflows, and website SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration SUPABASE_HOST="${SUPABASE_HOST:-192.168.45.3}" SUPABASE_PORT="${SUPABASE_PORT:-5432}" SUPABASE_DB="${SUPABASE_DB:-customer}" SUPABASE_USER="${SUPABASE_USER:-customer}" SUPABASE_PASSWORD="${SUPABASE_PASSWORD:-}" N8N_HOST="${N8N_HOST:-n8n.userman.de}" WEBSITE_DIR="${WEBSITE_DIR:-/var/www/botkonzept}" print_header() { echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}$1${NC}" echo -e "${BLUE}========================================${NC}" echo "" } print_success() { echo -e "${GREEN}✓${NC} $1" } print_error() { echo -e "${RED}✗${NC} $1" } print_info() { echo -e "${BLUE}ℹ${NC} $1" } print_warning() { echo -e "${YELLOW}⚠${NC} $1" } # Check prerequisites check_prerequisites() { print_header "Checking Prerequisites" local missing=0 # Check psql if command -v psql >/dev/null 2>&1; then print_success "PostgreSQL client installed" else print_error "PostgreSQL client not found" missing=1 fi # Check curl if command -v curl >/dev/null 2>&1; then print_success "curl installed" else print_error "curl not found" missing=1 fi # Check jq if command -v jq >/dev/null 2>&1; then print_success "jq installed" else print_warning "jq not found (optional, but recommended)" fi if [[ $missing -eq 1 ]]; then print_error "Missing required dependencies. Please install them first." exit 1 fi echo "" } # Setup database setup_database() { print_header "Setting up Database" if [[ -z "$SUPABASE_PASSWORD" ]]; then print_error "SUPABASE_PASSWORD not set" echo "Please set the password:" echo " export SUPABASE_PASSWORD='your-password'" exit 1 fi print_info "Connecting to PostgreSQL at ${SUPABASE_HOST}:${SUPABASE_PORT}" # Test connection if PGPASSWORD="$SUPABASE_PASSWORD" psql -h "$SUPABASE_HOST" -p "$SUPABASE_PORT" -U "$SUPABASE_USER" -d "$SUPABASE_DB" -c "SELECT 1" >/dev/null 2>&1; then print_success "Database connection successful" else print_error "Cannot connect to database" exit 1 fi # Create schema print_info "Creating BotKonzept schema..." if PGPASSWORD="$SUPABASE_PASSWORD" psql -h "$SUPABASE_HOST" -p "$SUPABASE_PORT" -U "$SUPABASE_USER" -d "$SUPABASE_DB" -f "${SCRIPT_DIR}/sql/botkonzept_schema.sql" >/dev/null 2>&1; then print_success "Schema created successfully" else print_error "Failed to create schema" exit 1 fi # Verify tables print_info "Verifying tables..." local tables=$(PGPASSWORD="$SUPABASE_PASSWORD" psql -h "$SUPABASE_HOST" -p "$SUPABASE_PORT" -U "$SUPABASE_USER" -d "$SUPABASE_DB" -tAc "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='public' AND table_name IN ('customers', 'instances', 'subscriptions', 'payments', 'emails_sent')") if [[ "$tables" -eq 5 ]]; then print_success "All tables created successfully" else print_warning "Expected 5 tables, found $tables" fi echo "" } # Setup n8n workflows setup_n8n_workflows() { print_header "Setting up n8n Workflows" print_info "n8n workflows need to be imported manually:" echo "" echo "1. Open n8n: https://${N8N_HOST}" echo "2. Go to Workflows → Import from File" echo "3. Import these files:" echo " - ${SCRIPT_DIR}/BotKonzept-Customer-Registration-Workflow.json" echo " - ${SCRIPT_DIR}/BotKonzept-Trial-Management-Workflow.json" echo "" echo "4. Configure credentials:" echo " - SSH (PVE20): Private key for Proxmox" echo " - PostgreSQL (Supabase): Host=${SUPABASE_HOST}, DB=${SUPABASE_DB}" echo " - SMTP (Postfix/SES): Your email server" echo "" read -p "Press Enter when workflows are imported and configured..." print_success "n8n workflows configured" echo "" } # Setup website setup_website() { print_header "Setting up Website" if [[ ! -d "${SCRIPT_DIR}/botkonzept-website" ]]; then print_error "Website directory not found: ${SCRIPT_DIR}/botkonzept-website" exit 1 fi print_info "Website files location: ${SCRIPT_DIR}/botkonzept-website" print_info "Target directory: ${WEBSITE_DIR}" echo "" # Update webhook URL in JavaScript print_info "Updating webhook URL in JavaScript..." local webhook_url="https://${N8N_HOST}/webhook/botkonzept-registration" if [[ -f "${SCRIPT_DIR}/botkonzept-website/js/main.js" ]]; then sed -i.bak "s|WEBHOOK_URL:.*|WEBHOOK_URL: '${webhook_url}',|" "${SCRIPT_DIR}/botkonzept-website/js/main.js" print_success "Webhook URL updated to: ${webhook_url}" fi echo "" print_info "To deploy the website, run:" echo " sudo mkdir -p ${WEBSITE_DIR}" echo " sudo cp -r ${SCRIPT_DIR}/botkonzept-website/* ${WEBSITE_DIR}/" echo " sudo chown -R www-data:www-data ${WEBSITE_DIR}" echo "" read -p "Deploy website now? (y/N): " deploy if [[ "$deploy" =~ ^[Yy]$ ]]; then sudo mkdir -p "${WEBSITE_DIR}" sudo cp -r "${SCRIPT_DIR}/botkonzept-website/"* "${WEBSITE_DIR}/" sudo chown -R www-data:www-data "${WEBSITE_DIR}" print_success "Website deployed to ${WEBSITE_DIR}" else print_info "Skipping website deployment" fi echo "" } # Test setup test_setup() { print_header "Testing Setup" # Test database print_info "Testing database connection..." if PGPASSWORD="$SUPABASE_PASSWORD" psql -h "$SUPABASE_HOST" -p "$SUPABASE_PORT" -U "$SUPABASE_USER" -d "$SUPABASE_DB" -c "SELECT COUNT(*) FROM customers" >/dev/null 2>&1; then print_success "Database accessible" else print_error "Database not accessible" fi # Test n8n webhook print_info "Testing n8n webhook..." local webhook_url="https://${N8N_HOST}/webhook/botkonzept-registration" local response=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$webhook_url" \ -H "Content-Type: application/json" \ -d '{"test": true}' 2>/dev/null || echo "000") if [[ "$response" == "200" ]] || [[ "$response" == "400" ]]; then print_success "n8n webhook accessible (HTTP $response)" else print_warning "n8n webhook returned HTTP $response" fi # Test website if [[ -d "$WEBSITE_DIR" ]]; then print_success "Website directory exists" else print_warning "Website not deployed yet" fi echo "" } # Generate summary generate_summary() { print_header "Setup Summary" echo -e "${GREEN}✓ BotKonzept setup completed!${NC}" echo "" echo "Next steps:" echo "" echo "1. Configure DNS:" echo " - Point botkonzept.de to your web server" echo " - Configure SSL certificate (Let's Encrypt)" echo "" echo "2. Test registration:" echo " - Visit https://botkonzept.de" echo " - Fill out registration form" echo " - Check email for welcome message" echo "" echo "3. Monitor workflows:" echo " - n8n: https://${N8N_HOST}" echo " - Check executions for errors" echo "" echo "4. Database access:" echo " - Host: ${SUPABASE_HOST}" echo " - Database: ${SUPABASE_DB}" echo " - User: ${SUPABASE_USER}" echo "" echo "5. Useful queries:" echo " - View customers: SELECT * FROM customer_overview;" echo " - View trials: SELECT * FROM trials_expiring_soon;" echo " - View emails: SELECT * FROM emails_sent ORDER BY sent_at DESC LIMIT 10;" echo "" echo "Documentation: ${SCRIPT_DIR}/BOTKONZEPT_README.md" echo "" } # Main execution main() { clear print_header "BotKonzept Setup" echo "This script will set up the complete BotKonzept platform." echo "" echo "Components:" echo " - PostgreSQL database schema" echo " - n8n workflows (manual import)" echo " - Website files" echo "" read -p "Continue? (y/N): " confirm if [[ ! "$confirm" =~ ^[Yy]$ ]]; then echo "Setup cancelled." exit 0 fi echo "" check_prerequisites setup_database setup_n8n_workflows setup_website test_setup generate_summary } # Run main function main "$@"