#!/bin/sh
# @function install PHP server
function InstallPHP()
{
    local extpath_= mod_= confext_= ver_=$1
    ver_=${ver_:-5.5}
    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
    
    # install
    eval cd ${_PATH_COMPILATION_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval rm -rf ./php-${ver_}.* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval tar jxvf ${_PATH_SOFTWARE_}/php-${ver_}.* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cd php-${ver_}.* ${_SYS_SHOW_INSTALL_DETAILS_}
    if [ "${_install_php_sets_[gd]}" = "1" ]; then
        # install basic libs
        eval yum install -y freetype freetype-devel gd gd-devel libpng libpng-devel openjpeg openjpeg-devel ${_SYS_SHOW_INSTALL_DETAILS_}
        confext_="${confext_} --with-gd --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-freetype-dir"
    fi
    if [ "${_install_php_sets_[tokenizer]}" != "1" ]; then
        confext_="${confext_} --disable-tokenizer"
    fi
    if [ "${_install_php_sets_[mysqli]}" = "1" ]; then
        confext_="${confext_} --with-mysqli"
    fi
    eval ./configure --prefix=/usr/local/php \
        ${mod_} \
        --with-config-file-path=/usr/local/php/etc \
        --with-mysql=mysqlnd \
        --with-iconv-dir=/usr/local \
        --with-zlib \
        --with-libxml-dir=/usr \
        --enable-xml \
        --disable-rpath \
        --enable-bcmath \
        --enable-inline-optimization \
        --with-curl \
        --enable-mbregex \
        --enable-mbstring \
        --with-mcrypt \
        --with-openssl \
        --with-mhash \
        --enable-pcntl \
        --enable-sockets \
        --with-xmlrpc \
        --enable-zip \
        --enable-soap \
        --with-pdo-mysql=mysqlnd \
        --disable-ipv6 \
        --without-sqlite3 \
        --without-pdo-sqlite \
        --without-cdb \
        --disable-phar \
        --enable-opcache \
        ${confext_} \
        ${_PHP_COMPLIE_EXTRAS_}
        ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make ZEND_EXTRA_LIBS='-liconv' ${_SYS_MAKE_EXT_} ${_SYS_SHOW_INSTALL_DETAILS_}   # make with liconv
    eval make install ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $? 1
    
    # cp configure
    eval \cp -f ${_PATH_CONFIGS_}/php.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
    extpath_=`/usr/local/php/bin/php-config --extension-dir`
    sed -ri '
        s#^(extension_dir\s*=\s*\").*(\".*)#\1'${extpath_}'\2#;
        s#^(date\.timezone\s*=\s*).*#\1'${_TIMEZONE_}'#;
        ' /usr/local/php/etc/php.ini
    ShowStatus $?
}