#!/bin/bash

. ./param_bash.ini

echo "$$" > ${IKIT_DIR}/tmp/.dbcreation.pid

cd $IKIT_FTP_DIR/$COPY_DB_SCRIPT

csh -f db_target.csh -mode=scratch -t_sid=$ORACLE_SID -s_dir=$IKIT_FTP_DIR/$EXLDB -t_server=$SERVER_NAME -t_dir=$IKIT_DB_DATA_DIR/oradata/$ORACLE_SID -sys_dir=$IKIT_DB_SYS_DIR/oradata/$ORACLE_SID -t_arc_dir=$IKIT_DB_ARCH_DIR/oradata/$ORACLE_SID -batch >> $gen_log
if [ "$?" != "0" ] ; then
    cp -p $IKIT_FTP_DIR/$COPY_DB_SCRIPT/log/db_target.log $IKIT_DIR/log
    for disk in ${IKIT_DB_ARCH_DIR} ${IKIT_DB_SYS_DIR} ${IKIT_DB_DATA_DIR}
    do
        if [ -d ${disk}/oradata/$ORACLE_SID ] ; then
            LOG "Removing ${disk}/oradata/$ORACLE_SID"
       	    rm -rf ${disk}/oradata/$ORACLE_SID
        fi
    done
    LOG "Removing $ORACLE_BASE/admin/$ORACLE_SID"
    rm -rf $ORACLE_BASE/admin/$ORACLE_SID
    LOG "Removing $ORACLE_HOME/dbs/spfile${ORACLE_SID}.ora"
    rm -f $ORACLE_HOME/dbs/spfile${ORACLE_SID}.ora
    LOG "Removing $ORACLE_HOME/dbs/orapw${ORACLE_SID}"
    rm -f $ORACLE_HOME/dbs/orapw${ORACLE_SID}
    LOG "Removing $ORACLE_HOME/dbs/hc_${ORACLE_SID}.dat"
    rm -f $ORACLE_HOME/dbs/hc_${ORACLE_SID}.dat
    LOG "Removing $ORACLE_HOME/dbs/peshm_${ORACLE_SID}_0"
    rm -f $ORACLE_HOME/dbs/peshm_${ORACLE_SID}_0
    ORACLE_SID_UPPER=`echo $ORACLE_SID | tr "[a-z]" "[A-Z]"`
    LOG "Removing $ORACLE_HOME/dbs/lk${ORACLE_SID_UPPER}"
    rm -f $ORACLE_HOME/dbs/lk${ORACLE_SID_UPPER}
    rm -rf ${IKIT_DIR}/tmp/.dbcreation.pid
    echo "The Rosetta installation automated message." |  $MAIL_UTIL -s "Rosetta installation - the Oracle DataBase installaion has failed." $MAIL
else
    cp -p $IKIT_FTP_DIR/$COPY_DB_SCRIPT/log/db_target.log $IKIT_DIR/log
    cd $IKIT_DIR
    $PS | grep -v grep | grep ora_pmon_${ORACLE_SID} > /dev/null 2>&1
    if [ "$?" != "0" ] ; then
	csh -f ora_startup "${ORACLE_SID}" "${ORACLE_HOME}" "${PS}" >> $gen_log
    fi
    if [ "$?" != "0" ] ; then
	rm -rf ${IKIT_DIR}/tmp/.dbcreation.pid
	echo "The Rosetta installation automated message." |  $MAIL_UTIL -s "Rosetta installation - The database was installed but FAILED to start, please check the logfile" $MAIL
    fi

    cat $IKIT_DIR/header.dat.tml | \
    sed -e "s:@EXL_DIR::" | \
    sed -e "s:@ORACLE_SID:$ORACLE_SID:" \
    > $IKIT_DIR/header.dat
    Update_params "ORACLE_SID" "${ORACLE_SID}"
    if [ ! -f $ORATAB ] ; then
	echo "$ORACLE_SID:$ORACLE_HOME:Y" > $ORATAB
        chown $ORACLE_USER:$ORACLE_GROUP $ORATAB
    else
        grep "$ORACLE_SID:$ORACLE_HOME" $ORATAB > /dev/null 2>&1
        if [ "$?" != "0" ] ; then
            echo "$ORACLE_SID:$ORACLE_HOME:Y" >> $ORATAB
        fi
    fi
    if [ ! -f $ORAINST ] ; then
        echo "inventory_loc=$ORACLE_HOME/inventory" > $ORAINST
        echo "inst_group=dba" >> $ORAINST
    else
        grep "inventory_loc=$ORACLE_HOME/inventory" $ORAINST > /dev/null 2>&1
        if [ "$?" != "0" ] ; then
            echo "inventory_loc=$ORACLE_HOME/inventory" > $ORAINST
            echo "inst_group=dba" >> $ORAINST
        fi
    fi
    chown $ORACLE_USER:$ORACLE_GROUP $ORAINST
    cat ${ORACLE_HOME}/.cshrc | sed -e "s@setenv  ORACLE_SID .*@setenv  ORACLE_SID ${ORACLE_SID}@" > ${ORACLE_HOME}/.cshrc.temp
    cp -p ${ORACLE_HOME}/.cshrc ${ORACLE_HOME}/.cshrc.ikit > /dev/null 2>&1
    mv ${ORACLE_HOME}/.cshrc.temp ${ORACLE_HOME}/.cshrc > /dev/null 2>&1

    export ORACLE_UNQNAME=${ORACLE_SID}
    LOG "DB is up and Running, installing DBConsole"
    bash em.bash >> $gen_log 2>&1
    LOG "Installed DBConsole with $? exit code, fixing time zone"
    emctl status dbconsole
    LOG "Installed DBConsole with $? exit code"
    bash updateTZ.bash "$DB_USER/$DB_PASSWD" ${ORACLE_HOME} >> $gen_log 2>&1
    LOG "time zone was updated with exit code $?"
    ${ORACLE_HOME}/oui/bin/runInstaller -silent -AttachHome -invPtrLoc /etc/oraInst.loc ORACLE_HOME="${ORACLE_HOME}" ORACLE_HOME_NAME=OraDb11g_home1 > /dev/null 2>&1
    ${ORACLE_HOME}/OPatch/opatch lsinventory | grep succeeded > /dev/null 2>&1
    LOG "OPATCH inventory status is $?"

    rm -rf ${IKIT_DIR}/tmp/.dbcreation.pid
    bash update_status.bash 3 ikit_3.dat
    echo "The Rosetta installation automated message." |  $MAIL_UTIL -s "Rosetta installation - The database was installed and startup." $MAIL
fi
