#!/usr/bin/env sh

set -eu

APP_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"

cd "$APP_DIR"

echo "Updating Laravel app in: $APP_DIR"

if [ ! -f artisan ]; then
  echo "artisan not found. Run this script from the project root."
  exit 1
fi

if [ ! -f .env ]; then
  echo "Warning: .env not found. Make sure production environment variables are already configured."
fi

php artisan down || true

php artisan migrate --force
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan cache:clear
php artisan optimize:clear
php artisan storage:link || true

if [ -d storage ]; then
  chmod -R ug+rwX storage bootstrap/cache || true
fi

php artisan up || true

echo "Update completed."
