#!/bin/bash
# *** #########################################################################################
# *** Author: 		ABS Laundry Business Solutions / MBU
# *** Version: 		1.0
# *** #########################################################################################
# ***
# *** Script: 		bi_install.sh
# *** Description: 	Installation of Oracle BI
# *** Author: 		ABS Laundry Business Solutions
# *** Version: 		1.0
# *** Remarks:		Run as oracle OS user
# *** 
# *** ### Changelog of script #################################################################
# *** Date        Author    Version 	Change    
# *** 10-Sep-2019 MBU		1.0			Initial version
###############################################################################################

#*** Set variables
export PAKDIR="$(dirname $( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ))"
export TMPDIR=$PAKDIR/tmp
export VARDIR=$PAKDIR/variable
export FIXDIR=$PAKDIR/fixed
. $PAKDIR/install_variables.sh

#*** Init TMP dir
mkdir $TMPDIR

#*** Set addition variable
export BI_CONN=$BI_DBSERVER:$BI_DBPORT:$BI_DBSID

#*** Extract BI infrastructure package and install silently
unzip $VARDIR/fmw_*_infrastructure_Disk1_1of1.zip -d $TMPDIR
cp $FIXDIR/FMW.rsp $TMPDIR/FMW.rsp
echo "ORACLE_HOME=$BI_DIR" >> $TMPDIR/FMW.rsp
find $TMPDIR -iname \*.jar -type f  -print | xargs -I {} $JAVA_HOME/bin/java -d64 -jar {} -silent -responseFile $TMPDIR/FMW.rsp
rm -rf $TMPDIR/*

#*** Extract BI package and install silently
find $VARDIR -iname \*fmw_*_bi_linux64_Disk1\*.zip -type f | sort | xargs -I {} unzip {} -d $TMPDIR
cp $FIXDIR/OBI.rsp $TMPDIR/OBI.rsp
echo "ORACLE_HOME=$BI_DIR" >> $TMPDIR/OBI.rsp
find $TMPDIR -iname \*.bin -type f -exec {} -silent -responseFile $TMPDIR/OBI.rsp  \;
rm -rf $TMPDIR/*

#*** Run RCU util for database schema's
cp $FIXDIR/RCU.rsp $TMPDIR/RCU.rsp
sed -i "/connectString=/c\connectString=${BI_CONN}" $TMPDIR/RCU.rsp
echo "$BI_DBSYSPASS" > $TMPDIR/passwords.txt
for i in {1..9}; do echo "ABSDEV" >> $TMPDIR/passwords.txt ; done 
$BI_DIR/oracle_common/bin/rcu -silent -responseFile $TMPDIR/RCU.rsp < $TMPDIR/passwords.txt
rm -rf $TMPDIR/*

#*** Workaround - Oracle Doc ID 2388947.1
echo 'export JVM_ARGS="-Djava.security.egd=file:///dev/../dev/urandom ${WLST_PROPERTIES} ${JVM_D64} ${UTILS_MEM_ARGS} ${CONFIG_JVM_ARGS}"' >> $BI_DIR/oracle_common/common/bin/setWlstEnv_internal.sh

#*** Run BI domain config util
cp $FIXDIR/CNF.rsp $TMPDIR/CNF.rsp
sed -i "/DOMAINS_DIR=/c\DOMAINS_DIR=${BI_DIR}/user_projects/domains" $TMPDIR/CNF.rsp
sed -i "/ADMIN_PASSWORD=/c\ADMIN_PASSWORD=${BI_SYSPASS}" $TMPDIR/CNF.rsp
sed -i "/ADMIN_CONFIRM_PASSWORD=/c\ADMIN_CONFIRM_PASSWORD=${BI_SYSPASS}" $TMPDIR/CNF.rsp
sed -i "/EXISTING_DB_PASSWORD=/c\EXISTING_DB_PASSWORD=ABSDEV" $TMPDIR/CNF.rsp
echo "CONNECT_STRING=$BI_CONN" >> $TMPDIR/CNF.rsp
$BI_DIR/bi/bin/config.sh -silent -responseFile $TMPDIR/CNF.rsp

#*** Check version of BI for EE feature disabling
if [ $BI_EDITION != EE ] 
then 
	#*** Import BAK file with to disable EE features
	if [ -f $BI_DIR/user_projects/domains/bi/bitools/bin/importarchive.sh ]
	then
		sh $BI_DIR/user_projects/domains/bi/bitools/bin/importarchive.sh ssi $FIXDIR/ssi_12214.bar encryptionpassword=abssys01
	else
		echo "importServiceInstance('$BI_DIR/user_projects/domains/bi','ssi','$FIXDIR/ssi.bar',true,true,false,'ABSDATAWAREHOUSE');" > $TMPDIR/bak.py
		echo "exit();" >> $TMPDIR/bak.py
		$BI_DIR/oracle_common/common/bin/wlst.sh $TMPDIR/bak.py	
	fi

	#*** Revoke VA permissions
	echo "import sys" > $TMPDIR/va.py
	echo "import os" >> $TMPDIR/va.py
	echo "from java.lang import System" >> $TMPDIR/va.py
	echo "import getopt" >> $TMPDIR/va.py
	echo "connect(\"SYS\", \"$BI_SYSPASS\", \"localhost:9500\")" >> $TMPDIR/va.py
	echo "revokeEntitlement(appStripe=\"obi\", principalClass=\"oracle.security.jps.service.policystore.ApplicationRole\",principalName=\"BIContentAuthor\",entitlementName=\"va.author\")" >> $TMPDIR/va.py
	$BI_DIR/oracle_common/common/bin/wlst.sh $TMPDIR/va.py
fi

#*** Create tnsnames.ora
cp $FIXDIR/tnsnames.ora $BI_DIR/user_projects/domains/bi/config/fmwconfig/bienv/core
sed -i "s/SID/${BI_DBSID}/g" $BI_DIR/user_projects/domains/bi/config/fmwconfig/bienv/core/tnsnames.ora
sed -i "s/DBSERVER/${BI_DBSERVER}/g" $BI_DIR/user_projects/domains/bi/config/fmwconfig/bienv/core/tnsnames.ora
sed -i "s/TCPPORT/${BI_DBPORT}/g" $BI_DIR/user_projects/domains/bi/config/fmwconfig/bienv/core/tnsnames.ora
[ -d $BI_DIR/user_projects/domains/bi/bidata/components/core/serviceinstances/ssi/oracledb ] && cp $BI_DIR/user_projects/domains/bi/config/fmwconfig/bienv/core/tnsnames.ora $BI_DIR/user_projects/domains/bi/bidata/components/core/serviceinstances/ssi/oracledb/

#*** Import RDP and catalog
$BI_DIR/user_projects/domains/bi/bitools/bin/datamodel.sh uploadrpd -I $VARDIR/$BI_RPD -SI ssi -U SYS -P $BI_SYSPASS -w abssys01
cp $BI_DIR/user_projects/domains/bi/bidata/service_instances/ssi/metadata/content/catalog/root.atr $BI_DIR/user_projects/domains/bi/bidata/service_instances/ssi/metadata/content/catalog/root.art.org
cp -r $BI_DIR/user_projects/domains/bi/bidata/service_instances/ssi/metadata/content/catalog/root $BI_DIR/user_projects/domains/bi/bidata/service_instances/ssi/metadata/content/catalog/root.org
rm -rf $BI_DIR/user_projects/domains/bi/bidata/service_instances/ssi/metadata/content/catalog/root/shared
unzip $VARDIR/$BI_CATALOG -d $BI_DIR/user_projects/domains/bi/bidata/service_instances/ssi/metadata/content/catalog/root

#*** Import user groups
echo "connect('SYS','$BI_SYSPASS', 't3://localhost:9500');" > $TMPDIR/users.py
echo "domainRuntime();" >> $TMPDIR/users.py
echo "cd('/DomainServices/DomainRuntimeService/DomainConfiguration/bi/SecurityConfiguration/bi/DefaultRealm/myrealm/AuthenticationProviders/DefaultAuthenticator');" >> $TMPDIR/users.py
echo "cmo.importData('DefaultAtn','$FIXDIR/export.ldif', Properties());" >> $TMPDIR/users.py
echo "grantAppRole('obi','BIContentAuthor','weblogic.security.principal.WLSGroupImpl','ABSBIAuthor');" >> $TMPDIR/users.py
echo "grantAppRole('obi','BIConsumer','weblogic.security.principal.WLSGroupImpl','ABSBIConsumer');" >> $TMPDIR/users.py
echo "exit();" >> $TMPDIR/users.py
$BI_DIR/oracle_common/common/bin/wlst.sh $TMPDIR/users.py

#*** Overwrite logo and instanceconfig.xml
cp $FIXDIR/instanceconfig.xml $BI_DIR/user_projects/domains/bi/config/fmwconfig/biconfig/OBIPS/
cp $FIXDIR/$BI_LOGO $TMPDIR/oracle_logo.png
find $BI_DIR/user_projects/domains/bi -name oracle_logo.png -exec cp $TMPDIR/oracle_logo.png {} \;

#*** Stop service
$BI_DIR/user_projects/domains/bi/bitools/bin/stop.sh

#*** Disable Single Sign On
echo "disableBISingleSignOn('$BI_DIR/user_projects/domains/bi');" > $TMPDIR/SSO.py
echo "exit();" >> $TMPDIR/SSO.py
$BI_DIR/oracle_common/common/bin/wlst.sh $TMPDIR/SSO.py

#*** Remove TMPDIR
rm -rf $TMPDIR

#*** Start service
$BI_DIR/user_projects/domains/bi/bitools/bin/start.sh