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

# Nginx Example Install Script


# Load service environment variables
source ./service.env

# Ensure local data folder exists
if [ ! -d "${LOCAL_DATA_FOLDER}" ]; then
    echo "Creating local data folder ${LOCAL_DATA_FOLDER}..."
    mkdir -p "${LOCAL_DATA_FOLDER}"
    chmod 777 "${LOCAL_DATA_FOLDER}"
fi

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..."
bash ./stop.sh || _die "Failed to stop container ${CONTAINER_NAME}"
_remove_container $CONTAINER_NAME || _die "Failed to remove container ${CONTAINER_NAME}"

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

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