#!/bin/bash
#
# Copyright (c) 2019, 2020 Oracle and/or its affiliates.
#
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
#
# Description: This script is used to Create Sites Domain, Execute RCU, Configwizard, and silent SitesConfig process. It will start and stop managed server during SitesConfig process, and finally restart Admin server.
#

########### SIGINT handler ############
function _int() {
   echo "Stopping container."
   echo "SIGINT received, shutting down Admin Server!"
   $DOMAIN_HOME/bin/stopWebLogic.sh
   exit;
}

########### SIGTERM handler ############
function _term() {
   echo "Stopping container."
   echo "SIGTERM received, shutting down Admin Server!"
   $DOMAIN_HOME/bin/stopWebLogic.sh
   exit;
}

########### SIGKILL handler ############
function _kill() {
   echo "SIGKILL received, shutting down the server!"
   kill -9 $childPID
}

#######Random Password Generation########
function rand_pwd(){
    while true; do
         s=$(cat /dev/urandom | tr -dc "A-Za-z0-9" | fold -w 8 | head -n 1)
         if [[ ${#s} -ge 8 && "$s" == *[A-Z]* && "$s" == *[a-z]* && "$s" == *[0-9]*  ]]
         then
             break
         else
             echo "Password does not Match the criteria, re-generating..." >&2
         fi
    done
    echo "${s}" 
}

INSTALL_START=$(date '+%s')

# Set SIGINT handler
trap _int SIGINT

# Set SIGTERM handler
trap _term SIGTERM

# Set SIGKILL handler
trap _kill SIGKILL


# Create an WebCenter Sites domain
# set environments needed for the script to work
ADD_DOMAIN=1

if [ -z ${DOMAIN_NAME} ]
then
	DOMAIN_NAME=wcsites_domain
	echo ""
	echo " Setting DOMAIN_NAME to $DOMAIN_NAME"
	echo ""
fi

if [ -z ${ADMIN_NAME} ]
then
        ADMIN_NAME=AdminServer
        echo ""
        echo " Setting ADMIN_NAME to $ADMIN_NAME"
        echo ""
fi


export DOMAIN_HOME="${DOMAIN_ROOT}/${DOMAIN_NAME}"

if [ ! -f ${DOMAIN_HOME}/servers/${ADMIN_NAME}/logs/${ADMIN_NAME}.log ]; then
	ADD_DOMAIN=0
fi

# Create Domain only if 1st execution
if [ $ADD_DOMAIN -eq 0 ];
then
   echo "Creating Domain 1st execution"

	#Check on required parameters
	PARAMS=true

	DB_SCHEMA_PASSWORD_AUTOGENERATED=false
	ADMIN_PASSWORD_AUTOGENERATED=false
	SITES_ADMIN_PASSWORD_AUTOGENERATED=false
	SITES_APP_PASSWORD_AUTOGENERATED=false
	SITES_SS_PASSWORD_AUTOGENERATED=false

	if [ -z $DOCKER_HOST ]; then
		echo "DOCKER_HOST not set."
		PARAMS=false
	fi

	if [ -z $DB_CONNECTSTRING ]; then
		echo "DB_CONNECTSTRING not set."
		PARAMS=false
	fi

	if [ -z $DB_USER ]; then
		echo "DB_USER not set."
		PARAMS=false
	fi

	if [ -z $DB_PASSWORD ]; then
		echo "DB_PASSWORD not set."
		PARAMS=false
	fi

	if [ -z $RCU_PREFIX ]; then
		echo "RCU_PREFIX not set."
		PARAMS=false
	fi

	if [ $PARAMS == "false" ]; then
		echo "All above required parameters not set in wcsitesadminserver.env.list"
		exit;    
	fi

	if [ -z ${SITES_SERVER_NAME} ]
	then
		SITES_SERVER_NAME=wcsites_server
		echo ""
		echo " Setting SITES_SERVER_NAME to wcsites_server"
		echo ""
	fi

	if [ -z ${MACHINE_NAME} ]
	then
		MACHINE_NAME=wcsites_machine
		echo ""
		echo " Setting MACHINE_NAME to wcsites_machine"
		echo ""
	fi

	if [ -z ${CLUSTER_NAME} ]
	then
		CLUSTER_NAME=wcsites_cluster
		echo ""
		echo " Setting CLUSTER_NAME to wcsites_cluster"
		echo ""
	fi

	if [ -z ${ADMIN_USERNAME} ]
	then
		ADMIN_USERNAME=weblogic
		echo ""
		echo " Setting ADMIN_USERNAME to weblogic"
		echo ""
	fi

	if [ -z ${SITES_ADMIN_USERNAME} ]
	then
		SITES_ADMIN_USERNAME=ContentServer
		echo ""
		echo " Setting SITES_ADMIN_USERNAME to ContentServer"
		echo ""
	fi

	if [ -z ${SITES_APP_USERNAME} ]
	then
		SITES_APP_USERNAME=fwadmin
		echo ""
		echo " Setting SITES_APP_USERNAME to fwadmin"
		echo ""
	fi

	if [ -z ${SITES_SS_USERNAME} ]
	then
		SITES_SS_USERNAME=SatelliteServer
		echo ""
		echo " Setting SITES_SS_USERNAME to SatelliteServer"
		echo ""
	fi

	if [ -z ${SAMPLES} ]
	then
		SAMPLES=false
		echo ""
		echo " Setting samples to false"
		echo ""
	fi

	if [ -z ${ADMIN_PORT} ]
	then
		ADMIN_PORT=7001
		echo ""
		echo " Setting ADMIN_PORT to 7001"
		echo ""
	fi

	if [ -z ${WCSITES_MANAGED_PORT} ]
	then
		WCSITES_MANAGED_PORT=7002
		echo ""
		echo " Setting WCSITES_MANAGED_PORT to 7002"
		echo ""
	fi

	if [ -z ${ADMIN_PASSWORD} ]
	then
		# Auto generate Oracle WebLogic Server admin password
		ADMIN_PASSWORD=$(rand_pwd)
		ADMIN_PASSWORD_AUTOGENERATED=true
		echo ""
		echo "    Oracle WebLogic Server Password Auto Generated :"
		echo ""
		echo "    ----> Username: $ADMIN_USERNAME ----> Password: $ADMIN_PASSWORD"
		echo ""
	fi;

	if [ -z ${DB_SCHEMA_PASSWORD} ]
	then
		# Auto generate Oracle Database Schema password
		temp_pwd=$(rand_pwd)
		#Password should not start with a number for database
		f_str=`echo $temp_pwd|cut -c1|tr [0-9] [A-Z]`
		s_str=`echo $temp_pwd|cut -c2-`
		DB_SCHEMA_PASSWORD=${f_str}${s_str}
		DB_SCHEMA_PASSWORD_AUTOGENERATED=true
		echo ""
		echo "    Database Schema password Auto Generated :"
		echo ""
		echo "    ----> Database schema password: $DB_SCHEMA_PASSWORD"
		echo ""
	fi

	if [ -z ${SITES_ADMIN_PASSWORD} ]
	then
		# Auto generate Oracle WebCenter Sites Administrator password
		SITES_ADMIN_PASSWORD=$(rand_pwd)
		SITES_ADMIN_PASSWORD_AUTOGENERATED=true
		echo ""
		echo "    Oracle WebCenter Sites Administrator Password Auto Generated :"
		echo ""
		echo "    ----> Username: $SITES_ADMIN_USERNAME ----> Password: $SITES_ADMIN_PASSWORD"
		echo ""
	fi;

	if [ -z ${SITES_APP_PASSWORD} ]
	then
		# Auto generate Oracle WebCenter Sites Application password
		SITES_APP_PASSWORD=$(rand_pwd)
		SITES_APP_PASSWORD_AUTOGENERATED=true
		echo ""
		echo "    Oracle WebCenter Sites Application Password Auto Generated :"
		echo ""
		echo "    ----> Username: $SITES_APP_USERNAME ----> Password: $SITES_APP_PASSWORD"
		echo ""
	fi;

	if [ -z ${SITES_SS_PASSWORD} ]
	then
		# Auto generate Oracle WebCenter Sites SatelliteServer password
		SITES_SS_PASSWORD=$(rand_pwd)
		SITES_SS_PASSWORD_AUTOGENERATED=true
		echo ""
		echo "    Oracle WebCenter Sites SatelliteServer Password Auto Generated :"
		echo ""
		echo "    ----> Username: $SITES_SS_USERNAME ----> Password: $SITES_SS_PASSWORD"
		echo ""
	fi;

	# These values can be parameterized later on. Hardcoding for now.
	export DOCKER_HOST=$DOCKER_HOST

	#Database Parameters
	export DB_USER=$DB_USER
	export DB_PASSWORD=$DB_PASSWORD
	export DB_CONNECTSTRING=$DB_CONNECTSTRING

	#Installer Parameters
	export RCU_PREFIX=$RCU_PREFIX
	export SAMPLES=$SAMPLES
	export DOMAIN_NAME=$DOMAIN_NAME
	export SITES_SERVER_NAME=$SITES_SERVER_NAME
	export ADMIN_USERNAME=$ADMIN_USERNAME
	#export ADMIN_PORT=7001
	#export WCSITES_MANAGED_PORT=7002
	export ADMIN_SSL_PORT=9001
	export WCSITES_SSL_PORT=9002
	ORACLE_HOME=/u01/oracle
	WORK_DIR=/u01/oracle/user_projects/wcs-wls-docker-install/work

	#Hostname Parameters
	export WCSITES_ADMIN_HOSTNAME=$(sed -r 's/\./\\\./g' <<< $(hostname -I))

	#dos2unix $SITES_CONTAINER_SCRIPTS/*.*

	#--------------------------------------------------------------------------------------------
	cd /u01/wcs-wls-docker-install
	#find bootstrap.properties -type f -exec dos2unix {} {} \;

	sed -i 's,^\(script.rcu.prefix=\).*,\1'$RCU_PREFIX',' bootstrap.properties
	sed -i 's,^\(script.java.path=\).*,\1'$JAVA_HOME',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.hostname=\).*,\1'$WCSITES_ADMIN_HOSTNAME',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.portnumber=\).*,\1'$WCSITES_MANAGED_PORT',' bootstrap.properties
	sed -i 's,^\(script.db.user=\).*,\1'$DB_USER',' bootstrap.properties
	sed -i 's,^\(script.db.password=\).*,\1'$DB_PASSWORD',' bootstrap.properties
	sed -i 's,^\(script.db.schema.password=\).*,\1'$DB_SCHEMA_PASSWORD',' bootstrap.properties
	sed -i 's,^\(script.db.connectstring=\).*,\1'$DB_CONNECTSTRING',' bootstrap.properties
	sed -i 's,^\(script.oracle.home=\).*,\1'$ORACLE_HOME',' bootstrap.properties
	sed -i 's,^\(script.work.dir=\).*,\1'$WORK_DIR',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.examples.avisports=\).*,\1'$SAMPLES',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.examples.fsii=\).*,\1'$SAMPLES',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.examples.Samples=\).*,\1'$SAMPLES',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.examples.blogs=\).*,\1'$SAMPLES',' bootstrap.properties
	sed -i 's,^\(script.wcsites.binaries.install.with.examples=\).*,\1'$SAMPLES',' bootstrap.properties
	sed -i 's,^\(script.oracle.domain=\).*,\1'$DOMAIN_NAME',' bootstrap.properties
	sed -i 's,^\(script.server.name=\).*,\1'$SITES_SERVER_NAME',' bootstrap.properties
	sed -i 's,^\(script.machine.name=\).*,\1'$MACHINE_NAME',' bootstrap.properties
	sed -i 's,^\(script.cluster.name=\).*,\1'$CLUSTER_NAME',' bootstrap.properties
	sed -i 's,^\(script.admin.server.username=\).*,\1'$ADMIN_USERNAME',' bootstrap.properties
	sed -i 's,^\(script.admin.server.password=\).*,\1'$ADMIN_PASSWORD',' bootstrap.properties
	sed -i 's,^\(script.admin.server.port=\).*,\1'$ADMIN_PORT',' bootstrap.properties
	sed -i 's,^\(script.admin.server.ssl.port=\).*,\1'$ADMIN_SSL_PORT',' bootstrap.properties
	sed -i 's,^\(script.sites.server.ssl.port=\).*,\1'$WCSITES_SSL_PORT',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.system.admin.user=\).*,\1'$SITES_ADMIN_USERNAME',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.system.admin.password=\).*,\1'$SITES_ADMIN_PASSWORD',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.app.user=\).*,\1'$SITES_APP_USERNAME',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.app.password=\).*,\1'$SITES_APP_PASSWORD',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.satellite.user=\).*,\1'$SITES_SS_USERNAME',' bootstrap.properties
	sed -i 's,^\(script.oracle.wcsites.satellite.password=\).*,\1'$SITES_SS_PASSWORD',' bootstrap.properties

	#--------------------------------------------------------------------------------------------
	#RCU + ConfigWizard + Sites Configuration
	#Groovy files are responsible for exexuting RCU + ConfigWizard + Sites Configuration for Sites.
	#Source files for the same are located at ./OracleWebCenterSites/dockerfiles/12.2.1.3/wcs-wls-docker-install/src/
	java -jar wcs-wls-docker-install.jar

	#--------------------------------------------------------------------------------------------

	if [ -e $WORK_DIR/WCSites_Config_Setup.suc ] 
	then
		echo ""	
		echo "Sites installation is successfull"	
	else
		echo ""	
		echo "Sites installation has failed. Please check Admin Container log for details"
		exit
	fi
		
	#
	# Export Domain Home/Root
	#=========================
	export DOMAIN_NAME=$DOMAIN_NAME
	export DOMAIN_ROOT="/u01/oracle/user_projects/domains"
	export DOMAIN_HOME="${DOMAIN_ROOT}/${DOMAIN_NAME}"

	#--------------------------------------------------------------------------------------------
	echo "Replacement started."

	sh $SITES_CONTAINER_SCRIPTS/replaceSitesTokens.sh

	rm $SITES_CONTAINER_SCRIPTS/replaceSitesTokens.sh

	echo "Replacement done successfully."
	#--------------------------------------------------------------------------------------------

	echo ""
	echo ""
	echo "Note down Auto Generated Sites, WebLogic Server and Database Schema passwords:"
	echo ""
	if [ $DB_SCHEMA_PASSWORD_AUTOGENERATED == "true" ]; then
		echo "    ----> Oracle Database Schema Credential:	Password: $DB_SCHEMA_PASSWORD"    
	fi

	if [ $ADMIN_PASSWORD_AUTOGENERATED == "true" ]; then
		echo "    ----> Oracle WebLogic Server Credential:	Username: $ADMIN_USERNAME	Password: $ADMIN_PASSWORD"
	fi

	if [ $SITES_ADMIN_PASSWORD_AUTOGENERATED == "true" ]; then
		echo "    ----> Oracle WebCenter Sites Administrator Credential:	Username: $SITES_ADMIN_USERNAME	Password: $SITES_ADMIN_PASSWORD"
	fi

	if [ $SITES_APP_PASSWORD_AUTOGENERATED == "true" ]; then
		echo "    ----> Oracle WebCenter Sites Application Credential:	Username: $SITES_APP_USERNAME	Password: $SITES_APP_PASSWORD"
	fi

	if [ $SITES_SS_PASSWORD_AUTOGENERATED == "true" ]; then
		echo "    ----> Oracle WebCenter Sites SatelliteServer Credential:	Username: $SITES_SS_USERNAME Password: $SITES_SS_PASSWORD"
	fi

	echo ""
	echo "Admin Server started, ready to start Managed Servers"
	echo "    ----> $SITES_CONTAINER_SCRIPTS/startSitesServer.sh $SITES_SERVER_NAME"
	echo ""
	
	INSTALL_END=$(date '+%s')
	INSTALL_ELAPSED=`expr $INSTALL_END - $INSTALL_START`

	echo "Sites Installation completed in $INSTALL_ELAPSED seconds."

	echo "Now wait for Admin Server to start before starting Sites Managed Server."

	echo "---------------------------------------------------------"
	echo ""
fi

echo "Starting the Admin Server"
echo "=========================="

# Now we start the Admin server in this container... 
echo "tailing ${DOMAIN_HOME}/servers/${ADMIN_NAME}/logs/weblogic.out"
${DOMAIN_HOME}/startWebLogic.sh > ${DOMAIN_HOME}/servers/${ADMIN_NAME}/logs/weblogic.out 2>&1 &

# Tail Admin Server logs... 
tail -900f ${DOMAIN_HOME}/servers/${ADMIN_NAME}/logs/weblogic.out &

childPID=$!
wait $childPID
