DOCKER_HOST=playground.pi
SITE_URL=iaro.sinewell.de
PROJECT_NAME=client-iaro
CONTAINER_NAME_WORDPRESS=${PROJECT_NAME}_wordpress
CONTAINER_NAME_DB=${PROJECT_NAME}_db
TEMPLATE_NAME=iaro

docker_deploy:

	docker run --rm -it \
	  -v /tmp/backup:/backup -v /var/lib/docker:/docker \
		-v $(shell pwd):/docker/volumes/${CONTAINER_NAME_WORDPRESS}/_data/wp-content/themes/${TEMPLATE_NAME} \
		alpine:edge tar cpfz /backup/volumes.tgz /docker/volumes/${CONTAINER_NAME_WORDPRESS} /docker/volumes/${CONTAINER_NAME_DB}
	rsync --progress /tmp/backup/volumes.tgz ${DOCKER_HOST}:/tmp/

	-DOCKER_HOST="ssh://${DOCKER_HOST}" docker stop ${CONTAINER_NAME_WORDPRESS}_1 ${CONTAINER_NAME_DB}_1
	-DOCKER_HOST="ssh://${DOCKER_HOST}" docker rm ${CONTAINER_NAME_WORDPRESS}_1 ${CONTAINER_NAME_DB}_1
	-DOCKER_HOST="ssh://${DOCKER_HOST}" docker volume rm ${CONTAINER_NAME_WORDPRESS} ${CONTAINER_NAME_DB}

	DOCKER_HOST="ssh://${DOCKER_HOST}" docker run --rm -it \
	  -v /var/lib/docker:/docker \
		-v /tmp:/volume-backup \
		alpine:edge tar --strip-components=2 -xpvf /volume-backup/volumes.tgz -C /docker/volumes/
	DOCKER_HOST="ssh://${DOCKER_HOST}" docker run --rm -it \
	  -v /var/lib/docker:/docker \
		-v /tmp:/volume-backup \
		alpine:edge sed -i 81idefine\\\(\\\'WP_SITEURL\\\',\\\'https://${SITE_URL}\\\'\\\)\\\;\\ndefine\\\(\\\'WP_HOME\\\',\\\'https://${SITE_URL}\\\'\\\)\\\; /docker/volumes/${CONTAINER_NAME_WORDPRESS}/_data/wp-config.php

	# # Need to fix MaxSessions in remote ssh daemon https://github.com/docker/compose/issues/6463

	cp wordpress.yml /tmp/wordpress.yml
	sed -e '/${TEMPLATE_NAME}/ s/^#*/#/' -i /tmp/wordpress.yml
	DOCKER_HOST="ssh://${DOCKER_HOST}" docker-compose -p ${PROJECT_NAME} -f /tmp/wordpress.yml up --no-start
	DOCKER_HOST="ssh://${DOCKER_HOST}" docker-compose -p ${PROJECT_NAME} -f /tmp/wordpress.yml start

	sleep 20
	hyperpotamus -v setup-jetpack.yml

docker_up:
	docker-compose -f wordpress.yml up

generate_pot:
	wp i18n make-pot kit kit/languages/kit.pot # generate source file
	for i18n in "de_DE" "fr_FR"; do \
	   # update translated language file \
		msgmerge -N kit/languages/$${i18n}.po kit/languages/kit.pot > kit/languages/$${i18n}.po.new ; \
		mv kit/languages/$${i18n}.po.new kit/languages/$${i18n}.po ;\
		# generate mo file \
		msgfmt -o kit/languages/$${i18n}.mo kit/languages/$${i18n}.po ; \
	done
