#! /usr/bin/env sh

sed -e "s/POSTGRES_HOST/${POSTGRES_HOST}/" -i /etc/opendkim/opendkim.conf
sed -e "s/POSTGRES_USER/${POSTGRES_USER}/" -i /etc/opendkim/opendkim.conf
sed -e "s/POSTGRES_PASSWORD/${POSTGRES_PASSWORD}/" -i /etc/opendkim/opendkim.conf
sed -e "s/POSTGRES_DB/${POSTGRES_DB}/" -i /etc/opendkim/opendkim.conf
sed -e "s/OPENDKIM_PORT/${OPENDKIM_PORT}/" -i /etc/opendkim/opendkim.conf
sed -e "s/MAIL_DOMAIN/${MAIL_DOMAIN}/" -i /etc/opendkim/KeyTable

if [ ! -e /etc/opendkim/keys/dkim.private ]
then
  cd /etc/opendkim/keys &&
  opendkim-genkey -s dkim -d "${MAIL_DOMAIN}" &&
  chown opendkim:opendkim dkim.private dkim.txt
  cd --
fi

sleep 5

rm -f /var/run/rsyslogd.pid
rm -f /var/run/opendkim/opendkim.pid
service rsyslog start
service opendkim start &&
cat /var/log/syslog
