include .env

DOCKERCOMPOSE := $(shell command -v docker-compose 2> /dev/null)
USAGE := Usage: make ENV={prod|preprod|dev} {docker-build|docker-start|docker-stop|docker-restart}

ifndef DOCKERCOMPOSE
$(error docker-compose is not available)
endif

ifndef ENV
$(info Environment variable is not set, using default (dev))
$(info $(USAGE))
# $(error Exiting)
endif

DC_OVERRIDE := $(shell test -e docker-compose.override.yml && echo "--file docker-compose.override.yml")
DC_OVERRIDE_ENV := $(shell test -e docker-compose.$(ENV).yml && echo "--file docker-compose.$(ENV).yml")

DC=ENV=$(ENV) PROJECT=$(PROJECT) $(DOCKERCOMPOSE) --project-name "$(PROJECT)_$(ENV)" --file docker-compose.yml $(DC_OVERRIDE) $(DC_OVERRIDE_ENV)

CC=$(CMS)

default:
	@echo "projet: " ${PROJECT}
	@echo "No command given"
	@echo "Usage: make {docker-build|docker-start|docker-stop|docker-restart}"
	@false

docker-build:
	$(shell conf/scripts/create-cert.sh)
	$(DC) build
	$(DC) up -d
	@cp volume/app/www/html/info.php volume/www/html/index.php
	@open https://${DOMAIN}

docker-start:
	$(DC) start

docker-stop:
	$(DC) stop

docker-down:
	$(DC) down

docker-restart: docker-stop docker-start

docker-rm: docker-stop
	$(DC) rm

init-ssl:
	$(shell conf/scripts/create-cert.sh)

db-backup:
	@echo "Backing up database"
	@mkdir -p backup/
	@docker exec --tty --interactive $(PROJECT).mariadb.$(ENV)  mariadb-dump -uuser -ppassword database > backup/db_`date +%d-%m-%Y"_"%H_%M_%S`.sql

db-restore:
	@echo "Restoring file to database (backup/db.sql)"
	@docker exec -i $(PROJECT).mariadb.$(ENV) mmariadb -uuser -ppassword < backup/db.sql

config-db-root:
	@echo "  --> Generating mariadb auth file"
	@cat conf/env/mariadb.env \
		| python3 -c "import sys; import string; v=sys.stdin.readline().split('MYSQL_ROOT_PASSWORD=')[1]; print(string.Template(open('conf/build/mariadb/my.cnf').read()).substitute({'PASSWORD':v}))" \
		| docker exec -i $(PROJECT).mariadb.$(ENV) sh -c 'cat > /root/.my.cnf'

get-cms:
ifeq ($(CMS),prestashop)
# turn the detached message off
	@git config --global advice.detachedHead false
	@echo "cms ok: " ${CMS}
	rm -f -r volume/www/html/*
	@curl -L https://github.com/PrestaShop/PrestaShop/releases/download/$(CMSVERSION)/prestashop_$(CMSVERSION).zip --output volume/www/html/prestashop.zip
	@unzip -o volume/www/html/prestashop.zip -d volume/www/html
	@unzip -o volume/www/html/prestashop.zip -d volume/www/html
	rm volume/www/html/Install_PrestaShop.html
	rm volume/www/html/prestashop.zip
	@mv volume/www/html/admin volume/www/html/administrator
	@mkdir -p backup/
	@echo "Saving database"
	@docker exec --tty --interactive $(PROJECT).mariadb.$(ENV)  mysqldump -uuser -ppassword --all-databases > backup/db_`date +%d-%m-%Y"_"%H_%M_%S`.sql
	@echo "Creating database"
	-@docker exec --tty --interactive $(PROJECT).mariadb.$(ENV) mysqladmin -uroot -ppassword -f create or replace database 'database'
	@mkdir -p ../
	@git clone https://github.com/Oksydan/falcon.git
	@mv falcon volume/www/html/themes/falcon
	@php volume/www/html/install/index_cli.php --domain=myshop.local  \
	--db_server=127.0.0.1 \
	--db_name=database \
	--db_user=user \
	--db_password=password \
	--db_create=1 \
	--name=${PROJECT} \
	--email=${EMAIL} \
	--password=${PASSWORD} \
	--prefix=${PREFIX} \
	--language=fr \
	--ssl=1

	@curl -L https://github.com/Oksydan/is_themecore/releases/download/v4.1.1/is_themecore.zip --output volume/www/html/modules/is_themecore.zip
	@unzip -o volume/www/html/modules/is_themecore.zip -d volume/www/html/modules
	@curl -L https://github.com/Oksydan/is_searchbar/releases/download/v3.0.1/is_searchbar.zip --output volume/www/html/modules/is_searchbar.zip
	@unzip -o volume/www/html/modules/is_searchbar.zip -d volume/www/html/modules
	@curl -L https://github.com/Oksydan/is_imageslider/releases/download/v2.3.2/is_imageslider.zip --output volume/www/html/modules/is_imageslider.zip
	@unzip -o volume/www/html/modules/is_imageslider.zip -d volume/www/html/modules
	@curl -L https://github.com/Oksydan/is_shoppingcart/releases/download/v3.0.1/is_shoppingcart.zip --output volume/www/html/modules/is_shoppingcart.zip
	@unzip -o volume/www/html/modules/is_shoppingcart.zip -d volume/www/html/modules

	@cp volume/app/prestashop/falconchild.zip volume/www/html/themes/
	@unzip -o volume/www/html/themes/falconchild.zip -d volume/www/html/themes/
	rm volume/www/html/themes/falconchild.zip

	@mv volume/www/html/install volume/www/html/installator
	@cp volume/app/www/html/info.php volume/www/html/info.php
	@cp volume/app/prestashop/parameters.php volume/www/html/app/config/parameters.php
	@cp volume/app/prestashop/defines.inc.php volume/www/html/config/defines.inc.php
	@open https://${DOMAIN}/administrator
else
	@echo "Nothing to CMS"
endif

ifeq ($(CMS),wordpress)
	@rm -f -r volume/www/*
	@composer create-project roots/bedrock
	@mv bedrock volume/www/html
	@cp volume/app/bedrock/.env volume/www/html/.env
	@cp volume/app/bedrock/picostrap5.zip volume/www/html/web/app/themes/picostrap5.zip
	@unzip -o volume/www/html/web/app/themes/picostrap5.zip -d volume/www/html/web/app/themes
	rm volume/www/html/web/app/themes/picostrap5.zip
	@cp volume/app/bedrock/picostrap5-child.zip volume/www/html/web/app/themes/picostrap5-child.zip
	@unzip -o volume/www/html/web/app/themes/picostrap5-child.zip -d volume/www/html/web/app/themes
	rm volume/www/html/web/app/themes/picostrap5-child.zip
	@cp volume/app/bedrock/plugins.zip volume/www/html/web/app/themes/plugins.zip
	@unzip -o volume/www/html/web/app/themes/plugins.zip -d volume/www/html/web/app/plugins
	rm volume/www/html/web/app/themes/plugins.zip
else
	@echo "Nothing to CMS"
endif
	
get-all-plugins:
	@git clone https://github.com/ldesmarets/myWpPlugin.git wp-plugins
	@unzip -o "wp-plugins/*.zip" -d volume/bedrock/web/app/plugins/
	rm -f -r wp-plugins