#!/bin/sh
set -e

echo "==> Starting El Captain deployment..."

# Ensure storage directories exist
mkdir -p \
    storage/framework/cache/data \
    storage/framework/sessions \
    storage/framework/views \
    storage/logs \
    bootstrap/cache

# Fix permissions
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache

# Create storage link if not exists
if [ ! -L public/storage ]; then
    php artisan storage:link --no-interaction
fi

# Cache configuration for production
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

# Run pending migrations
php artisan migrate --force --no-interaction

# Publish Livewire assets
php artisan livewire:publish --assets 2>/dev/null || true

echo "==> El Captain ready. Starting services..."

exec "$@"
