#!/bin/bash

H=$1

set -e
set -x

if [ "$H" == "" ]; then
   H=`/opt/pdadmin/bin/hostname.pl`
fi

test -e /opt/pdadmin/sslcerts/$H-key || {
    echo "/opt/pdadmin/sslcerts/$H-key not found"
    echo "usage: $0 <hostname>"
    exit 1
}

#qmail StartTTLS
touch /var/qmail/control/servercert.pem
chown qmaild:nofiles /var/qmail/control/servercert.pem
chmod 440 /var/qmail/control/servercert.pem
cat /opt/pdadmin/sslcerts/$H-key /opt/pdadmin/sslcerts/$H-cert /opt/pdadmin/sslcerts/$H-cacert > /var/qmail/control/servercert.pem

touch /usr/local/pd-admin2/share/imapd.pem
chown qmaild:nofiles /usr/local/pd-admin2/share/imapd.pem
chmod 440 /usr/local/pd-admin2/share/imapd.pem
cat /opt/pdadmin/sslcerts/$H-key /opt/pdadmin/sslcerts/$H-cert > /usr/local/pd-admin2/share/imapd.pem
ln -sf /usr/local/pd-admin2/share/imapd.pem /usr/local/pd-admin2/share/pop3d.pem
cat /opt/pdadmin/sslcerts/$H-cacert > /usr/local/pd-admin2/share/imapd.cacert

if [ ! -e /service/qmail-smtpSd/run ]; then

cd /usr/src
wget http://download.pd-admin.de/qmail-smtpSd.tar.gz
tar xzf qmail-smtpSd.tar.gz
mv qmail-smtpSd /service

fi

mkdir -p /etc/ssl/
cat /opt/pdadmin/sslcerts/$H-cacert > /etc/ssl/cert.pem

grep -q "^ssl *= *yes" /usr/local/pd-admin2/dovecot-2.2/etc/dovecot/conf.d/10-ssl.conf || /usr/local/pd-admin2/CONFIGURE/dovecot-2.2.sh 
svc -du /service/dovecot22/

grep TLSRSACertificateFile /usr/local/pd-admin2/etc/proftpd.conf > /dev/null || {
cat <<__ >> /usr/local/pd-admin2/etc/proftpd.conf
<IfModule mod_tls.c>
TLSEngine                  on
TLSProtocol SSLv3 TLSv1
TLSRSACertificateFile      /opt/pdadmin/sslcerts/$H-cert
TLSRSACertificateKeyFile   /opt/pdadmin/sslcerts/$H-key
TLSCACertificateFile       /opt/pdadmin/sslcerts/$H-cacert
TLSVerifyClient            off
TLSRequired                off
TLSRenegotiate none
</IfModule>
__
killall proftpd
}

crontab -l | grep -q update_tmprsadh || {
cd /usr/src
wget download.pd-admin.de/.cron_tmprsadh.pl
perl .cron_tmprsadh.pl
/var/qmail/bin/update_tmprsadh
}

/opt/pdadmin/bin/httpd_vhosts.pl

exit
