#!/bin/bash
source "${AGENT_PATH}/_common.sh"
_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG"

# Watchtower Install Script


# Load service environment variables
source ./service.env

echo "Checking Docker installation..."
_check_docker_installed || _die "Docker test failed, aborting installation..."

echo "Pulling image ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}..."
docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || _die "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG"

echo "Stopping and removing any existing container..."
# No need to check failure, might not exist
_stop_container $CONTAINER_NAME
_remove_container $CONTAINER_NAME || _die "Failed to remove container ${CONTAINER_NAME}"

echo "Starting container..."
bash ./start.sh $1 || _die "Failed to start container ${CONTAINER_NAME}"

echo "Installation complete for service ${CONTAINER_NAME}."
