#! /usr/bin/env sh

replace_vars()
{
  file="${1}"
  sed -e "s/DOVECOT_LMTP_PORT/${DOVECOT_LMTP_PORT}/" -i "${file}"
  sed -e "s/DOVECOT_SASL_PORT/${DOVECOT_SASL_PORT}/" -i "${file}"
  sed -e "s/EXIM_SMARTHOST_HOST/${EXIM_SMARTHOST_HOST}/" -i "${file}"
  sed -e "s/EXIM_SMARTHOST_USER/${EXIM_SMARTHOST_USER}/" -i "${file}"
  sed -e "s/EXIM_SMARTHOST_PASSWORD/${EXIM_SMARTHOST_PASSWORD}/" -i "${file}"
}

sed -e "s/connect *=.*/connect = host=${POSTGRES_HOST} dbname=${POSTGRES_DB} user=${POSTGRES_USER} password=${POSTGRES_PASSWORD}/g" -i /etc/dovecot/dovecot-pgsql.conf


replace_vars /etc/dovecot/dovecot.conf
replace_vars /etc/exim4/update-exim4.conf.conf
replace_vars /etc/exim4/passwd.client


rm -f /var/run/dovecot/master.pid &&
rm -f /var/run/exim4/exim.pid &&
service exim4 start &&

dovecot -F
