#!/bin/bash
set -e
set -o pipefail

echo "progetto"  $1
REPO_NAME=$1
COMMIT_SHA=$(git rev-parse HEAD)
REGISTRY_URL=$2

# Controlla se la directory containers esiste
if [ ! -d "containers" ]; then
  echo "⚠️  Directory 'containers' non trovata, niente da fare"
  exit 0
fi

for dir in containers/*/; do
  CONTAINER_NAME=$(basename "$dir")
  if [ -d "src/${CONTAINER_NAME}" ]; then
    cp -R "src/${CONTAINER_NAME}/." "containers/${CONTAINER_NAME}/."
  else
    echo "Directory src/${CONTAINER_NAME} non trovata, skip copia sorgenti"
  fi
  ls -la $dir

  # Cerca sia dockerfile che Dockerfile
  if [ -f "$dir/dockerfile" ]; then
    DOCKERFILE="$dir/dockerfile"
  elif [ -f "$dir/Dockerfile" ]; then
    DOCKERFILE="$dir/Dockerfile"
  else
    echo "Dockerfile non trovato in $dir"
    continue
  fi



  IMAGE_TAG="${REGISTRY_URL}/${REPO_NAME}/${CONTAINER_NAME}:${COMMIT_SHA}"
  IMAGE_TAG_LATEST="${REGISTRY_URL}/${REPO_NAME}/${CONTAINER_NAME}:latest"
  echo "IMAGE_TAG_${CONTAINER_NAME}=$IMAGE_TAG" >> ./imglist
 
done