#!/bin/sh
USER_INF=`whoami`
    if [ $USER_INF != "pgsql" ]; then
        echo "Please use 'pgsql' account.[current $USER_INF]"
        exit 1
    fi
    
CNT=`find . -maxdepth 1 -name "rcdb_schema.sql" | wc -l`
    if [ $CNT -eq 0 ]; then
        echo "Not Found Schema File."
        exit 1
    fi

#clean
rm -f err > /dev/null 2>&1

echo -n "1. init db "
initdb --locale=C > /dev/null 2>err
    if [ $? -ne 0 ]; then
    	echo "...[ERR]"
    	cat err
    	exit 1
    fi
echo "...[OK]"

echo -n "2. start RCDB "
pg_ctl start > /dev/null 2>err
    if [ $? -ne 0 ]; then
    	echo "...[ERR]"
    	cat err
    	exit 1
    fi
echo -n "."
while [ 1 ]
do
    echo -n "."
    sleep 1
    if [  -n ~/data/postmaster.pid  ]; then
        break
    fi
done
echo ".[OK]"

echo -n "3. create RCDB schema "
psql postgres < rcdb_schema.sql > /dev/null 2>err
    if [ $? -ne 0 ]; then
    	echo "...[ERR]"
    	cat err
    	exit 1
    fi
echo "...[OK]"

echo -n "4. stop RCDB "
pg_ctl stop -m f > /dev/null 2>err
    if [ $? -ne 0 ]; then
    	echo "...[ERR]"
    	cat err
    	exit 1
    fi
echo "...[OK]"
