FRONT_PORT := 3001
FRONT_LOG := ../../front.log #relative to final/front
BACK_PORT := 8080
BACK_LOG := ../../back.log #relative to final/back
DB_PORT := 27017
MONGOD_PATH := mongod/bin/mongod #relative to final/db
DB_PATH := data/ #relative to final/db
DB_LOG := ../../db.log #relative to final/db
IO_PORT := 3000
IO_LOG := ../../io.log #relative to final/io

DOCKER_DB := $(shell pwd)/final/db/data #realtive to project root

DOCKER := sudo docker
DOCKER_BUILD := ${DOCKER} build -f dockerfile.prod -t capstone:prod .
DOCKER_RUN := ${DOCKER} run --detach=true -p ${IO_PORT}:3000 -p ${FRONT_PORT}:3001 -p ${BACK_PORT}:8080 -p ${DB_PORT}:27017 -v "${DOCKER_DB}":/app/trafficdb capstone:prod

docker: build run attach
dockerTest: build run attach cleanImage
dockerShell: build shell cleanImage
local: back db io front
cleanLocal: stopBack stopDb stopFront

################################################################################################
# Docker building
build:
	@echo "Building capstone:prod"
	@${DOCKER_BUILD}
	@echo "Successfully built capstone:prod"

run: 
	@echo -n "Running container: "
	@${DOCKER_RUN}
	@echo "Extended to: http://localhost:${FRONT_PORT}" | grep --color=always -E 'https?://[^ ]+'

attach:
	@LAST_ID=$$(${DOCKER} ps -q --latest); \
	if [ -z $$LAST_ID ]; then \
		echo "No container to attach to"; \
	else \
		echo "Attaching to $$LAST_ID"; \
		${DOCKER} exec -it $$LAST_ID /bin/bash; \
		echo "Detached from $$LAST_ID"; \
	fi

shell:
	@${DOCKER} run -v /dev:/dev -it -p ${IO_PORT}:3000 -p ${FRONT_PORT}:3001 -p ${BACK_PORT}:8080 --entrypoint /bin/bash capstone:prod

stop: 
	@LAST_ID=$$( ${DOCKER} ps -q --latest); \
	if [ -z $$LAST_ID ]; then \
		echo "No container to stop"; \
	else \
		echo -n "Stopping: $$LAST_ID"; \
		${DOCKER} stop $$LAST_ID; \
		echo "Container stopped"; \
	fi

clean: stop
	@LAST_ID=$$( ${DOCKER} ps -q --latest); \
	if [ -z $$LAST_ID ]; then \
		echo "No container to remove"; \
	else \
		echo -n "Removing latest container: "; \
		${DOCKER} rm $$LAST_ID; \
		echo "Done cleaning"; \
	fi

cleanDoc: stop
	@${DOCKER} system prune

cleanImage: clean
	@${DOCKER} rmi capstone:prod

################################################################################################
# Local building
buildLocal:
	@(cd final/front/ && npm i); (cd final/back && npm i); (cd final/io && npm i)

front:
	@cd ./final/front && PORT=${FRONT_PORT} npm start 2>&1 | tee ${FRONT_LOG} & echo $$! > .front.pid

back: 
	@cd ./final/back/ && node app.js 2>&1 | tee ${BACK_LOG} & echo $$! > .back.pid

db: 
	@cd ./final/db && ${MONGOD_PATH} --dbpath ${DB_PATH} --port ${DB_PORT} 2>&1 | tee ${DB_LOG} & echo $$! > .db.pid

io:
	@cd ./final/io && \
	npm run build && \
	(PORT=${IO_PORT} npm run watch & PORT=${IO_PORT} npm run server:dev) 2>&1 | tee ${IO_LOG} &

stopFront:
	@if [ -f .front.pid ]; then \
		echo "Stopping front-end process..."; \
		fuser -k ${FRONT_PORT}/tcp; \
		fuser -k ${IO_PORT}/tcp; \
		kill `cat .front.pid` || true; \
		rm .front.pid; \
	fi

stopBack:
	@if [ -f .back.pid ]; then \
		echo "Stopping back-end process..."; \
		fuser -k ${BACK_PORT}/tcp; \
        kill `cat .back.pid` || true; \
        rm .back.pid; \
    fi

stopDb:
	@if [ -f .db.pid ]; then \
        echo "Stopping database process..."; \
		fuser -k ${DB_PORT}/tcp; \
        kill `cat .db.pid` || true; \
        rm .db.pid; \
    fi
