#!/bin/bash

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

BASE_DIR=$(dirname $0)
if [ $BASE_DIR == "." ] ; then
    SCRIPT_DIR=$(pwd)
else
    SCRIPT_DIR=$(pwd)/$BASE_DIR
fi

# Variables
INSTALL_USER=`whoami`
INSTALL_DIR=$1

SOURCE_DIR=${SCRIPT_DIR}/source_files

APACHE_VERSION=2.4.37
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 "[ERROR] input install directory" >> install.log.$INSTALL_DATE
    echo "[ERROR] input install directory" 
    echo "#################################################"
    echo "[ INSTALL_HOME ] is $SCRIPT_DIR"
    INSTALL_DIR=$SCRIPT_DIR
    INSTALL_DIR="/data2/PlaceWAS/web/engine/apache24"
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 pcre-devel zlib 


echo "SCRIPT_DIR=$SCRIPT_DIR"
echo "SOURCE_DIR=$SOURCE_DIR"
echo "INSTALL_DIR=$INSTALL_DIR"

cd $SOURCE_DIR
tar -xvzf ${APACHE_INSTALL_FILE}.tar.gz || exit 1
tar -xvzf ${AJP_CONNECTOR_INSTALL_FILE}.tar.gz || exit 1




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

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

APACHE_OPTIONS="
--prefix=${INSTALL_DIR} \
--with-mpm=worker \
--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/


############ Apahce HTTPD Server Directory Replace ########
${SCRIPT_DIR}/apache-replace.sh ${INSTALL_DIR}



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