#!/bin/bash

# Terminate configure if no httpd (ServerGOD) installed in system
array=($(rpmquery -a | grep httpd))
if [[ ${#array[@]} == 0 ]]; then
    echo "ServerGOD not installed!"
    exit 0
fi

# Default values
HOST=172.0.1.2
SUBNET=172.0.1.

HTTPD_CONF=/etc/httpd/conf/httpd.conf

# Backup httpd.conf
if [[ ! -f $HTTPD_CONF'.orig' ]]; then
    cp $HTTPD_CONF $HTTPD_CONF'.orig'
else
    # Restore orig
    cp $HTTPD_CONF'.orig' $HTTPD_CONF
fi

# Install nessasary modules
cp modules/mod_* /etc/httpd/modules/
# Install mod_cluster config writed iSergio
cp conf.d/mod_cluster.conf /etc/httpd/conf.d/

# Don't use proxy_balancer, they conflict with mod_cluster_proxy
sed -i '/^LoadModule proxy_balancer_module/s/^/#/' $HTTPD_CONF
# Remove default access to root directory written by vniins
sed -i '/<Directory \/>/,/<\/Directory>/d' $HTTPD_CONF
# Set default encoding to UTF8
sed -i 's/AddDefaultCharset KOI8-R/AddDefaultCharset UTF-8/g' $HTTPD_CONF

# Configure listen ip address
echo -n "Please, enter a ip address of this server(Default: $HOST): "
read _HOST
if [[ -z $_HOST ]]; then
    _HOST=$HOST
fi
HOST=$_HOST
sed -i 's/__HOST__/'$HOST'/g' /etc/httpd/conf.d/mod_cluster.conf

# Configure access ip's
echo -n "Please, enter allowed access subnet(Default: $SUBNET): "
read _SUBNET
if [[ -z $_SUBNET ]]; then
    _SUBNET=$SUBNET
fi
SUBNET=$_SUBNET
sed -i 's/__SUBNET__/'$SUBNET'/g' /etc/httpd/conf.d/mod_cluster.conf

# Turn on auto start of httpd service
chkconfig httpd on
# Restart httpd service
service httpd restart
