#! /bin/sh

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>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>

case "$1" in
    configure)
    	# add asterisk user
	if ! getent passwd asterisk > /dev/null ; then
		echo 'Adding system user for Asterisk' 1>&2
		adduser --system --group --quiet \
			--home /var/lib/asterisk \
			--no-create-home --disabled-login \
			--gecos "Asterisk PBX daemon" \
			asterisk
	fi

	# add asterisk to required groups
	for group in dialout audio; do
		if groups asterisk | grep -w -q -v $group; then
			adduser asterisk $group
		fi
	done

	# chown asterisk on all $dirs and their subdirectories
	# do not harm the files, they should be empty on new installations
	# and we don't want to mess-up anything on old installations
	find /var/log/asterisk \
	     /var/lib/asterisk \
	     -type d | while read dir; do
		if ! dpkg-statoverride --list "$dir" > /dev/null ; then
			chown asterisk: $dir
		fi
	done 
	
	# this is not needed for new installations but is not such a bad idea
	# removing this will _break_ upgrades from versions < 1:1.4.10.1~dfsg-1
	#
	# we are doing the same for subdirectories, since we are not shipping
	# any and it's supposed to be user-modifiable
	if ! dpkg-statoverride --list "/etc/asterisk" > /dev/null ; then
		chown asterisk: /etc/asterisk
	fi

	# spool holds some sensitive information (e.g. monitor, voicemail etc.)
	find /var/spool/asterisk -type d | while read dir; do
		if ! dpkg-statoverride --list "$dir" > /dev/null ; then
			chown asterisk: $dir
			chmod 750 $dir
		fi
	done

	# Create /usr/local directory; policy 9.1.2
	if [ ! -e /usr/local/share/asterisk/sounds ]; then
		if mkdir -p /usr/local/share/asterisk/sounds 2>/dev/null ; then
			chown root:staff /usr/local/share/asterisk/sounds
			chmod 2775 /usr/local/share/asterisk/sounds
		fi
	fi

	### this is done here in case asterisk-config was installed/upgraded first

	set +e # ignore errors temporarily

	# find the name of the package providing config; either asterisk-config
	# or a package providing asterisk-config-custom
	ASTERISK_CONFIG=`dpkg-query -W -f='${Package}\t${Provides}\n' | \
			 sed -nr 's/(.*)\tasterisk-config-custom|(asterisk-config)(\t.*)?/\1\2/p'`

	# find conffiles under /etc/asterisk belonging to asterisk-config
	# and chown them to user asterisk.
	dpkg-query -W -f='${Conffiles}\n' $ASTERISK_CONFIG 2>/dev/null | \
	  sed -nr -e 's; (/etc/asterisk/.*) [0-9a-f]*;\1;p' | \
	while read conffile; do
		chown asterisk: $conffile 2>/dev/null
	done

	# handle them in the end with a glob since it's way faster
	dpkg-statoverride --quiet --list '/etc/asterisk/*' | while read STAT; do
		chown `echo $STAT | cut -d' ' -f 1,2,4 | sed 's/ /:/'` \
			2>/dev/null
	done

	set -e
    ;;

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

    *)
        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#

exit 0


