#! /bin/bash
#include <genscript.warning>

#include <get_old_bins.inc>

SHELL=/bin/sh
export dpkg_action=$1

current=%PG_VERSION%
lastinstall=`echo $2 | cut -d\- -f1 | cut -d. -f1-2`
operation=$1

if [ -r /etc/postgresql/postgresql.env ]
then
	set -a
	. /etc/postgresql/postgresql.env
	set +a
fi

echo
if [ -d ${PGDATA:=/var/lib/postgres/data} ]
then
	installed=`cat ${PGDATA}/PG_VERSION 2>/dev/null`
	if [ -n "${installed}" ]
	then
		if [ A"${installed}" != A${current} ]
		then
			# This is where we should end up for an existing Debian package that
			# conflicts with this one and for a non-Debian installation that
			# happens to be in our standard location
		
			if [ A"${lastinstall}" = A${installed} ]
			then
				echo Saving old binaries
				get_old_bins ${installed} $2
			else
				# A previous install should already have saved the binaries
				# we want
				echo Old binaries should already have been saved.
			fi
	
		fi
	fi
else
	# there was no database where we are going to put the new one,
	# but the executables of an older package might be left around
	# and might be needed to do a dump
	
	if [ -d /usr/lib/postgresql/bin -a "$1" != upgrade ]
	then
		get_old_bins unknown
	fi
fi

#DEBHELPER#

exit 0
