baseimage := bugbounty-base
extimage := bugbounty_extback
intimage := bugbounty_intback
extsrc := external/src
intsrc := internal/src

build: mount
	docker-compose build

run:
	docker run -e "YENV_TYPE=development" --name bugbounty_intback -p 9002 \
	    --volume /home/remedy/bugbounty/core:/app/core \
	    --volume /home/remedy/bugbounty/external/src/app:/app/app \
	    --volume /home/remedy/bugbounty/external/src/external:/app/external \
	    --network host --detach ${intimage}

debug:
	docker run --name bugbounty_intback \
	    -e "YENV_TYPE=development" -e "UWSGI_SHARED_SOCKET=[]:9001" \
	    --network host --rm -it ${intimage} /bin/bash
#	    --volume /home/remedy/bugbounty/core:/app/core \
#	    --volume /home/remedy/bugbounty/internal/src/app:/app/app \
#	    --volume /home/remedy/bugbounty/internal/src/internal:/app/internal \

buildbase:
	docker build --tag ${baseimage} --file Dockerfile.base .

runbase:
	docker run --name bugbounty-base \
	    --rm -it ${baseimage} /bin/bash

mount:
	@test -d ${extsrc}/core || mkdir ${extsrc}/core
	mountpoint ${extsrc}/core || sudo mount --bind core ${extsrc}/core
	@test -d ${extsrc}/static || mkdir ${extsrc}/static
	mountpoint ${extsrc}/static || sudo mount --bind ../bugbounty-front/external/dest ${extsrc}/static
	@test -d ${intsrc}/core || mkdir ${intsrc}/core
	mountpoint ${intsrc}/core || sudo mount --bind core ${intsrc}/core
	@test -d ${intsrc}/static || mkdir ${intsrc}/static
	mountpoint ${intsrc}/static || sudo mount --bind ../bugbounty-front/internal/dest ${intsrc}/static


.PHONY: clean

# Remove containers in Exited state
# Remove untagged images
clean:
	docker ps -a | egrep 'Exited|Created' | perl -wnle 'm!(\S+)$$! && print $$1' | xargs --no-run-if-empty docker rm
	docker images | grep '<none>' | perl -wnle 'my @items = split; print $$items[2]' | xargs --no-run-if-empty docker rmi
