# SliTaz package receipt.

PACKAGE="postgresql"
VERSION="9.1.2"
CATEGORY="misc"
SHORT_DESC="SQL database system."
MAINTAINER="pascal.bellard@slitaz.org"
TARBALL="$PACKAGE-$VERSION.tar.bz2"
WEB_SITE="http://www.postgresql.org/"
WGET_URL="ftp://ftp.postgresql.org/pub/source/v$VERSION/$TARBALL"
DEPENDS="postgresql-client slitaz-base-files"
BUILD_DEPENDS="zlib-dev readline-dev ncurses-dev bison flex perl libxslt"
CONFIG_FILES="/var/lib/pgsql/*.conf"
DATABASE_FILES="/var/lib/pgsql"

# Rules to configure and make the package.
compile_rules()
{
	cd $src
	[ -d ../postgrpsql-$VERSION ] && cp -a ../postgrpsql-$VERSION/* .
	cp $stuff/*.files-list .
	./configure --prefix=/usr --infodir=/usr/share/info \
	--sysconfdir=/etc --includedir=/usr/include/postgresql --with-gnu-ld \
	--with-includes=/usr/include/readline --enable-integer-datetimes \
	--enable-thread-safety --with-system-tzdata=/usr/share/zoneinfo \
	--libdir=/usr/lib/postgresql --mandir=/usr/share/man \
	$CONFIGURE_ARGS &&
	make &&
	make DESTDIR=$DESTDIR install
	make -C doc/src/sgml DESTDIR=$DESTDIR install-html
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/usr/share $fs/usr/lib $fs/var/lib/pgsql \
		 $fs/var/log/postgresql $fs/etc/pgsql.d
	cp -a $_pkg/usr/bin $fs/usr
	cp -a $_pkg/usr/lib/postgresql $fs/usr/lib
	rm -f $fs/usr/lib/postgresql/*a
	cp -a $_pkg/usr/share/postgresql $fs/usr/share
	cp -a $stuff/etc $fs
	ln -s /var/lib/pgsql/postgresql.conf $fs/etc
	chmod 700 $fs/var/lib/pgsql
	cat $stuff/*.files-list | while read file; do
		[ -f $fs$file ] && rm -f $fs$file
	done

}

# Pre and post install commands for Tazpkg.
post_install()
{
        # adduser postgres if needed
	if ! grep -q postgres: $1/etc/passwd; then
		echo -n "Adding user postgres..."
		chroot $1/ adduser postgres -D -H -u 88 -h /var/lib/pgsql
		#[ -d $1/var/lib/pgsql ] && rm -f $1/var/lib/pgsql/.* $1/var/lib/pgsql/*
		status
	fi
	# addgroup postgres if needed
	if ! grep -q postgres: $1/etc/group; then
		echo -n "Adding group postgres..."
		chroot $1/ sh -c 'addgroup -g 88 postgres'
		status
	fi
	[ ! -d $1/var/lib/pgsql ] && mkdir -p $1/var/lib/pgsql
	chroot $1/ chown -R postgres.postgres /var/lib/pgsql /var/log/postgresql
	cat <<EOF
----
postgres has superuser access.
Configure /var/lib/pgsql/*.conf files.
To start $PACKAGE server you can run :

    /etc/init.d/$PACKAGE start

Or add $PACKAGE to RUN_DAEMONS in /etc/rcS.conf
----
EOF
}

post_remove()
{
	# when both user and group have the same id# and name
	# both group and user will get removed with either delusr/delgroup
	chroot $1/ deluser postgres
}