#!/bin/bash

#if [[ "$EUID" -ne 0 ]]; then
#	echo -e "Sorry, you need to run this as root"
#	exit 1
#fi

BASE_DIR="$(cd "$(dirname "$0")"; pwd)";

# Variables
INSTALL_USER=`whoami`
INSTALL_DIR=$1
DOCUMENT_ROOT=$2
HTTP_PORT=$3
SSL_PORT=$4

SOURCE_DIR=${BASE_DIR}/source_files

APACHE_VERSION=2.4.41
APACHE_INSTALL_FILE=httpd-${APACHE_VERSION}

AJP_CONNECTOR_VERSION=1.2.46
AJP_CONNECTOR_INSTALL_FILE=tomcat-connectors-${AJP_CONNECTOR_VERSION}-src

INSTALL_DATE=`date +%Y%m%d_%H%M%S`

if [ "x$1" = "x" ]; then
    echo "#################################################"
    echo "[WARN] Empty input install directory. Use default value." >> install.log.$INSTALL_DATE
    echo "[WARN] Empty input install directory. Use default value."
    echo "#################################################"

    INSTALL_DIR="/opt/WASup/engines/apache-2.4"
    echo "[ INSTALL_HOME ] is $INSTALL_DIR"
fi

if [ "x$2" = "x" ]; then
    DOCUMENT_ROOT=$INSTALL_DIR/htdocs
fi

if [ "x$3" = "x" ]; then
    HTTP_PORT=80
fi

if [ "x$4" = "x" ]; then
    SSL_PORT=443
fi

clear

# Cleanup
# The directory should be deleted at the end of the script, but in case it fails
#rm -r /usr/local/src/apache/ >> /dev/null 2>&1
#mkdir -p ${INSTALL_DIR}/modules
#mkdir -p ${INSTALL_DIR}/tmp/client
#mkdir -p ${INSTALL_DIR}/tmp/proxy
#mkdir -p ${INSTALL_DIR}/tmp/fastcgi

# Dependencies
#apt-get update
#apt-get install -y build-essential ca-certificates wget curl libpcre3 libpcre3-dev autoconf unzip automake libtool tar git libssl-dev zlib1g-dev uuid-dev lsb-release

#sudo yum install -y gcc zlib openssl-devel apr-util-devel apr-devel pcre-devel perl-devel nghttp2 libnghttp2-devel

packageList="gcc zlib openssl-devel apr-util-devel apr-devel pcre-devel perl-devel nghttp2 libnghttp2-devel"

for packageName in $packageList; do
  rpm --quiet --query $packageName || sudo yum install -y $packageName
done

echo "BASE_DIR=$BASE_DIR"
echo "SOURCE_DIR=$SOURCE_DIR"
echo "INSTALL_DIR=$INSTALL_DIR"
echo "DOCUMENT_ROOT=$DOCUMENT_ROOT"
echo "HTTP_PORT=$HTTP_PORT"
echo "SSL_PORT=$SSL_PORT"

cd $SOURCE_DIR
rm -rf ${APACHE_INSTALL_FILE}
tar -xvzf ${APACHE_INSTALL_FILE}.tar.gz || exit 1
tar -xvzf ${AJP_CONNECTOR_INSTALL_FILE}.tar.gz || exit 1
tar -xvzf apr-1.6.5.tar.gz || exit 1
tar -xvzf apr-util-1.6.1.tar.gz || exit 1
mv apr-1.6.5 ${APACHE_INSTALL_FILE}/srclib/apr
mv apr-util-1.6.1 ${APACHE_INSTALL_FILE}/srclib/apr-util

############# APACHE HTTPD - Source Compile & Make INSTALL ################

echo "#########################################"
echo "#######   Apache Httpd Install ##########"
echo "#########################################"

APACHE_OPTIONS="
--prefix=${INSTALL_DIR} \
--with-mpm=worker \
--with-included-apr \
--with-included-apr-util \
--with-http2 \
--enable-modules=most \
--enable-mods-shared=most \
--enable-cache \
--enable-file-cache \
--enable-so \
--enable-mime-magic \
--enable-expires \
--enable-header \
--enable-proxy \
--enable-ssl \
--enable-https \
--enable-mpms-shared=all \
--enable-vhosts-alias --enable-rewrite \
"

cd $SOURCE_DIR/${APACHE_INSTALL_FILE}

./configure $APACHE_OPTIONS
make -j "$(nproc)"
make install

############# AJP Connector - Source Compile & Make INSTALL ################

echo "#########################################"
echo "#######  Tomcat Connector Install #######"
echo "#########################################"

cd $SOURCE_DIR/${AJP_CONNECTOR_INSTALL_FILE}/native

AJP_CONNECTOR_OPTIONS="
--with-apxs=${INSTALL_DIR}/bin/apxs
"
echo "OPTIONS==> ./configure $AJP_CONNECTOR_OPTIONS"

./configure $AJP_CONNECTOR_OPTIONS
make -j "$(nproc)"
make install

############ Default Template Copy ##############
cp -fR $SOURCE_DIR/bin/* ${INSTALL_DIR}/bin/
cp -fR $SOURCE_DIR/conf/* ${INSTALL_DIR}/conf/

if [ ! -d $DOCUMENT_ROOT ] ; then
    mkdir -p $DOCUMENT_ROOT
    cp $INSTALL_DIR/htdocs/index.html $DOCUMENT_ROOT/
fi

############ Apahce HTTPD Server Directory Replace ########
sh ${BASE_DIR}/replace.sh ${INSTALL_DIR} ${DOCUMENT_ROOT} ${HTTP_PORT} ${SSL_PORT}

############ Apahce HTTPD Server Directory Replace ########
