# adds imapd and pop3 entries to /etc/inetd.conf

if [ -z "$PKG_INSTALL_ROOT" ]; then
	SERVICES=/etc/services
	SSLDIR=/etc/ssl
	CONFDIR=/etc/dovecot
else
	SERVICES=${PKG_INSTALL_ROOT}/etc/services
	SSLDIR=${PKG_INSTALL_ROOT}/etc/ssl
	CONFDIR=${PKG_INSTALL_ROOT}/etc/dovecot
fi

for f in $SERVICES ; do
	if [ ! -f $f ]; then
		cp /dev/null $f
		chmod 644 $f
	fi
done

PATH=/usr/xpg4/bin:$PATH

TMP=`egrep -c -e '^pop2[[:space:]]*.*/tcp' $SERVICES`
if [ $TMP -eq 0 ]; then
	echo 'pop2		109/tcp	pop pop-2	# Post Office Protocol - Version 2' >>$SERVICES
fi
TMP=`egrep -c -e '^pop3[[:space:]]*.*/tcp' $SERVICES`
if [ $TMP -eq 0 ]; then
	echo 'pop3		110/tcp			# Post Office Protocol - Version 3' >>$SERVICES
fi
TMP=`egrep -c -e '^imap[[:space:]]*.*/tcp' $SERVICES`
if [ $TMP -eq 0 ]; then
	echo 'imap		143/tcp			# Internet Mail Access Protocol v2' >>$SERVICES
fi
TMP=`egrep -c -e '^imaps[[:space:]]*.*/tcp' $SERVICES`
if [ $TMP -eq 0 ]; then
	echo 'imaps		993/tcp			# Internet Mail Access Protocol v2 over SSL/TLS' >>$SERVICES
fi
TMP=`egrep -c -e '^pop3s[[:space:]]*.*/tcp' $SERVICES`
if [ $TMP -eq 0 ]; then
	echo 'pop3s		995/tcp			# Internet Mail Access Protocol v2 over SSL/TLS' >>$SERVICES
fi

for dir in certs keys ; do
	echo " "
	if [ ! -r ${SSLDIR}/${dir}/dovecot.pem ]; then
		cp -p ${SSLDIR}/${dir}/dovecot.pem.default ${SSLDIR}/${dir}/dovecot.pem
		echo "NOTE: Installed self-signed SSL certificate for $cert as"
		echo "      ${SSLDIR}/${dir}/dovecot.pem ! You should create or buy"
		echo "      your own SSL certificate, since using the default one"
		echo "      gives you not much security - anybody knows the key!!!"
	else
		WHAT="certificate"
		[ "$dir" == "keys" ] && WHAT="key"
		echo "It seems, that you have already an SSL $WHAT for dovecot."
		echo "Fine."
	fi
done

if [ ! -r $CONFDIR/dovecot.conf ]; then
	cp -p $CONFDIR/original/dovecot.conf $CONFDIR/dovecot.conf
	for CNF in 10-auth.conf 10-logging.conf 10-mail.conf 10-master.conf 10-ssl.conf 20-imap.conf 20-pop3.conf 90-plugin.conf auth-system.conf auth-deny.conf; do
		if [ ! -r $CONFDIR/conf.d/$CNF ]; then
			cp -p $CONFDIR/original/conf.d/$CNF $CONFDIR/conf.d/
		fi
	done
fi

echo "
NOTE: Per default dovecot is disabled. To enable the service,
      verify the configuration file /etc/dovecot/dovecot.conf
      and use the svcadm(1M) tool as usual, i.e.

      svcadm enable dovecot
"
# vim: ts=4 filetype=sh
PATH=/usr/xpg4/bin:/usr/bin:/usr/sbin:/sbin
export PATH

ETCDIR=${PKG_INSTALL_ROOT}/etc

# check, whether the group dovecot exists
egrep -s '^dovecot:' ${ETCDIR}/group
if  [ $? -ne 0 ]; then
echo " 
WARNING:
  For security reason dovecot should be run as user with the group 'dovecot'.
  This group is not part of your ${ETCDIR}/group ! Please setup
  such an group! E.g. using:

	groupadd -g 43 dovecot
"
	BREAK="true"
fi

# check, whether a user dovecot exists
egrep -s '^dovecot:' ${ETCDIR}/passwd
if  [ $? -ne 0 ]; then
echo "
WARNING:
  For security reason dovecot should be run as user 'dovecot'.
  This user is not part of your ${ETCDIR}/passwd ! Please setup
  such an account! E.g.:

	useradd -c 'Dovecot Authentication Helpers' -d /var/dovecot \\
		-u 43 -g dovecot -s /bin/true dovecot
"
	BREAK="true"
fi

#if [ "$BREAK" = "true" ]; then
#	exit 3
#fi

exit 0
