# Makefile

# --- Variables ---
IMAGE_NAME      := ilp-coursework
IMAGE_TAG       := 0.2
FULL_IMAGE_NAME := ${IMAGE_NAME}:${IMAGE_TAG}
CONTAINER_NAME  := ilp-coursework-app
SUBMISSION_FILE := ilp_submission_image.tar


# --- Targets ---

# Default command
.PHONY: all
all: save

.PHONY: build
build:
	./gradlew build

.PHONY: docker-build
docker-build: build
	docker build -t ${FULL_IMAGE_NAME} .

# Run the container in detached mode.
# It will first stop and remove any existing container with the same name.
.PHONY: run
run: docker-build
	@echo "Stopping and removing old container if it exists..."
# 	-docker stop ${CONTAINER_NAME} > /dev/null 2>&1 || true
# 	-docker rm ${CONTAINER_NAME} > /dev/null 2>&1 || true
	docker-compose stop
	@echo "Starting new container '${CONTAINER_NAME}' on http://localhost:8080"
	docker-compose up -d

.PHONY: stop
stop:
	@echo "Stopping and removing container: ${CONTAINER_NAME}"
# 	-docker stop ${CONTAINER_NAME} > /dev/null 2>&1 || true
# 	-docker rm ${CONTAINER_NAME} > /dev/null 2>&1 || true
	docker-compose stop > /dev/null 2>&1 || true


# --- Submission Target ---

.PHONY: save
save: docker-build
	docker save -o ${SUBMISSION_FILE} ${FULL_IMAGE_NAME}
	@echo "Submission file '${SUBMISSION_FILE}' created successfully."


# --- Utility Target ---

.PHONY: clean
clean:
	./gradlew clean
	-rm -f ${SUBMISSION_FILE}
