#!/usr/bin/env bash
set -e

cd /var/www/html

mkdir -p var/cache var/log var/sessions public/uploads tmp
chown -R www-data:www-data var public/uploads tmp 2>/dev/null || true

needs_composer_install=0

if [ ! -f vendor/autoload.php ]; then
    needs_composer_install=1
fi

if [ "${APP_ENV:-dev}" != "prod" ] && [ ! -d vendor/symfony/web-profiler-bundle ]; then
    needs_composer_install=1
fi

if [ "$needs_composer_install" = "1" ]; then
    if [ "${APP_ENV:-dev}" = "prod" ]; then
        composer install --no-dev --prefer-dist --no-interaction --no-scripts --optimize-autoloader
    else
        composer install --prefer-dist --no-interaction --no-scripts
    fi
fi

php -d memory_limit=-1 bin/console cache:clear --env="${APP_ENV:-dev}" --no-warmup || true

exec "$@"
