#!/bin/sh
#
# Postinst script for fetchmail
# $Id: fetchmail.postinst 469 2008-05-18 12:47:05Z nion $
#

set -e

# Create fetchmail user and its homedir if we may need it
if ! getent passwd fetchmail >/dev/null; then
	adduser --system --ingroup nogroup --home /var/lib/fetchmail \
	--shell /bin/false --disabled-password fetchmail
fi

# if the login shell is not /bin/false, change this, see #481727
LOGINSH=$(getent passwd fetchmail | cut -d ':' -f 7)
if [ "x$LOGINSH" != "x/bin/false" ]; then
	chsh -s /bin/false fetchmail
fi

if ! [ -d /var/lib/fetchmail ]; then
    mkdir -p /var/lib/fetchmail
fi
chmod 700 /var/lib/fetchmail
chown -h -R fetchmail:nogroup /var/lib/fetchmail

# Code to handle the upgrade to use /etc/default/fetchmail
case "$1" in
  configure)
    if dpkg --compare-versions "$2" lt 6.3.1
	then
		if [ -e /etc/fetchmailrc ]
		then
			if [ `grep -c poll /etc/fetchmailrc` ]
			then
				# If /etc/fetchmailrc exits and is defined a pool line
				# I assume is correctly configured and make the default to
				# run on boot
				FILE=`mktemp`
				cat /etc/default/fetchmail | sed 's/START_DAEMON=no/START_DAEMON=yes/' > $FILE
				mv $FILE /etc/default/fetchmail
			fi
		fi
		# update home directory for old installations because of #327250
		usermod -d /var/lib/fetchmail fetchmail

		# Removing old /var/run/fetchmail if empty
	    rm -f "/var/run/fetchmail/.fetchids" >/dev/null 2>&1 || true
		rm -f "/var/run/fetchmail/.fetchmail-UIDL-cache" >/dev/null 2>&1 || true
		if [ ! -f "/var/lib/fetchmail/.fetchmail.pid" ]
		then
			mv "/var/run/fetchmail/.fetchmail.pid" "/var/lib/fetchmail/.fetchmail.pid" >/dev/null 2>&1 || true
		fi
	    rmdir "/var/run/fetchmail" >/dev/null 2>&1 || true
	fi
    ;;
  abort-upgrade|abort-remove|abort-deconfigure)
    ;;
esac
 
if [ -x /etc/init.d/fetchmail ]; then
	update-rc.d fetchmail start 99 2 3 4 5 . >/dev/null

	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d --quiet fetchmail start || true
	else
		/etc/init.d/fetchmail start || true
	fi
fi

#DEBHELPER#

exit 0

# vim:ts=4:sw=4:
