#!/bin/bash
#
# This script configures WebLogic to use Log4j v2 for logging.

# Abort immediately on any error.
set -o errexit

# *** Parse Parameters ***

# Arg 1: the path of the WebLogic domain to install to.
if [ -n "$1" ]; then
    domain_dir="$1"
else
    echo "Usage: $0 domain_dir"
    exit 1
fi

# *** Other configuration ***

# The path to the domain source files (e.g., JARs in source control). Assumed to be the
# directory above this script.
source_dir=$(dirname "$0")/..

# The version of log4j2 that we are installing.
log4j_version=2.3

# *** Now on to business ***

echo "*** Configuring WebLogic to use Log4j v2 for logging. ***"

# Get WL_HOME. Note: this script changes the working directory.
source $domain_dir/bin/setDomainEnv.sh

echo "Copying the necessary Log4j files to the WebLogic domain's lib directory..."
# Don't preserve mode to avoid incorrectly setting executable bit.
cp --no-preserve=mode \
    "$source_dir/lib/log4j-core-$log4j_version.jar" \
    "$source_dir/lib/log4j-api-$log4j_version.jar" \
    "$source_dir/lib/log4j-1.2-api-$log4j_version.jar" \
    "$source_dir/lib/log4j-web-$log4j_version.jar" \
    "$source_dir/lib/org.osgi-3.0.0.jar" \
    "$WL_HOME/server/lib/wllog4j.jar" \
    --target-directory="$DOMAIN_HOME/lib"

echo "Copying the Log4j configuration file to the domain's config directory..."
# Don't preserve mode to avoid incorrectly setting executable bit.
cp --no-preserve=mode "$source_dir/config/log4j2.xml" \
    --target-directory "$DOMAIN_HOME/config"

echo "Copying the setUserOverrides script..."
# Note: WebLogic automatically sources this script if it exists.
# Don't preserve mode to avoid incorrectly setting executable bit.
cp --no-preserve=mode "$source_dir/bin/setUserOverrides.sh" \
    --target-directory "$DOMAIN_HOME/bin"

echo "Done!"
