#!/bin/bash
replace_vars()
{
  file="${1}"
  sed -e "s/POSTGRES_HOST/${POSTGRES_HOST}/" -i "${file}"
  sed -e "s/POSTGRES_USER/${POSTGRES_USER}/" -i "${file}"
  sed -e "s/POSTGRES_PASSWORD/${POSTGRES_PASSWORD}/" -i "${file}"
  sed -e "s/POSTGRES_DB/${POSTGRES_DB}/" -i "${file}"
  sed -e "s/MAIL_USER_DB_HOST/${MAIL_USER_DB_HOST}/" -i "${file}"
  sed -e "s/MAIL_USER_DB_USER/${MAIL_USER_DB_USER}/" -i "${file}"
  sed -e "s/MAIL_USER_DB_PASSWORD/${MAIL_USER_DB_PASSWORD}/" -i "${file}"
  sed -e "s/MAIL_USER_DB_DB/${MAIL_USER_DB_DB}/" -i "${file}"
  sed -e "s/MAIL_USER_DB_PORT/${MAIL_USER_DB_PORT}/" -i "${file}"
  sed -e "s!IMAP_HOST!${IMAP_HOST}!" -i "${file}"
  sed -e "s!SMTP_HOST!${SMTP_HOST}!" -i "${file}"
  sed -e "s/SIEVE_HOST/${SIEVE_HOST}/" -i "${file}"
  sed -e "s/DES_KEY/${DES_KEY}/" -i "${file}"
}

replace_vars /var/www/html/config/config.inc.php
replace_vars /var/www/html/plugins/password/config.inc.php
replace_vars /var/www/html/plugins/vacation_sieve/config.inc.php

rm -f /run/apache2/apache2.pid
exec /usr/sbin/apache2ctl -D FOREGROUND
