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

if [ -z "$PKG_INSTALL_ROOT" ]; then
	SERVICES=/etc/services
	SSLDIR=/etc/ssl
else
	SERVICES=${PKG_INSTALL_ROOT}/etc/services
	SSLDIR=${PKG_INSTALL_ROOT}/etc/ssl
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
		echo "It seems, that you have already an SSL certificate for dovecot."
		echo "Fine."
	fi
done

echo "
NOTE: Per default dovecot is disabled. To enable the service,
      create the appropriate configuration file /etc/dovecot.conf
      (you may have a look at /etc/dovecot.conf.default) 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
