#!/bin/bash

. ./param_bash.ini

clear
START "Extract Oracle software" "ikit_3_1"

if [ ! -w /etc/passwd ] && [ "~" == "$ORACLE_HOME" ] ; then
	ERROR "Please run the script as $ORACLE_USER user or root user" "ikit_3_1"
fi 

OracleUserExist
if [ "$?" = "1" ] ; then 
	ERROR "The $ORACLE_HOME user doesn't exist" "ikit_3_1"
	else 
  		ORACLE_USER=`cat /etc/passwd | grep $ORACLE_HOME |tail -1 | awk -F: '{print $1}'` > /dev/null 2>&1
fi

if [ ! -d ${ORACLE_BASE/product} ] ; then
	ERROR "You must create directories in order to extract Oracle software" "ikit_3_1"
fi 
if [ -w /etc/passwd ] ; then  
    su $param_to_su $ORACLE_USER -c "bash extract_oracle_scripts.bash"
else
    bash extract_oracle_scripts.bash
fi
if [ "$?" != "0" ] ; then
    LOG "Failed extracting oracle scripts..."
fi
if [ -d ${ORACLE_HOME} ] ; then
	DONE "Oracle software already exists on the server" "ikit_3_1"
fi 

########################su to oracle###########################################
echo "Oracle RDBMS:"
if [ -w /etc/passwd ] ; then  
	su $param_to_su $ORACLE_USER -c "bash extract_oracle_sw.bash"
else
	bash extract_oracle_sw.bash
fi
if [ "$?" = "0" ] ; then 

##############Fixing oradism Settings##########################################
    if [ "${OS}" = "SunOS" ] ; then
        cd ${ORACLE_HOME}/bin
        chmod 4550 oradism > /dev/null 2>&1
        chmod g+s oradism > /dev/null 2>&1
        chown root:dba oradism > /dev/null 2>&1
    fi
##############Fixing oracle permissions#########################################
    cd ${ORACLE_HOME}/bin
    chmod 6751 oracle > /dev/null 2>&1
###############################################################################
    bash ${ORACLE_BASE}/product/${ORACLE_VER}/root.sh -silent
    if [ "$?" = "1" ] ; then
            INFO "run bash ${ORACLE_BASE}/product/${ORACLE_VER}/root.sh as root user" "ikit_3_1"
    fi
    DONE "Oracle software was extracted" "ikit_3_1"
else
        if [ -d ${ORACLE_BASE}/product/${ORACLE_VER} ] ; then
            mv ${ORACLE_BASE}/product/${ORACLE_VER} ${ORACLE_BASE}/product/${ORACLE_VER}.temp > /dev/null 2>&1 
            rm -rf ${ORACLE_BASE}/product/${ORACLE_VER}.temp > /dev/null 2>&1 & 
        fi
	ERROR "Cannot extract Oracle software" "ikit_3_1"
fi
