#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

if [ $(id -u) != "0" ]
then
  echo -e "Error: You must be root to run this script, please use root to install this script."
  exit 1
fi

#disabled selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0

yum -y install gcc gcc-c++

wget=$(rpm -q wget|grep "not installed")
if [ "$wget" != "" ]; then
  yum -y install wget
fi

yum -y install bzip2

#install APR
if [ ! -d "/usr/local/apr" ]; then
  cd /usr/local/src
  if [ ! -f "apr-1.7.6.tar.bz2" ]; then
    wget -c http://archive.apache.org/dist/apr/apr-1.7.6.tar.bz2 -P /usr/local/src --progress=bar
  fi

  test !-d "apr-1.7.6" || rm -rf "apr-1.7.6"
  tar -jxvf apr-1.7.6.tar.bz2

  cd apr-1.7.6
  ./configure --prefix=/usr/local/apr
  make && make install > /dev/null
fi

#install APR-util
if [ ! -d "/usr/local/apr-util" ]; then
  cd /usr/local/src
  if [ ! -f "apr-util-1.5.4.tar.bz2" ]; then
    wget -c http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.bz2 -P /usr/local/src --progress=bar
  fi

  test ! -d "apr-util-1.5.4" || rm -rf "apr-util-1.5.4"
  tar -jxvf apr-util-1.5.4.tar.bz2

  cd apr-util-1.5.4
  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
  make && make install > /dev/null
fi

#install pcre
if [ ! -d "/usr/local/pcre" ]; then
  cd /usr/local/src
  if [ ! -f "pcre-8.45.tar.bz2" ]; then
    wget -c http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.45.tar.bz2 -P /usr/local/src --progress=bar
  fi

  test ! -d "pcre-8.45" || rm -rf "pcre-8.45"
  tar -jxvf pcre-8.45.tar.bz2

  cd pcre-8.45
  ./configure --prefix=/usr/local/pcre
  make && make install > /dev/null
  echo "export PATH=$PATH:/usr/local/pcre/bin" >> /etc/profile
  source /etc/profile
fi

#install openssl
if [ ! -d "/usr/local/openssl" ]; then
  cd /usr/local/src
  if [ ! -f "openssl-3.5.7.tar.gz" ]; then
    wget -c https://gh-proxy.com/https://github.com/openssl/openssl/releases/download/openssl-3.5.7/openssl-3.5.7.tar.gz -P /usr/local/src --progress=bar
  fi

  test ! -d "openssl-3.5.7" || rm -rf "openssl-3.5.7"
  tar xzvf openssl-3.5.7.tar.gz

  cd openssl-3.5.7
  yum install -y perl perl-IPC-Cmd perl-Time-Piece
  ./config --prefix=/usr/local/openssl -fPIC no-gost
  make depend && make install > /dev/null
fi

#install apache
if [ ! -d "/usr/local/apache" ]; then
  cd /usr/local/src
  if [ ! -f "httpd-2.4.68.tar.bz2" ]; then
    wget -c https://archive.apache.org/dist/httpd/httpd-2.4.68.tar.bz2 -P /usr/local/src --progress=bar
  fi

  test ! -d "httpd-2.4.68" || rm -rf "httpd-2.4.68"
  tar -xvf httpd-2.4.68.tar.bz2
  cd httpd-2.4.68
  ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-mods-shared=most --enable-rewrite --enable-ssl --with-ssl=/usr/local/openssl --enable-mpms-shared=all --enable-dav
  make && make install > /dev/null
    
  echo "export PATH=\$PATH:/usr/local/apache/bin" >> /etc/profile
  source /etc/profile
fi

egrep "^www" /etc/group >& /dev/null
if [ $? -ne 0 ]; then
  groupadd www
fi

egrep "^www" /etc/passwd >& /dev/null
if [ $? -ne 0 ]; then
  useradd -s /sbin/nologin www -g www
fi

sed -i -e "s/^#LoadModule rewrite_module/LoadModule rewrite_module/" \
-e "/^User/cUser www" \
-e "/^Group/cGroup www" \
-e "/^#ServerName/cServerName localhost:80" \
-e "s/DirectoryIndex index.html/DirectoryIndex index.html index.php/" \
-e "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" \
-e '0,/AllowOverride none/s/AllowOverride none/AllowOverride all/' \
/usr/local/apache/conf/httpd.conf

cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
 
[Service]
Type=forking
PIDFile=/usr/local/apache/logs/httpd.pid
ExecStart=/usr/local/apache/bin/apachectl -k start
ExecReload=/usr/local/apache/bin/apachectl -k graceful
ExecStop=/usr/local/apache/bin/apachectl -k stop
PrivateTmp=true
Restart=on-failure
RestartSec=5
 
[Install]
WantedBy=multi-user.target
EOF

systemctl enable --now httpd

query=$(firewall-cmd --zone=public --query-port=80/tcp)
if [ ! "$query" == "yes" ]; then
  firewall-cmd --zone=public --add-port=80/tcp --permanent > /dev/null
  firewall-cmd --reload > /dev/null
fi

query=$(firewall-cmd --zone=public --query-port=443/tcp)
if [ ! "$query" == "yes" ]; then
  firewall-cmd --zone=public --add-port=443/tcp --permanent > /dev/null
  firewall-cmd --reload > /dev/null
fi

echo -e "Apache has been installed successfully."