#!/bin/bash
# Copyright (c) 2020, Oracle and/or its affiliates.

apacheConf="${APACHE_DIR}/conf/httpd.conf"

# Get the current https port
https_port="$(grep "<VirtualHost \*:" ${apacheConf} | \
            cut -d':' -f2 | tr -d '>' | head -1)"

# Check whether the httpd-sgd-virtual.conf is already included 
include="Include conf/extra/httpd-sgd-virtual.conf"
isPresent="$(grep "${include}" "${apacheConf}")"
if [ -z "${isPresent}" ]
then
    # Find the VirtualHost block for the https port and insert
    # the include the line before it
    awk -v includestr="${include}" -v https_port="${https_port}" '
    {
        if ($0 == "<VirtualHost *:"https_port">" )
        {
            found = 1;
        }

        if ($0 == "</VirtualHost>" && found)
        {
             print "    "includestr
             found = 0;
        }
   
        print $0
    }' ${apacheConf} > ${apacheConf}.sgd.tmp
    mv -f ${apacheConf}.sgd.tmp ${apacheConf}
fi

