#!/bin/sh

# Exit script on any error
set -e

# Ensure we're in the right directory
cd /var/www/html

# Install PHP dependencies
composer install --no-dev --optimize-autoloader

# Install Node.js dependencies if node_modules not present
if [ ! -d "node_modules" ]; then
    npm install
fi

# Build frontend assets
npm run build

# Set correct permissions for storage and cache
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/

# Run database migrations
php artisan migrate --force

# Start PHP-FPM
php-fpm
