#!/bin/bash
set -e

echo "Starting System Tool Service..."

# Wait for dependencies
if [ -n "$DB_HOST" ]; then
    echo "Waiting for MySQL at $DB_HOST:$DB_PORT..."
    until nc -z "$DB_HOST" "${DB_PORT:-3306}"; do
        sleep 2
    done
    echo "MySQL is ready!"
fi

if [ -n "$REDIS_HOST" ]; then
    echo "Waiting for Redis at $REDIS_HOST:$REDIS_PORT..."
    until nc -z "$REDIS_HOST" "${REDIS_PORT:-6379}"; do
        sleep 2
    done
    echo "Redis is ready!"
fi

# Run database migrations if needed
# java -jar app.jar flyway:migrate || true

# Start the application
exec java $JAVA_OPTS -jar app.jar
