#!/bin/bash
# *** #########################################################################################
# *** Author: 		ABS Laundry Business Solutions / MBU
# *** Version: 		1.0
# *** #########################################################################################
# ***
# *** Script: 		bi_root_pre.sh
# *** Description: 	Root action before Oracle BI install
# *** Author: 		ABS Laundry Business Solutions
# *** Version: 		1.0
# *** Remarks:		Run as root user
# *** 
# *** ### Changelog of script #################################################################
# *** Date        Author    Version 	Change    
# *** 10-Sep-2019 MBU		1.0			Initial version
###############################################################################################

# Creating Oracle OS groups
/usr/sbin/groupadd oinstall
/usr/sbin/groupadd dba

# Create required Oracle user if necassary
if getent passwd oracle > /dev/null 2>&1; then
    echo "The Oracle user already exists"
	
	# Add user to group
	/usr/sbin/usermod -a -G oinstall,dba oracle
else
	echo "Creating the Oracle user"

	# Create Oracle user with password
	/usr/sbin/useradd -G oinstall,dba oracle
	echo $BI_OSPASS | passwd oracle --stdin								
fi

# Install common packages
yum install -y rsync zip unzip ftp

# Install packages for database and BI
yum install -y binutils compat-libcap1 compat-libstdc++-33 compat-libstdc++-33.i686 gcc gcc-c++ glibc glibc.i686 glibc-devel libaio libaio-devel libgcc libgcc.i686 libstdc++
yum install -y libstdc++.i686 libstdc++-devel ksh make sysstat numactl numactl-devel motif motif-devel compat-libstdc++-33 smartmontools psmisc gtk2-devel-2.24.31-1.el7.i686

# Installing JAVA required for BI and setting JAVA_HOME
JAVAINSTALLER=$(find $VARDIR -iname jdk\*linux-x64.rpm -type f | sort -r | head -1)
chmod 775 $JAVAINSTALLER
yum install -y $JAVAINSTALLER
export JAVA_HOME=$(readlink -f /usr/java/latest)
echo "" >> /home/oracle/.bash_profile
echo "# Set JAVA variables" >> /home/oracle/.bash_profile
echo "JAVA_HOME=$JAVA_HOME" >> /home/oracle/.bash_profile
echo "export JAVA_HOME" >> /home/oracle/.bash_profile

# Create Oracle base and inventory if required
mkdir -p $BI_DIR
chown -R oracle:oinstall $BI_DIR
if [ ! -f /etc/oraInst.loc ]
then
	export BI_ORAINV=$(dirname $BI_DIR)/oraInventory
	mkdir $BI_ORAINV
	chown -R oracle:oinstall $BI_ORAINV
	echo "inventory_loc=$BI_ORAINV" > /etc/oraInst.loc
	echo "inst_group=oinstall" >> /etc/oraInst.loc
fi

# Change DIR owner to Oracle
chown -R oracle:oinstall $PAKDIR

# Open TCP ports for database and for BI
firewall-cmd --zone=public --add-port=9500/tcp --permanent
firewall-cmd --zone=public --add-port=9502/tcp --permanent
firewall-cmd --zone=public --add-port=9503/tcp --permanent
firewall-cmd --reload