PORT ?= 5000
DEPLOY_ENV ?= loc

BIN_NAME ?= nemt-portal-api
APP_NAME ?= nemt-portal-api

BUILD_CONTAINER_NAME = ${APP_NAME}-build
RUN_CONTAINER_NAME = ${APP_NAME}-run

#############################
## Environment definition. ##
#############################

set-loc: 
	$(eval DEPLOY_ENV := loc)

set-dev: 
	$(eval DEPLOY_ENV := dev)

set-stg: 
	$(eval DEPLOY_ENV := stg)

set-prd: 
	$(eval DEPLOY_ENV := prd)

######################
## Building process ##
######################

# Creates the container for the building process.
create-build-container:

	# Verifies if the container already exists.
	$(eval CONTAINER_ID:=$(shell docker ps -q --filter ancestor=$(BUILD_CONTAINER_NAME)))
	
	# If it does, removes it.
	if [ $(CONTAINER_ID) ]; then \
		docker stop $(CONTAINER_ID) && \
		docker rm $(CONTAINER_ID) && \
		docker rmi -f $(BUILD_CONTAINER_NAME); \
	fi

	# Creates "build" container.
	docker build -f Dockerfile.build -t ${BUILD_CONTAINER_NAME}:latest --build-arg BIN_NAME=${BIN_NAME} --build-arg APP_NAME=${APP_NAME} --force-rm .

# Runs the `build` container and gets its binary.
build: clean create-build-container

	# Creates the necessary folders.
	mkdir -p dist/static
	mkdir -p dist/docs

	# Builds inside the container.
	docker run \
		--name ${BUILD_CONTAINER_NAME} -d ${BUILD_CONTAINER_NAME}
	
	# Gets the binary.
	docker cp \
		${BUILD_CONTAINER_NAME}:/go/src/bitbucket.org/nemt/${APP_NAME}/${BIN_NAME} \
		./dist/${BIN_NAME}

	# Kills the container.
	docker stop ${BUILD_CONTAINER_NAME}
	docker rm ${BUILD_CONTAINER_NAME}

	# Copies the docs and the static files to the correct folder.
	cp -R static/* ./dist/static/
	cp -R docs/swagger/ ./dist/docs/
	cp config.${DEPLOY_ENV}.toml ./dist/config.toml
	cp authorization_model.conf ./dist/authorization_model.conf
	cp authorization_policy.csv ./dist/authorization_policy.csv

#################################################
## Building, based on the current environment. ##
#################################################

build-loc: set-loc build

build-dev: set-dev build

build-stg: set-stg build

build-prd: set-prd build

#####################
## Running process ##
#####################

create-run-container:

	# Verifies if the container already exists.
	$(eval CONTAINER_ID:=$(shell docker ps -q --filter ancestor=$(RUN_CONTAINER_NAME)))
	
	# If it does, removes it.
	if [ $(CONTAINER_ID) ]; then \
		docker stop $(CONTAINER_ID) && \
		docker rm $(CONTAINER_ID) && \
		docker rmi -f $(RUN_CONTAINER_NAME); \
	fi
	
	# Creates "run" container.
	docker build -f Dockerfile.run -t ${RUN_CONTAINER_NAME}:latest --force-rm \
		--build-arg BIN_NAME=${BIN_NAME} \
		--build-arg APP_NAME=${APP_NAME} \
		.

run: create-run-container
	docker run -p ${PORT}:${PORT} -d --rm --name ${RUN_CONTAINER_NAME} ${RUN_CONTAINER_NAME}
	echo 'Container exposing port ${PORT}.'

################################################
## Running, based on the current environment. ##
################################################

build-run-loc: build-loc create-run-container

build-run-dev: build-dev create-run-container

build-run-stg: build-stg create-run-container

build-run-prd: build-prd create-run-container

run-loc: build-run-loc run

run-dev: build-run-dev run

run-stg: build-run-stg run

run-prd: build-run-prd run

############
## Others ##
############

clean:
	rm -f ${BIN_NAME}
	rm -rf dist

run-host:
	glide install
	go build -o ${BIN_NAME} .
	cp config.loc.toml ./config.toml
	./${BIN_NAME}

migrate:
	echo "Not implemented" && exit 500

test:
	echo "Not implemented" && exit 500