# Custom code intended to be used after dh_installsysusers/dh_usrlocal
# and before dh_installinit/dh_installsystemd
if [ "$1" = "configure" ]; then
	# Fixing config directory permissions
	for d in yaws yaws/conf.avail yaws/conf.d ; do
		if ! dpkg-statoverride --list /etc/$d >/dev/null; then
			chown root:yaws /etc/$d
			chmod 750 /etc/$d
		fi
	done

	# Yaws configs may contain sensitive information (passwords)
	for f in /etc/yaws/yaws.conf /etc/yaws/conf.avail/*.conf ; do
		if ! dpkg-statoverride --list $f >/dev/null; then
			chown root:yaws $f
			chmod 640 $f
		fi
	done

	# Create config symlinks
	# Do it only if it's a fresh install or upgrade from version
	# less than 1.66-2 (which is the first version with multiple
	# configs), allowing admin to safely remove it.

	if dpkg --compare-versions "$2" lt "1.66-2" ; then
		for f in localhost.conf localhost-ssl.conf ; do
			if [ ! -f /etc/yaws/conf.d/$f ] ; then
				ln -s ../conf.avail/$f /etc/yaws/conf.d/
			fi
		done
	fi

	# Create the homedir if it doesn't exist for some reason
	mkdir -p /var/cache/yaws

	# Only user yaws and root are allowed to create yaws control files
	chown yaws:yaws /var/cache/yaws
	chmod 750 /var/cache/yaws

	install -o yaws -g adm  -m 750 -d /var/log/yaws

	# Finish moving /usr/lib/yaws/custom/* to /usr/local/lib/yaws-appmods/
	dira=/usr/lib/yaws-custom.dpkg-backup
	dirb=/usr/local/lib/yaws-appmods
	if [ -d $dira ] ; then
		# If the /usr/lib/yaws/custom.dpkg-backup directory exists then we
		# are upgrading from an older version and have to move it to
		# /usr/local/lib/yaws-appmods

		echo "Relocating /usr/lib/yaws/custom into $dirb..."

		for f in $(ls -A $dira) ; do
			mv -f $dira/$f $dirb/
		done
		rmdir $dira
	fi
fi
# End of custom code
