#!/bin/sh
# @function install PHP server
function InstallST2PHP()
{
    local extpath_= mod_= confext_= ver_=$1
    ver_=${ver_:-7.1}
    declare -A _install_php_sets_=()
    if [ ! -z "$2" ]; then
        eval declare -A _install_php_sets_=$2 2>/dev/null
    fi
    
    ShowResult "[green]正在安装PHP服务器... [/green]" 1
    
    if [ "${_WEB_SERVER_TYPE_}" = "nginx" ]; then # nginx, using fpm
        mod_="--enable-fpm"
    else # default, using apx2
        mod_="--with-apxs2=/usr/local/apache2/bin/apxs"
    fi
    if [ -d /usr/local/openssl ];then
        ssl_="--with-openssl=/usr/local/openssl"
    else
        ssl_=""
    fi
    eval yum -y install libxml2 libxml2-devel \
        libmcrypt libmcrypt-devel \
        libjpeg-devel libpng-devel freetype-devel \
        readline readline-devel \
        libxslt-devel perl perl-devel \
        psmisc.x86_64 recode recode-devel \
        libtidy libtidy-devel \
        php-gd openssl openssl-devel ${_SYS_SHOW_INSTALL_DETAILS_}
    
    # install
    eval cd ${_PATH_COMPILATION_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval rm -rf ./php-${ver_}.* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval tar xvf ${_PATH_SOFTWARE_}/php-${ver_}.* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cd php-${ver_}.* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval ./configure --prefix=/usr/local/php \
        --with-config-file-path=/usr/local/php/etc \
        --with-mysqli=/usr/local/mysql/bin/mysql_config \
        --with-pdo-mysql=/usr/local/mysql \
        --enable-fpm \
        --enable-opcache \
        --enable-xml \
        --enable-bcmath \
        --enable-shmop \
        --enable-exif \
        --enable-sysvsem \
        --enable-inline-optimization \
        --enable-mbregex \
        --enable-mbstring \
        --enable-gd-native-ttf \
        --enable-pcntl \
        --enable-sockets \
        --enable-ftp \
        --enable-zip \
        --enable-soap \
        --with-jpeg-dir=/usr \
        --with-iconv-dir=/usr/local \
        --with-freetype-dir=/usr \
        --with-png-dir=/usr \
        --with-libxml-dir=/usr \
        --with-curl \
        --with-zlib \
        --with-bz2 \
        --with-gd \
        --with-mcrypt \
        --with-mhash \
        --with-xmlrpc \
        --with-gettext \
        --without-pear \
        --disable-phar \
        --disable-fileinfo \
        --disable-rpath \
        ${ssl_}
        ${_PHP_COMPLIE_EXTRAS_}
        ${_SYS_SHOW_INSTALL_DETAILS_}
    sed -i '/^EXTRA_LIBS\ =/ s/$/\ \-liconv/' Makefile
    eval make ${_SYS_MAKE_EXT_} ${_SYS_SHOW_INSTALL_DETAILS_}   # make with liconv
    eval make install ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $? 1
    
    # cp configure
    /usr/local/php/bin/php ${_PATH_CONFIGS_}/go-pear.phar ${_SYS_SHOW_INSTALL_DETAILS_}

    eval \cp -f ${_PATH_CONFIGS_}/php-st2.ini /usr/local/php/etc/php.ini ${_SYS_SHOW_INSTALL_DETAILS_}
    
    # fpm.ini
    if [ "${_WEB_SERVER_TYPE_}" = "nginx" ]; then # nginx, add fpm.ini, but not support yet...
        eval \cp -f ${_PATH_CONFIGS_}/php-fpm-conf/php-fpm.conf /usr/local/php/etc/php-fpm.conf ${_SYS_SHOW_INSTALL_DETAILS_}
        pool_path_=/usr/local/php/etc/pool.conf.d/
        let i=1
        _PHP_FPM_POOL_NUMS_=${_PHP_FPM_POOL_NUMS_:-1}
        mkdir -p ${pool_path_}
        while [ $i -le ${_PHP_FPM_POOL_NUMS_} ]; do
            eval \cp -f ${_PATH_CONFIGS_}/php-fpm-conf/pool.conf.d/pool.tpl.conf ${pool_path_}/pool-${i}.conf ${_SYS_SHOW_INSTALL_DETAILS_}
            sed -ri "
                s@\{POOL_ID\}@${i}@g;
                s@\{WEB_ROOT\}@${_PATH_WEB_}@g;
            " ${pool_path_}/pool-${i}.conf
            let i++
        done
        eval \cp -f ${_PATH_CONFIGS_}/php-fpm-conf/php-fpm.init.sh /etc/init.d/php-fpm ${_SYS_SHOW_INSTALL_DETAILS_}
        eval chmod +x /etc/init.d/php-fpm ${_SYS_SHOW_INSTALL_DETAILS_}
        eval chkconfig --add php-fpm ${_SYS_SHOW_INSTALL_DETAILS_}
    fi
    
    # edit php.ini
    sed -ri '
        s#^(date\.timezone\s*=\s*).*#\1'${_TIMEZONE_}'#;
        ' /usr/local/php/etc/php.ini
    ShowStatus $?
}
