#!/bin/sh
# functions - install modules
# @function install Apache-Server
function InstallHttpd()
{
    local tmpname=
    
    if [ "${_WEB_SERVER_TYPE_}" = "nginx" ]; then
        return
    fi
    
    ShowResult "[green]正在安装Apache服务器... [/green]" 1
    
    cd ${_PATH_COMPILATION_}
    # prepare
    eval rm -rf ./httpd-2.* ${_SYS_SHOW_INSTALL_DETAILS_}   # clear httpd src-code before
    eval rm -rf ./apr-* ${_SYS_SHOW_INSTALL_DETAILS_}       # clear apr* src-code before
    eval tar jxvf ${_PATH_SOFTWARE_}/httpd-2.* ${_SYS_SHOW_INSTALL_DETAILS_}   # extract httpd package
    eval tar jxvf ${_PATH_SOFTWARE_}/apr-1.* ${_SYS_SHOW_INSTALL_DETAILS_}     # extract apr package
    eval tar jxvf ${_PATH_SOFTWARE_}/apr-util-* ${_SYS_SHOW_INSTALL_DETAILS_}  # extract apr-util package
    eval tmpname=`ls | grep httpd-2.`
    eval mv -f apr-1.* ./${tmpname}/srclib/apr           # mv & rename to httpd's srclib path
    eval mv -f apr-util-* ./${tmpname}/srclib/apr-util
    # install
    eval cd ${tmpname} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval ./configure --prefix=/usr/local/apache2 \
        --with-mpm=prefork \
        --with-pcre=/usr/local/pcre \
        --with-included-apr \
        --enable-mods-shared=all \
        --with-ssl --enable-ssl \
        --enable-so \
        --disable-cgid --disable-cgi \
        --enable-modules=all \
        --enable-vhost-alias \
        --enable-cache --enable-disk-cache --enable-mem-cache \
        --enable-deflate=shared \
        --enable-expires=shared \
        --enable-rewrite=shared \
        --enable-static-support \
        ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make ${_SYS_MAKE_EXT_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make install ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $? 1
    
    # set service
    eval \cp -f /usr/local/apache2/bin/apachectl /etc/init.d/httpd ${_SYS_SHOW_INSTALL_DETAILS_}
    
    # set chkconfig info
    eval "sed -i '/#\!\/bin\/sh/a\# chkconfig: 2345 50 90\n# description: Activates/Deactivates Apache Web Server' /etc/init.d/httpd ${_SYS_SHOW_INSTALL_DETAILS_}"
    eval chkconfig --add httpd ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chkconfig --level 2345 httpd off ${_SYS_SHOW_INSTALL_DETAILS_}
    eval \cp /usr/local/apache2/bin/apachectl /usr/bin/ ${_SYS_SHOW_INSTALL_DETAILS_}
    
    # replace configure files
    eval \cp -f ${_PATH_CONFIGS_}/httpd.conf /usr/local/apache2/conf/httpd.conf ${_SYS_SHOW_INSTALL_DETAILS_}
    eval \cp -f ${_PATH_CONFIGS_}/httpd-mpm.conf /usr/local/apache2/conf/extra/httpd-mpm.conf ${_SYS_SHOW_INSTALL_DETAILS_}
    eval \cp -f ${_PATH_CONFIGS_}/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf ${_SYS_SHOW_INSTALL_DETAILS_}
    
    #mkdir
    mkdir -p "${_WEB_VHOSTS_STD_SAVE_PATH_}"
    
    if [ "${_WEB_SERVER_USING_SSL_}" = "1" ]; then
        sed -ri '
            s/[# ]*(LoadModule ssl_module )/\1/; s/[# ]*(LoadModule socache_shmcb_module )/\1/;
            s@[# ]*(Include conf/extra/httpd-ssl\.conf)@\1@
            ' /usr/local/apache2/conf/httpd.conf
        eval \cp -f ${_PATH_CONFIGS_}/httpd-ssl.conf /usr/local/apache2/conf/extra/httpd-ssl.conf ${_SYS_SHOW_INSTALL_DETAILS_}
        sed -ri "
            s@\{_SSL_CRT_\}@${_WEB_SERVER_SSL_CRT_PATH_}@g;
            s@\{_SSL_KEY_\}@${_WEB_SERVER_SSL_KEY_PATH_}@g;
            s@\{_SSL_CHAIN_CRT_\}@${_WEB_SERVER_SSL_CHAIN_CRT_PATH_}@g;
            " /usr/local/apache2/conf/extra/httpd-ssl.conf
        mkdir -p "${_WEB_VHOSTS_SSL_SAVE_PATH_}"
    fi
    
    ShowStatus $?
}