#!/bin/sh

# Fail on any error
#
set -e

# Bypass phpturd virtual filesystem
#
unset PHPTURD

# Set required variables on systemd-239 and older
#
if [ -z "${RUNTIME_DIRECTORY}" ] ; then
    RUNTIME_DIRECTORY=/run/suitecrm/${INSTANCE}
fi
if [ -z "${STATE_DIRECTORY}" ] ; then
    STATE_DIRECTORY=/var/lib/suitecrm/${INSTANCE}
fi
if [ -z "${CACHE_DIRECTORY}" ] ; then
    CACHE_DIRECTORY=/var/cache/suitecrm/${INSTANCE}
fi
if [ -z "${LOGS_DIRECTORY}" ] ; then
    LOGS_DIRECTORY=/var/log/suitecrm/${INSTANCE}
fi
if [ -z "${CONFIGURATION_DIRECTORY}" ] ; then
    CONFIGURATION_DIRECTORY=/etc/suitecrm/${INSTANCE}
fi

# Restore selinux file contexts on directories created by systemd
#
# This ought not to be necessary.  systemd's selinux-util.c installs a
# callback that is supposed to detect an selinux policy reload.
# However, experiments with strace show that systemd receives the
# SELNL_MSG_POLICYLOAD but then completely ignores the updated policy.
# Using "systemctl daemon-reexec" causes systemd to pick up the new
# policy, but this is not something we want to do from an RPM
# postinstall script.
#
# Work around the problem by using restorecon to correct the file
# contexts on the directories that were created by systemd based on
# potentially-outdated policy.
#
restorecon ${RUNTIME_DIRECTORY} \
	   ${STATE_DIRECTORY} \
	   ${CACHE_DIRECTORY} \
	   ${LOGS_DIRECTORY} \
	   ${CONFIGURATION_DIRECTORY}

# Create writable directories
#
for rwdir in ${CACHE_DIRECTORY}/session \
	     ${CACHE_DIRECTORY}/wsdl \
	     ${CACHE_DIRECTORY}/cache \
	     ${STATE_DIRECTORY}/custom \
	     ${STATE_DIRECTORY}/upload ; do
    mkdir -p ${rwdir}
    chmod 0770 ${rwdir}
    chgrp suitecrm ${rwdir}
done

# Create readonly directories
#
for rodir in ${STATE_DIRECTORY}/install \
	     ${STATE_DIRECTORY}/Api/V8/OAuth2 \
	     ${CONFIGURATION_DIRECTORY}/conf.d ; do
    mkdir -p ${rodir}
    chmod 0750 ${rodir}
    chgrp suitecrm ${rodir}
done

# Create writable placeholder files
#
for rwfile in ${STATE_DIRECTORY}/install/status.json \
	      ${STATE_DIRECTORY}/config_override.php \
	      ${CONFIGURATION_DIRECTORY}/private.key \
	      ${CONFIGURATION_DIRECTORY}/public.key \
	      ${LOGS_DIRECTORY}/error.log \
	      ${LOGS_DIRECTORY}/install.log \
	      ${LOGS_DIRECTORY}/php-fpm.log \
	      ${LOGS_DIRECTORY}/slow.log \
	      ${LOGS_DIRECTORY}/suitecrm.log ; do
    touch ${rwfile}
    chmod 0660 ${rwfile}
    chgrp suitecrm ${rwfile}
done

# Create readonly placeholder files
#
for rofile in ${CONFIGURATION_DIRECTORY}/config.php \
	      ${CONFIGURATION_DIRECTORY}/api.key \
	      ${STATE_DIRECTORY}/conf_d.php ; do \
    touch ${rofile}
    chmod 0640 ${rofile}
    chgrp suitecrm ${rofile}
done

# Assemble conf_d.php
#
cat $(find {/etc/suitecrm,${CONFIGURATION_DIRECTORY}}/conf.d \
	   -name '*.php' | \
      while read phpinc ; do echo $(basename ${phpinc}) ${phpinc} ; done | \
      sort -s | cut -d" " -f2) > ${STATE_DIRECTORY}/conf_d.php

# Create API key
#
if [ ! -s ${CONFIGURATION_DIRECTORY}/api.key ] ; then
    head -c 32 /dev/random > ${CONFIGURATION_DIRECTORY}/api.key
fi

# Create private key
#
if [ ! -s ${CONFIGURATION_DIRECTORY}/private.key ] ; then
    openssl genrsa -out ${CONFIGURATION_DIRECTORY}/private.key
fi

# Create public key
#
if [ ! -s ${CONFIGURATION_DIRECTORY}/public.key ] ; then
    openssl rsa -in ${CONFIGURATION_DIRECTORY}/private.key \
	    -pubout -out ${CONFIGURATION_DIRECTORY}/public.key
fi

# Create symlinks
#
ln -s -f -T ${CACHE_DIRECTORY}/cache ${STATE_DIRECTORY}/cache
ln -s -f -T ${LOGS_DIRECTORY}/install.log ${STATE_DIRECTORY}/install.log
ln -s -f -T ${LOGS_DIRECTORY}/suitecrm.log ${STATE_DIRECTORY}/suitecrm.log
ln -s -f -T ${CONFIGURATION_DIRECTORY}/config.php ${STATE_DIRECTORY}/config.php
ln -s -f -T ${CONFIGURATION_DIRECTORY}/api.key ${STATE_DIRECTORY}/api.key
ln -s -f -T ${CONFIGURATION_DIRECTORY}/private.key \
   ${STATE_DIRECTORY}/Api/V8/OAuth2/private.key
ln -s -f -T ${CONFIGURATION_DIRECTORY}/public.key \
   ${STATE_DIRECTORY}/Api/V8/OAuth2/public.key
ln -s -f -T /dev/null ${STATE_DIRECTORY}/.htaccess

# Create php-fpm configuration file
#
cat > ${RUNTIME_DIRECTORY}/php-fpm.conf <<EOF
[global]
pid=${RUNTIME_DIRECTORY}/php-fpm.pid
error_log=${LOGS_DIRECTORY}/php-fpm.log
daemonize=yes

[pool]
user = suitecrm
group = suitecrm
listen = ${RUNTIME_DIRECTORY}/php-fpm.sock
listen.group = suitecrm
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = ${LOGS_DIRECTORY}/slow.log
catch_workers_output = on
php_admin_value[error_log] = ${LOGS_DIRECTORY}/error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = ${CACHE_DIRECTORY}/session
php_value[soap.wsdl_cache_dir] = ${CACHE_DIRECTORY}/wsdl
php_value[upload_max_filesize] = 128M
php_value[include_path] = /usr/share/suitecrm/code:${STATE_DIRECTORY}:/usr/share/pear
env[PHPTURD] = /usr/share/suitecrm/code:${STATE_DIRECTORY}
EOF
