#!/bin/bash

# Validate arguments
if [[ -z "$1" ]]; then
    echo "First argument should be the RECO_TYPE (logo/med). Exiting..."
    exit 1
fi

if [[ -z "$2" ]]; then
    echo "Second argument should be the MIGRATION_NUMBER (e.g., 013). Exiting..."
    exit 1
fi

RECO_TYPE_CLOUD="reco-${1}"
MIGRATION_NUMBER=$2

# Remove leading zeros to convert, e.g., "013" to "13"
MIGRATION_VERSION=$(echo $MIGRATION_NUMBER | sed 's/^0*//')

echo "Running migration $MIGRATION_NUMBER (version $MIGRATION_VERSION) for $RECO_TYPE_CLOUD..."

# Run the specific migration through CLI and output all details including errors
docker exec ${RECO_TYPE_CLOUD}-php1 bash -c "cd /var/www/html/recomedcloud && php index.php partner/cli_migrate/version/$MIGRATION_VERSION"

# Print status
echo "Migration attempt completed with exit status: $?"