#!/bin/bash
# Copyright (c) 2019, 2020, Oracle and/or its affiliates.
#
# This script is run by rpm after the rpm is installed.
# It should do any special setup that needs to be done for the webservers
# for an install or an upgrade.
# Usage: postinstall install_flag
# Where install_flag is a number that tells you if this is a fresh installation
# or an upgrade. 
# install_flag == 1 - Initial install
# install_flag == 2 - Upgrade

#
# Exit codes
#
OK=0
FAIL=1

#
# Check we have the necessary arguments.
# 

if [ $# -lt 1 -o $# -gt 1 ]
then
    echo "Usage: $0 install_flag"
    exit $FAIL
fi

INSTALL_FLAG="$1"
UPGRADE="false"
if [ ${INSTALL_FLAG} -eq 2 ]
then
    UPGRADE="true"
elif [ ${INSTALL_FLAG} -ne 1 ]
then
    echo "Invalid install_flag: ${INSTALL_FLAG}"
    exit $FAIL
fi

if [ "${UPGRADE}" == "true" ]
then
    # Recover any webapp sessions files that have been saved
    sessions_file="SESSIONS.ser"
    if [ -f "${TTA_TMPDIR}/sgd_${sessions_file}" ]
    then
        echo "Restoring workspace tomcat sessions"
        mkdir -p "${TOMCAT_DIR}/work/Catalina/localhost/sgd"
        mv "${TTA_TMPDIR}/sgd_${sessions_file}" \
            "${TOMCAT_DIR}/work/Catalina/localhost/sgd/${sessions_file}"
        chown -R ttaserv:ttaserv "${TOMCAT_DIR}/work/Catalina"
        chmod -R 700 "${TOMCAT_DIR}/work/Catalina"
    fi

    # Check whether the Include of httpd-sgd.conf is already present.
    # If not, add it in.
    includeStr="Include conf/extra/httpd-sgd.conf"
    apacheConf="${APACHE_DIR}/conf/httpd.conf"
    includePresent="$(grep "${includeStr}" "${apacheConf}")"
    if [ -z "${includePresent}" ]
    then
        echo "${includeStr}" >> "${apacheConf}"
    fi

    # Add the SGD block inside the VirtualHost
    ${INSTDIR}/bin/scripts/install_scripts/rpm_scripts/webserver/insert-into-virtualhost
fi

# Configure the AJP Secret only if requiredSecret set to false in server.xml

APACHE_WORKERS_PROPERTIES="${APACHE_DIR}/conf/workers.properties"
TOMCAT_SERVER_XML="${TOMCAT_DIR}/conf/server.xml"

AJP_SECRET_CMD="openssl rand -base64 10 | sed \"s|[\/=,]||g\""
AJP_SECRET=$(eval "${AJP_SECRET_CMD}")
AJP_SECRETREQUIRED_FALSE="secretRequired=\"false\""
AJP_SECRETREQUIRED_TRUE="secretRequired=\"true\""
AJP_SECRET_PASSWORD="secret=\"${AJP_SECRET}\""

AJP_SECRETREQUIRED_NAME="secretRequired"
AJP_SECRET_NAME="secret="

##########################
# Change Tomcat server.xml
# - Always set the secretRequired attribute value to "true"
# - Always configure a new AJP secret

sed -i "s/\(${AJP_SECRETREQUIRED_NAME}=\)[^ ]*\( .*\)/\1\"true\"\2/" ${TOMCAT_SERVER_XML}

# If this is a new installation, there will be no secret by default, so add it
if (grep -q ${AJP_SECRET_NAME} ${TOMCAT_SERVER_XML})
then
    # Find the secret attribute and set to a new AJP_SECRET
    sed -i "s/\(${AJP_SECRET_NAME}\)[^ ]*\( .*\)/\1\"${AJP_SECRET}\"\2/" ${TOMCAT_SERVER_XML}
else
    # Insert the secret attribute before the secretRequired attribute
    sed -i "s/${AJP_SECRETREQUIRED_TRUE}/${AJP_SECRET_PASSWORD} ${AJP_SECRETREQUIRED_TRUE}/" ${TOMCAT_SERVER_XML}
fi

# Add or update the secret for the Apache HTTPD AJP workers.properties
#
# Args: $1 - Worker property to update the secret for
#

worker_properties_add_secret()
{
    workers_property_host="$1.host=127.0.0.1"
    workers_property_secret_name="$1.secret="
    workers_property_secret="$1.secret=${AJP_SECRET}"

    # If this is a new installation, there will be no secret by default, so add it
    if (grep -q ${workers_property_secret_name} ${APACHE_WORKERS_PROPERTIES})
    then
        # Find the secret attribute and set to a new AJP_SECRET
        sed -i "/^${workers_property_secret_name}/s/=.*$/=${AJP_SECRET}/" ${APACHE_WORKERS_PROPERTIES}
    else
        sed -i "/${workers_property_host}/i ${workers_property_secret}" ${APACHE_WORKERS_PROPERTIES}
    fi

}

###########################
# Change workers.properties
# - add or replace the secret on each of the worker types after 
#   the host setting

worker_properties_add_secret "worker.soap"
worker_properties_add_secret "worker.tta"
worker_properties_add_secret "worker.sgdadmin"


exit $OK

