#!/bin/sh

set -e

. /usr/share/dbconfig-common/dpkg/postinst.pgsql
. /usr/share/dbconfig-common/dpkg/common
dbc_share="/usr/share/dbconfig-common"
dbc_package=$1
dbc_basepackage=`echo $dbc_package | cut -d_ -f1`
dbc_dbtype=pgsql
dbc_sqldir=$dbc_share/data/$dbc_basepackage/install/$dbc_dbtype
dbc_popdir=/usr/share/$dbc_basepackage/install/$dbc_dbtype
dbc_dbname=`grep -v '#' /etc/dbconfig-common/pybit-web.conf |grep dbc_dbname|cut -d= -f2|sed -e "s:'::g"`

if [ -d "$dbc_sqldir" ]; then
	CHK=`getent passwd $dbc_dbname || true`
	if [ "$CHK" = "" ]; then
		dbc_logline "adding system user $dbc_dbname -- see /usr/share/doc/pybit-web/README.Debian"
		adduser --quiet --system --shell /bin/sh --home /var/lib/pybit-web --no-create-home --group $dbc_dbname
	fi
	dbc_logline "altering database ownership for $dbc_dbname"
	su -s /bin/sh postgres -c "psql -d $dbc_dbname -c \"ALTER DATABASE $dbc_dbname OWNER TO $dbc_dbname;\" 2>/dev/null"
	for FILE in $dbc_sqldir/* ; do
		dbc_logline "creating tables in database for $dbc_dbname using $FILE "
		su -s /bin/sh $dbc_dbname -c "psql -d $dbc_dbname -f $FILE 2> /dev/null"
		dbc_logline "$FILE done"
	done
else
	dbc_logline "$dbc_sqldir not found\n"
fi

if [ -d "$dbc_popdir" ]; then
	for FILE in $dbc_popdir/* ; do
		dbc_logline "populating tables in database for $dbc_dbname using $FILE "
		su -s /bin/sh $dbc_dbname -c "psql -d $dbc_dbname -f $FILE 2> /dev/null"
		dbc_logline "$FILE done"
	done
else
	dbc_logline "$dbc_popdir not found\n"
fi
