#!/usr/bin/env bash
set -euo pipefail

BASE_PATH="/var/www/html/infopetani"

# Check if directory.txt exists
if [ ! -f directory.txt ]; then
  echo "❌ directory.txt not found!"
  exit 1
fi

while IFS= read -r dirName; do
  # skip empty lines
  [[ -z "$dirName" ]] && continue

  targetDir="$BASE_PATH/$dirName"

  if [ ! -d "$targetDir" ]; then
    echo "❌ Directory not found: $targetDir"
    continue
  fi

  echo "📂 Entering $targetDir"
  cd "$targetDir"

  echo "📦 Running composer install..."
  if composer install --no-interaction --prefer-dist --optimize-autoloader; then
    echo "✅ composer install success in $dirName"
  else
    echo "❌ composer install failed in $dirName"
    continue
  fi

  if [ -f ".env.example" ] && [ ! -f ".env" ]; then
    cp .env.example .env
    echo "✅ Copied .env.example to .env in $dirName"
  fi

  echo "---------------------------------------------"
done < directory.txt

