#!/bin/sh set -e echo "🚀 Starting TT Booking Production..." # Create required directories if they don't exist echo "📁 Ensuring directories exist..." mkdir -p /app/data /app/backups /app/logs chmod 755 /app/data /app/backups /app/logs # Set defaults for environment variables export DATABASE_URL="${DATABASE_URL:-/app/data/sqlite.db}" export NODE_ENV="${NODE_ENV:-production}" export ADMIN_EMAIL="${ADMIN_EMAIL:-admin@tabletennis.com}" export ADMIN_PASSWORD="${ADMIN_PASSWORD:-admin123}" # Validate required environment variables if [ -z "$NEXTAUTH_SECRET" ]; then echo "❌ NEXTAUTH_SECRET is required but not set" echo "💡 Generate one with: openssl rand -base64 32" exit 1 fi if [ -z "$NEXTAUTH_URL" ]; then echo "❌ NEXTAUTH_URL is required but not set" echo "💡 Set to your application URL, e.g., https://your-domain.com" exit 1 fi # Check database state and determine what actions are needed echo "📊 Analyzing database state..." DB_CHECK_EXIT=0 node dist/check-database.js || DB_CHECK_EXIT=$? case $DB_CHECK_EXIT in 0) echo "✅ Database is ready - no action needed" ;; 1) echo "🔧 Database needs migration..." npm run db:push echo "✅ Migration completed" ;; 2) echo "🌱 Database needs seeding..." echo " Admin Email: $ADMIN_EMAIL" echo " Admin Password: [HIDDEN]" node dist/setup-database.js --essential-only echo "✅ Seeding completed" echo "💡 You can now login with: $ADMIN_EMAIL / $ADMIN_PASSWORD" ;; 3) echo "🔄 Database needs migration and seeding..." npm run db:push echo "✅ Migration completed" echo "🌱 Seeding database..." echo " Admin Email: $ADMIN_EMAIL" echo " Admin Password: [HIDDEN]" node dist/setup-database.js --essential-only echo "✅ Database initialization completed" echo "💡 You can now login with: $ADMIN_EMAIL / $ADMIN_PASSWORD" ;; 4) echo "❌ Database state check failed - see logs above" exit 1 ;; *) echo "❌ Unexpected database check result: $DB_CHECK_EXIT" exit 1 ;; esac echo "🌟 Starting server..." # Execute the main command exec "$@"