#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# precreate log files
if [[ ! -f /config/log/php/error.log ]]; then
    touch /config/log/php/error.log
fi

# set default timezone
if [[ -f /etc/php83/conf.d/30_timezone.ini ]]; then
    printf "date.timezone = %s\\n" "${TZ:-UTC}" >/etc/php83/conf.d/30_timezone.ini
fi

# create local php.ini if it doesn't exist, set local timezone
if [[ ! -f /config/php/php-local.ini ]]; then
    printf "; Edit this file to override php.ini directives\\n\\n" >/config/php/php-local.ini
fi

# symlink user php-local.ini to image
rm -rf /etc/php83/conf.d/php-local.ini
rm -rf /etc/php83/conf.d/zzz_php-local.ini

if [[ -f /config/php/php-local.ini ]] && [ -d /etc/php83/conf.d ]; then
    ln -s /config/php/php-local.ini /etc/php83/conf.d/zzz_php-local.ini
fi

# create override for www.conf if it doesn't exist
if [[ ! -f /config/php/www2.conf ]]; then
    printf "; Edit this file to override www.conf and php-fpm.conf directives and restart the container\\n\\n; Pool name\\n[www]\\n\\n" >/config/php/www2.conf
fi

# copy user www2.conf to image
if [[ -f /config/php/www2.conf ]] && [ -d /etc/php83/conf.d ]; then
    cp /config/php/www2.conf /etc/php83/php-fpm.d/www2.conf
fi
