#!/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
#
#
#
#
#
#
# Drops and remakes the scalix DB.
#
# Usage: sxpsql-recreatedb
#

# Pass "" to prevent implicit passing of $*
. /opt/scalix-postgres/bin/common-helpers.sh ""

/etc/init.d/scalix-tomcat status | grep 'is running' >/dev/null 2>/dev/null
if [ $? = "0" ]; then
    TOMCAT_RUNNING=1
else
    TOMCAT_RUNNING=0
fi

if [ $TOMCAT_RUNNING -eq 1 ]; then
    /etc/init.d/scalix-tomcat stop
fi

/etc/init.d/scalix-postgres status | grep 'is running' >/dev/null 2>/dev/null
if [ $? = "0" ]; then
    POSTGRES_RUNNING=1
else
    POSTGRES_RUNNING=0
fi

if [ $POSTGRES_RUNNING -ne 1 ]; then
    /etc/init.d/scalix-postgres start
fi

su - -c "$PGENGINE/dropdb -h $PSQL_DIR -p $PSQL_PORT scalix" postgres || exit $?
su - -c "$PGENGINE/createdb -h $PSQL_DIR -p $PSQL_PORT -E UNICODE scalix" postgres || exit $?

if [ $TOMCAT_RUNNING -eq 1 ]; then
    /etc/init.d/scalix-tomcat start
fi
