#!/bin/sh
# postinst script for #PACKAGE#
#
# 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


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

		# copy sample configs
		echo 'Creating default configs if not exists' 1>&2
		/bin/cp -n /etc/zuul/samples/layout.yaml-sample /etc/zuul/layout.yaml
		/bin/cp -n /etc/zuul/samples/logging.conf-sample /etc/zuul/logging.conf
		/bin/cp -n /etc/zuul/samples/zuul.conf-sample /etc/zuul/zuul.conf
		/bin/cp -n /etc/zuul/samples/clonemap.yaml-sample /etc/zuul/clonemap.yaml

		chown -R zuul:zuul /etc/zuul
		chown -R zuul:zuul /var/lib/zuul
		chown -R zuul:adm /var/log/zuul
		chmod 0640 -R /etc/zuul/*

		chmod 0750 /etc/zuul
		test -d /etc/zuul/layout && chmod 0750 /etc/zuul/layout
		chmod 0750 /var/lib/zuul
		chmod 0750 /var/log/zuul

		# start of zuul services
		service zuul start
		service zuul-merger start
	;;

	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
