#!/bin/bash

# Production runner for backup web application using Gunicorn

APP_DIR="/home/acedanger/shell"
APP_MODULE="backup-web-app:app"
CONFIG_FILE="gunicorn.conf.py"
VENV_PATH="/home/acedanger/shell/venv"

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

print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if we're in the right directory
cd "$APP_DIR" || {
    print_error "Cannot change to app directory: $APP_DIR"
    exit 1
}

# Check for virtual environment
if [[ -d "$VENV_PATH" ]]; then
    print_status "Activating virtual environment..."
    source "$VENV_PATH/bin/activate"
fi

# Set environment variables
export BACKUP_ROOT="/mnt/share/media/backups"
export FLASK_ENV="production"

# Check if gunicorn is installed
if ! command -v gunicorn &> /dev/null; then
    print_error "Gunicorn is not installed"
    print_status "Install with: pip install gunicorn"
    exit 1
fi

print_status "Starting backup web application with Gunicorn..."
print_status "Configuration: $CONFIG_FILE"
print_status "Module: $APP_MODULE"
print_status "Directory: $APP_DIR"

# Start Gunicorn
exec gunicorn \
    --config "$CONFIG_FILE" \
    "$APP_MODULE"
