#!/bin/bash

echo "[`date +"%d-%b-%Y %T"`] PHP-FPM Docker container boot"

### create system user for php pool
if [ ! "$PHP_USER" == "" ] && [ ! "$PHP_UID" == "" ] && [ ! "$PHP_GID" == "" ] && [ ! "$PHP_HOME" == "" ]; then
	echo "env PHP_USER: creating new system user: ${PHP_USER} ${PHP_UID}:${PHP_GID} ${PHP_HOME}"
	mkdir -p $PHP_HOME && \
	groupadd -f $PHP_USER -g $PHP_GID && \
  useradd  -d $PHP_HOME -u $PHP_UID -g $PHP_GID -s /usr/sbin/nologin $PHP_USER
fi

### load PHP ini configurations
if [ ! "$PHP_INI_PATH" == "" ]; then
	printf "env PHP_INI_PATH: "
	for f in ${PHP_INI_PATH}; do
		printf "$f, ";
		rm -f /usr/local/etc/php/conf.d/${f##*/}
		cp $f /usr/local/etc/php/conf.d/
	done
	echo "into [ /usr/local/etc/php/conf.d/ ] folder..."
fi

### make sure to create clean php-fpm.conf
### to avoid adding multiple times the same lines next
### in case container is restarting
echo "[global]"                     >  /usr/local/etc/php-fpm.conf
echo "include=etc/php-fpm.d/*.conf" >> /usr/local/etc/php-fpm.conf

### load PHP-FPM pool configurations
if [ ! "$PHP_POOL_PATH" == "" ]; then
	printf "env PHP_POOL_PATH: "
	for f in ${PHP_POOL_PATH}; do
		printf "$f, ";
		rm -f /usr/local/etc/php-fpm.d/${f##*/}
		cp $f /usr/local/etc/php-fpm.d/
	done
	echo "into [ /usr/local/etc/php-fpm.d/ ] folder..."
fi

### load boot bash scripts
for f in /usr/local/bin/*.sh; do
	/bin/bash -c $f
done

### load additional on-demand scripts
if [ ! -z ${PHP_BOOT_SCRIPTS+x} ] && [ "$PHP_BOOT_SCRIPTS" != "" ]; then
	printf "env PHP_BOOT_SCRIPTS: "

	for f in ${PHP_BOOT_SCRIPTS}; do
		printf "$f, ";

		rm -f /usr/local/bin/${f##*/}
		cp $f /usr/local/bin/

		chmod +x /usr/local/bin/${f##*/}
	done

	echo

	for f in ${PHP_BOOT_SCRIPTS}; do
		/bin/bash -c /usr/local/bin/${f##*/}
	done
fi

### expose php version
echo
/usr/local/bin/php -v
