#!/bin/bash
set -e

# Initialize if needed (generates identity + channel key + config)
if [ ! -f "$SOOSEF_DATA_DIR/config.json" ]; then
    echo "First run — initializing SooSeF..."
    soosef init
    echo "Initialization complete."
fi

# Determine HTTPS mode
HTTPS_FLAG=""
if [ "${SOOSEF_HTTPS_ENABLED:-true}" = "false" ]; then
    HTTPS_FLAG="--no-https"
fi

echo "Starting SooSeF on port ${SOOSEF_PORT:-35811}..."

# Run with gunicorn for production
exec gunicorn \
    --bind "0.0.0.0:${SOOSEF_PORT:-35811}" \
    --workers "${SOOSEF_WORKERS:-2}" \
    --timeout 180 \
    --access-logfile - \
    --error-logfile - \
    "frontends.web.app:create_app()"
