#!/bin/sh
set -e

if [ $# -gt 0 ]; then
    # Execute the passed command
    exec "$@"
else
    # Install npm dependencies if needed
    if [ -f /var/www/package.json ] && [ ! -d /var/www/node_modules ]; then
        echo "Installing npm dependencies..."
        cd /var/www && npm install
    fi

    # Build assets in dev mode if needed
    if [ -f /var/www/vite.config.js ] && [ ! -d /var/www/public/build ]; then
        echo "Building frontend assets..."
        cd /var/www && npm run build 2>/dev/null || true
    fi

    # Run Laravel setup
    if [ -f /var/www/artisan ]; then
        php /var/www/artisan storage:link --force 2>/dev/null || true
    fi

    # Start services
    exec /usr/bin/supervisord -c /etc/supervisord.conf
fi
