WP_CLI=wp-cli --allow-root

install: configure

configure:  
	@echo "⚙️ Configuring Wordpress database..."
	dockerize -wait tcp://wp_service_1_db:3306 -timeout 120s 2> /dev/null #2>&1
	@rm -f wp-config.php

	@echo "Sleeping for 10 seconds to let dust settle in..."
	@sleep 10
	@echo "Continuing..."

	$(WP_CLI) core config \
		--dbhost=${WORDPRESS_DB_HOST} \
		--dbname=${WORDPRESS_DB_NAME} \
		--dbuser=${WORDPRESS_DB_USER} \
		--dbpass=${WORDPRESS_DB_PASSWORD} \
		--locale=${WORDPRESS_LOCALE}

	$(WP_CLI) core install \
		--url="${CHALL_SERVER_IP}:${CHALL_SERVER_PORT}" \
		--title="$(WORDPRESS_WEBSITE_TITLE)" \
		--admin_user=${WORDPRESS_ADMIN_USER} \
		--admin_password=${WORDPRESS_ADMIN_PASSWORD} \
		--admin_email=${WORDPRESS_ADMIN_EMAIL}

	$(WP_CLI) option update siteurl "http://${CHALL_SERVER_IP}:${CHALL_SERVER_PORT}"
	$(WP_CLI) rewrite structure $(WORDPRESS_WEBSITE_POST_URL_STRUCTURE)
	$(WP_CLI) plugin delete akismet
	$(WP_CLI) plugin delete hello-dolly
	$(WP_CLI) plugin activate ghost-post

	$(WP_CLI) user create ghosty ghosty@ctf.example --role=author --porcelain

	$(WP_CLI) post create --post_title="Ghost View" --post_status=publish --post_type=page --post_content='[ghost_list]' --post_author=2 --porcelain
	$(WP_CLI) post create --post_title="Ghost Stats" --post_status=publish --post_type=page --post_content='[ghost_sync_stats]' --post_author=2 --porcelain
	$(WP_CLI) post create --post_title='Found me!' --post_status=publish --post_type='ghost_entry' --post_content="Found it! ${FLAG_FLAG}" --post_author=2 --porcelain

	@chmod -R 555 /var/www/html/
	@chmod -R 755 /var/www/html/wp-content/uploads
	@curl "http://${CHALL_SERVER_IP}:${CHALL_SERVER_PORT}" > /dev/null
	@sleep 1
	@chmod 111 /var/www/html/wp-config.php