#!/bin/sh
# postinst script for indimail
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

prefix=/usr
servicedir=/service
qsysconfdir=/etc/indimail
libexecdir=/usr/libexec/indimail
rm=/bin/rm
####################################

setperm()
{
	local user="$1"; shift
	local group="$1"; shift
	local mode="$1"; shift
	local file="$1"; shift
	/usr/sbin/dpkg-statoverride --list "$file" >/dev/null && return 0
	/usr/sbin/dpkg-statoverride --update --add "$user" "$group" "$mode" "$file"
}

fixperms()
{
	if [ $# -eq 3 ] ; then # directory
	#%dir %attr(0555,root,indimail) /var/indimail/sbin
		lin=$2
		file=$3
		if [ ! -d $file ] ; then
			return 0
		fi
	elif [ $# = 2 ] ; then # file
	#%attr(0555,root,qmail) /var/indimail/bin/rsmtp
		lin=$1
		file=$2
		if [ ! -f $file ] ; then
			return 0
		fi
	fi
	perm=`echo $lin|cut -d\( -f2 | cut -d, -f1`
	perm_f1=`echo $perm | cut -c1`
	own=`echo $lin|cut -d\( -f2 | cut -d, -f2`
	grp=`echo $lin|cut -d\( -f2 | cut -d, -f3|cut -d\) -f1`
	if [ -f $file ] ; then
		if [ $perm_f1 -eq 0 ] ; then # 4755, 0644
			/bin/chown $own:$grp $file
			/bin/chmod $perm $file
		else
			echo $file
			if [ -f /usr/sbin/dpkg-statoverride ] ; then
				setperm $own $grp $perm $file
			else
				/bin/chown $own:$grp $file
				/bin/chmod $perm $file
			fi
		fi
	elif [ -d $file ] ; then
		/bin/chown $own:$grp $file
		/bin/chmod $perm $file
	fi
}

ID=`id -u`
if [ $ID -ne 0 ] ; then
	echo "You are not root" >&2
	exit 1
fi

upgrade=0
case "$1" in
	configure)
	if [ -n "$2" ] ; then
		upgrade=1
	fi
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
	exit 0
	;;

	*)
	echo "postinst called with unknown argument \`$1'" >&2
	exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

#
# We need to do this for debian. SIGH!!
#
if [ -f ${qsysconfdir}/ucspi-tcp.perms ] ; then
	(
	/bin/grep -v "^#" ${qsysconfdir}/ucspi-tcp.perms | while read line
	do
		if [ " $line" = " " ] ; then
			continue
		fi
		fixperms $line
	done
	) > ${qsysconfdir}/ucspi-tcp-stat.override
	if [ ! -s ${qsysconfdir}/ucspi-tcp-stat.override ] ; then
		/bin/rm -f ${qsysconfdir}/ucspi-tcp-stat.override
	fi
fi

if [ $upgrade -eq 1 ] ; then # upgrade
	(
	echo "checking services using tcpclient, tcpserver"
	set +e
	for i in `grep -l -E "tcpserver|tcpclient" ${servicedir}/*/run`
	do
		i=$(dirname $i)
		if [ -f $i.down ] ; then
			${rm} -f $i.down
			${prefix}/bin/svc -u $i 2>/dev/null || true
		fi
	done
	set -e
	) >> /var/log/ucspi-tcp-setup.log 2>&1
	exit 0
fi

(
echo "checking permissions"
if [ -x ${libexecdir}/instcheck.ucspi-tcp ] ; then
	${libexecdir}/instcheck.ucspi-tcp > /dev/null 2>&1
fi
) >> /var/log/ucspi-tcp-setup.log
exit 0
