#!/bin/sh

set -e

. /usr/share/debconf/confmodule

CONFFILE="/etc/openerp-server.conf"
LOGFILE="/var/log/openerp-server.log"

case "${1}" in
	configure)
		db_version 2.0

		db_get openerp-server/username
		_USERNAME="${RET:-openerp}"

		db_stop

		if ! getent passwd | grep -q "^${_USERNAME}"
		then
			adduser --system --no-create-home --quiet --gecos 'Open ERP server' --group ${_USERNAME}
		else
			echo "Open ERP user (${_USERNAME}) already exists, doing nothing."
		fi

		sed -i -e "s|^process_user.*$|process_user = ${_USERNAME}|" ${CONFFILE}

		# Creating log file
		touch ${LOGFILE}
		chown ${_USERNAME}:adm ${LOGFILE}
		chmod 0640 ${LOGFILE}

		# Creating local storage directory
		mkdir -p /var/lib/openerp-server/filestore

		# Setting ownership and permissions
		chmod 0640 ${CONFFILE}
		chown ${_USERNAME}:${_USERNAME} ${CONFFILE}
		chown ${_USERNAME}:${_USERNAME} /var/lib/openerp-server -R

		echo
		echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
		echo "* Open ERP uses a PostgreSQL database to store its data. With the first *"
		echo "* generation of packages, you have to setup this database manually.     *"
		echo "* Please read /usr/share/doc/openerp-server/README.Debian how to do it. *"
		echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
		echo
		;;

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

		;;

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

#DEBHELPER#

exit 0
