#!/bin/sh

# install wordpress with cli : https://make.wordpress.org/cli/handbook/how-to-install/
# commands : https://developer.wordpress.org/cli/commands/
mkdir -p ${WP_DIR}
	wp core download --path="${WP_DIR}" --allow-root

# create config file :
if ! wp core version --path="${WP_DIR}"; then
	wp config create \
		--dbhost="${DB_HOST}" \
		--dbname="${DB_NAME}" \
		--dbuser="${DB_USER}" \
		--dbpass="${DB_PSWD}" \
		--path="${WP_DIR}" --allow-root
fi

# install wordpress :
if ! wp core is-installed --path="${WP_DIR}"; then
	wp core install \
		--url="${WP_URL}" \
		--title="${WP_TITLE}" \
		--admin_user="${WP_ADMIN}" \
		--admin_email="${WP_ADMIN_EMAIL}" \
		--admin_password="${WP_ADMIN_PSWD}" \
		--skip-email \
		--path="${WP_DIR}" --allow-root
fi

# create user :
	wp user create \
		"${WP_USER}" "${WP_USER_EMAIL}" \
		--user_pass="${WP_USER_PSWD}" \
		--path="${WP_DIR}" --allow-root

# change owner and permissions to wp files
chown -R www-data:www-data /var/www/*
chmod 755 -R /var/www/*

exec "${PHP_VERSION}" -FR

