# SliTaz package receipt.

PACKAGE="cyrus-imapd"
VERSION="2.3.16"
CATEGORY="network"
SHORT_DESC="IMAP server."
MAINTAINER="pascal.bellard@slitaz.org"
TARBALL="$PACKAGE-$VERSION.tar.gz"
WEB_SITE="http://cyrusimap.web.cmu.edu/"
WGET_URL="ftp://ftp.andrew.cmu.edu/pub/cyrus/$TARBALL"
BUILD_DEPENDS="libcomerr libcomerr-dev cyrus-sasl cyrus-sasl-dev openssl-dev \
db-dev perl"
DEPENDS="libcomerr cyrus-sasl openssl db perl libwrap slitaz-base-files \
net-snmp"
CONFIG_FILES="/etc/imapd.conf /etc/cyrus.conf"

# Rules to configure and make the package.
compile_rules()
{
	cd $src
	./configure --prefix=/usr --infodir=/usr/share/info \
	--with-service-path=/usr/lib/cyrus \
	--with-cyrus-prefix=/usr/lib/cyrus \
	--with-com_err=yes \
	--mandir=/usr/share/man $CONFIGURE_ARGS &&
	make &&
	make DESTDIR=$PWD/_pkg install
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/usr/lib $fs/var/imap $fs/var/spool/imap/stage.
	cp -a $_pkg/usr/bin $fs/usr
	cp -a $_pkg/usr/lib/cyrus $fs/usr/lib
	cp -a $_pkg/usr/lib/perl5 $fs/usr/lib
	strip -s $fs/usr/lib/cyrus/*
	cp $src/tools/mkimap $fs/usr/lib/cyrus
	cp -a $stuff/etc $fs
	cp $src/master/conf/small.conf $fs/etc/cyrus.conf
	for i in proc db socket log msg ptclient; do
		mkdir -m 755 $fs/var/imap/$i
	done
}

# Pre and post install commands for Tazpkg.
post_install()
{
        # adduser cyrus if needed
	if ! grep -q cyrus $1/etc/passwd; then
		echo -n "Adding user cyrus..."
		chroot $1/ adduser cyrus -D -H -S -h /tmp
		chroot $1/ sh -c 'echo "cyrus:cyrus" | chpasswd -m > /dev/null'
		status
	fi
	# addgroup mail if needed
	if ! grep -q mail $1/etc/group; then
		echo -n "Adding group mail..."
		chroot $1/ sh -c 'addgroup mail && addgroup cyrus mail'
		status
	fi
	chroot $1/ chown -R cyrus.mail /var/spool/imap /var/imap
	while read name port end; do
		grep -q $port $1/etc/services || \
			echo "$name		$port	$end" >> $1/etc/services
	done <<EOF
pop3 110/tcp pop-3
nntp 119/tcp readnews untp
imap 143/tcp imap2 imap4
imsp 406/tcp 
nntps 563/tcp snntp
acap 674/tcp
imaps 993/tcp
pop3s 995/tcp
kpop 1109/tcp
sieve 2000/tcp
lmtp 2003/tcp
fud 4201/udp
EOF
	[ -z "$1" ] && /etc/init.d/$PACKAGE start
	if [ -f $1/etc/init.d/postfix ]; then
		if ! grep -v ^# $1/etc/postfix/main.cf | grep -q lmtp:unix; then
			echo "Append to /etc/postfix/main.cf:"
			echo -n "    "
			echo "mailbox_transport = lmtp:unix:/var/imap/socket/lmtp" | \
				tee -a $1/etc/postfix/main.cf
			if [ -z "$1" ]; then
				/etc/init.d/postfix start || /etc/init.d/postfix reload
			fi
		fi
	fi
	msg="Creating mailbox for"
	for i in root $(awk -F: '{ if ($3 >= 1000) print $1 }' < $1/etc/passwd); do
		echo -n "$msg $i"
		msg=","
	done
	echo ""
	for i in root $(awk -F: '{ if ($3 >= 1000) print $1 }' < $1/etc/passwd); do
		echo "createmailbox user.$i"
	done | chroot $1/ cyradm -u cyrus -w cyrus 127.0.0.1 >/dev/null 2>&1
	chroot $1/ su -c "/usr/lib/cyrus/bin/reconstruct" cyrus >/dev/null 2>&1
	cat <<EOF
----
Users must have a password to access the mailbox.
To start $PACKAGE server you can run :

    /etc/init.d/$PACKAGE start

Or add $PACKAGE to RUN_DAEMONS in /etc/rcS.conf
----
EOF

	# A security hole with ssh...
	[ -f $1/etc/ssh/sshd_config ] &&
	! grep -q cyrus $1/etc/ssh/sshd_config &&
	echo "DenyUsers cyrus" >> $1/etc/ssh/sshd_config
}

post_remove()
{
	deluser cyrus
	delgroup mail
}