#!/bin/bash


. ./param_bash.ini

clear
START "Initialize Rosetta database" "$0"
if [ ! -w /etc/passwd ] && [ "$USER" != "$MAIN_USER" ] ; then
        ERROR "Run the script as $MAIN_USER user or root user" "$0"
fi

MainUserExist
if [ "$?" = "1" ] ; then
        ERROR "$MAIN_USER user does not exist" "$0"
fi

if [ "$MODE" = "0" ] ; then
        echo -n "Enter a slot number ${SOFT_DIR}[${SLOT_NUMBER}] : "
        read ans
        if [ "$ans" != "" ] ; then
                SLOT_NUMBER=$ans
        fi
fi
if [ ! -d $APP_HOME/$SOFT_DIR$SLOT_NUMBER ] ; then
        ERROR "The directory ${APP_HOME}/${SOFT_DIR}$SLOT_NUMBER does not exist on the server" "$0"
fi

if [ "${PROMPT_FOR_ORA_PASS}" = "N" ] ; then
    if [ ~ = "$APP_HOME" ] ; then
        bash check_db_schemas.bash >> $gen_log 2>&1
    else
        su $param_to_su $MAIN_USER -c "bash check_db_schemas.bash" >> $gen_log 2>&1
    fi
    if [ "$?" = "0" ] ; then
        WARN "DB Schemas already exist"
        WARN "Please Confirm whether you want to continue"
        WARN "If you continue, all data will be lost"
        CONFIRM
        if [ "$?" != "0" ] ; then
            CANCEL "Rosetta DB initialization is canceled" "$0"
        fi
    fi
fi

WAIT "Initializing Rosetta database"
if [ "${PROMPT_FOR_ORA_PASS}" = "Y" ] ; then
    if [ ~ = "$APP_HOME" ] ; then
        bash init_db.bash
    else
        su $param_to_su $MAIN_USER -c "bash init_db.bash"
    fi
else
    if [ ~ = "$APP_HOME" ] ; then
        bash init_db.bash >> $gen_log 2>&1
    else
        su $param_to_su $MAIN_USER -c "bash init_db.bash" >> $gen_log 2>&1
    fi
    if [ ~ = "$APP_HOME" ] ; then
        bash check_db_schemas.bash >> $gen_log 2>&1
    else
        su $param_to_su $MAIN_USER -c "bash check_db_schemas.bash" >> $gen_log 2>&1
    fi
fi
if [ "$?" != "0" ] ; then
    ERROR "The Rosetta database initilization has failed" "$0"
else
    DONE "The Rosetta database was initialized" "$0"
fi
