#!/bin/bash
set -e


if [ "$USER" = "root" ]; then

    # set localtime
    ln -sf /usr/share/zoneinfo/$LOCALTIME /etc/localtime

    # secure path
    chmod a-rwx -R $PHP_INI_DIR/conf.d/ /usr/local/etc/php-fpm.d/
fi

#
# functions


function set_conf {
    echo "$4">$2; IFSO=$IFS; IFS=$(echo -en "\n\b")
    
    for c in `printenv|grep $1`; do echo "`echo $c|cut -d "=" -f1|awk -F"$1" '{print $2}'` $3 `echo $c|cut -d "=" -f2`" >> $2; done;

    IFS=$IFSO
}

# Syntaxe Posix for Array and dot __env__ will do env[] and ___ will do .

function set_conf_posix {
    echo "$4">$2; IFSO=$IFS; IFS=$(echo -en "\n\b")
    
    for c in `printenv|grep $1`
    do 
      if [[ "${c,,}" == *"__env__"* ]] ;then
        echo "`echo $c|cut -d "=" -f1|awk -F"$1" '{print $2}' | awk -F"__" '{print $1 "[" $2 "]"'}` $3 `echo $c|cut -d "=" -f2`" >> $2
      elif [[ "${c,,}" == *"____"* ]] ;then
        echo "`echo $c|cut -d "=" -f1|awk -F"$1" '{print $2}' | awk -F"__" '{print $1 "[" $2 "]"'}` $3 `echo $c|cut -d "=" -f2`" >> $2
      else
        echo "`echo $c|cut -d "=" -f1|awk -F"$1" '{print $2}'` $3 `echo $c|cut -d "=" -f2`" >> $2
      fi
    done
    IFS=$IFSO
}

#
# PHP

echo "date.timezone = \"${LOCALTIME}\"" >> $PHP_INI_DIR/conf.d/00-default.ini
if [ "$PHP_php5enmod" != "" ]; then docker-php-ext-enable $PHP_php5enmod > /dev/null 2>&1; fi;

# Set php.ini
set_conf_posix "PHP__" "$PHP_INI_DIR/conf.d/40-user.ini" "="

# Set phpfpm.conf 
set_conf_posix "PHPFPM_GLOBAL__" "/usr/local/etc/php-fpm.d/40-user-global.conf" "=" "[global]"
set_conf_posix "PHPFPM__" "/usr/local/etc/php-fpm.d/41-user-pool.conf" "=" "[www]"



if [ -f /usr/local/etc/php-fpm.d/www.conf ]; then 
  mv /usr/local/etc/php-fpm.d/www.conf /usr/local/etc/php-fpm.d/00-www.conf
fi

#
# docker links

# Deprecated - Set memcached server with link
if [ -n "$PHP_MEMCACHED_PORT_11211_TCP_ADDR" ]; then
    echo "[WARNING] Deprecated - Future versions of Docker will not support links - you should remove them for forwards-compatibility."
    echo "session.save_handler = memcached" > $PHP_INI_DIR/conf.d/20-memcached.ini
    echo "session.save_path = $PHP_MEMCACHED_PORT_11211_TCP_ADDR:$PHP_MEMCACHED_PORT_11211_TCP_PORT" >> $PHP_INI_DIR/conf.d/20-memcached.ini
elif [ -f $PHP_INI_DIR/conf.d/20-memcached.ini ]; then
    rm $PHP_INI_DIR/conf.d/20-memcached.ini
fi

# Deprecated - Set msmtp server with link
if [ -n "$SMTP_PORT_25_TCP_ADDR" ]; then
    echo "[WARNING] Deprecated - Future versions of Docker will not support links - you should remove them for forwards-compatibility."
    echo 'sendmail_path = /usr/bin/msmtp -t' >> $PHP_INI_DIR/conf.d/00-default.ini
    echo -e "defaults \nauth           off \ntls            off \nlogfile        ~/.msmtp.log \naccount        mailcatcher \nhost           ${$SMTP_PORT_25_TCP_ADDR} \nport           ${$SMTP_PORT_25_TCP_PORT} \nauto_from on \naccount default : mailcatcher" > /etc/msmtprc
fi

# Set memcached session save handle
if [ -n "$MEMCACHED" ]; then
    if [ ! -f $PHP_INI_DIR/conf.d/docker-php-ext-memcached.ini ]; then docker-php-ext-enable  memcached > /dev/null; fi

    IFSO=$IFS; IFS=' ' read -ra BACKENDS <<< "${MEMCACHED}"
    for BACKEND in "${BACKENDS[@]}"; do
        SAVE_PATH="${SAVE_PATH}${BACKEND}?${MEMCACHED_CONFIG:-persistent=1&timeout=5&retry_interval=30},"
    done; IFS=$IFSO;

cat << EOF >> $PHP_INI_DIR/conf.d/20-memcached.ini
    session.save_handler = memcached
    session.save_path = "${SAVE_PATH}"
EOF

fi

# Set msmtp server
if [ -n "$SMTP" ]; then
    echo 'sendmail_path = /usr/bin/msmtp -t' >> $PHP_INI_DIR/conf.d/00-default.ini
    echo -e "defaults \nauth           off \ntls            off \nlogfile        ~/.msmtp.log \naccount        mailcatcher \nhost           ${SMTP} \nport           ${SMTP_PORT:-25} \nauto_from on \naccount default : mailcatcher" > /etc/msmtprc
fi


#
# Run
exec "$@"