#!/bin/bash
source "$(dirname "$0")/_common.sh"

SERVICE_CONFIG_DIR="$1"

# Required environment variables
check_required_env_vars "CONTAINER_NAME" "INTERVAL"

DOCKER_RUN_CMD="docker run -d \
                --restart unless-stopped \
                --name ${CONTAINER_NAME} \
                -v /var/run/docker.sock:/var/run/docker.sock \
                -v ${SERVICE_CONFIG_DIR}/config.json:/config.json
                ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG} \
                --interval ${INTERVAL}"

if ! create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then
    echo "RUN_CMD failed:"
    echo "$DOCKER_RUN_CMD"
    die "Failed to start container ${CONTAINER_NAME}"
fi

# Check if the container is running
if ! _is_container_running "$CONTAINER_NAME"; then
    die "Container ${CONTAINER_NAME} is not running"
fi

echo "Container ${CONTAINER_NAME} started"
