#!/bin/sh
#
#
#    Scalix Public License
#
#
#    The contents of this file are subject to the Scalix Public License
#    Version 1.1 ("License"); you may not use this file except in
#    compliance with the License. You may obtain a copy of the License at
#
# 
#    http://www.scalix.com/community/licensing 
#
# 
#    Software distributed under the License is distributed on an "AS IS"
#    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
#    the License for the specific language governing rights and limitations
#    under the License.
#
# 
#    The Original Code is Scalix Server. 
#    The Initial Developer of the Original Code is Scalix Corporation. 
#    Copyright (C) 2006 Scalix Corporation. 
#    All Rights Reserved.
#    Additional Copyrights may apply - see the notice in each file
#
#
#
#
#

INST_DIR=$(/opt/scalix-postgres/bin/sxpsql-get-inst-dir "")
# create the default instance:
if [ ! -d $INST_DIR ] ; then
    mkdir -p $INST_DIR
    chown postgres:postgres $INST_DIR
fi
# backup the old /var/opt/scalix-postgres directory
if [ -d /var/opt/scalix-postgres ] ; then
	if [ ! -d /var/opt/scalix-postgres.backup ] ; then
		mv /var/opt/scalix-postgres /var/opt/scalix-postgres.backup
	else
		dircount=1
		while [ -d /var/opt/scalix-postgres.backup.$dircount ]
		do
			dircount=$(expr $dircount + 1)
		done
		mv /var/opt/scalix-postgres /var/opt/scalix-postgres.backup.$dircount
	fi
fi

case "$1" in
    configure | upgrade )
        # initialize the database before starting it if not already done:
        /opt/scalix-postgres/bin/sxpsql-init
        encoding=`/opt/scalix-postgres/bin/sxpsql-checkencoding`
        if [ ! "UNICODE" = "$encoding" ]; then
            /opt/scalix-postgres/bin/sxpsql-recreatedb
        fi
    ;;

    abort-upgrade | abort-remove | abort-deconfigure )
    ;;
esac


#DEBHELPER#
