#!/bin/bash

# Print commands
# set -x

# Exit on first command failed
set -e

DIR="$(cd "$(dirname "$0")" && pwd)"
AWSLOCAL="${DIR}/../env/bin/awslocal"

SERVICE_NAME="stmtdb-to-sf"

TASKS=(
    "extract-sales"
)

for task in "${TASKS[@]}"; do
    task_name="${SERVICE_NAME}-${task}"
    task_path=${task//-/_} # Replace dashes with underscores

    echo "Creating ECR repository for ${task}"
    
   repository_uri=$($AWSLOCAL ecr create-repository \
        --repository-name $task_name \
        --image-scanning-configuration scanOnPush=true \
        | jq --raw-output ".repository.repositoryUri")
    
    echo "Created ECR repository: ${repository_uri}"

    echo "Pushing docker image for ${task}"

    docker build $DIR/../ecs/tasks/$task_path --tag $task_name
    docker tag $task_name $repository_uri
    docker push $repository_uri

    echo "Docker image pushed"
done
