#!bin/bash

set -e

# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: please use root to install"
exit 1
fi

id -u www >& /dev/null
if [ $? -ne 0 ]; then
groupadd www
useradd -g www -M -s /sbin/nologin www
fi


PHP_INFO=('php-7.2.29' 'php-7.3.16' 'php-7.4.4')
PHP_ADDR=(
'https://www.php.net/distributions/php-7.2.29.tar.gz'
'https://www.php.net/distributions/php-7.3.16.tar.gz'
'https://www.php.net/distributions/php-7.4.4.tar.gz'
)
SWOOLE_ADDR='http://123.207.144.90/resource/swoole-4.4.16.tar.gz'
SWOOLE_FILE=$(basename ${SWOOLE_ADDR})

Install_PHP()
{
PHP_Select=-1
if [ $PHP_Select -lt 0 ]; then
echo "1: Install ${PHP_INFO[0]}"
echo "2: Install ${PHP_INFO[1]}"
echo "3: Install ${PHP_INFO[2]}"
read -p "Enter your choice (1, 2 or 3), default is 2: " PHP_Select
fi

case "${PHP_Select}" in
"1" )
echo You will install ${PHP_INFO[0]}
PHP_Select=0
;;
"2" )
echo You will install ${PHP_INFO[1]}
PHP_Select=1
;;
"3" )
echo You will install ${PHP_INFO[2]}
PHP_Select=2
;;
* )
echo No input, You will install ${PHP_INFO[1]}
PHP_Select=1
esac

PHP_SOURCE_ADDR=${PHP_ADDR[$PHP_Select]}
PHP_SOURCE_FILE=$(basename ${PHP_SOURCE_ADDR})

echo $PHP_SOURCE_ADDR
echo $PHP_SOURCE_FILE

[ -d /data/software ] || mkdir -p /data/software
[ -d /data/logs ] || mkdir -p /data/logs
cd /data/software

yum install -y epel-release && yum -y update
yum -y install libjpeg libjpeg-devel libpng libpng-devel \
libXpm libXpm-devel freetype freetype-devel mcrypt mhash \
libpqxx libpqxx-devel libxml2-devel curl-devel libmcrypt \
libmcrypt-devel postgresql-devel openssl-devel oniguruma oniguruma-devel
yum -y install make gcc gcc-c++ wget

if [ ! -e ${PHP_SOURCE_FILE} ]; then
wget $PHP_SOURCE_ADDR -O $PHP_SOURCE_FILE
fi
if [ -d php-src ]; then
rm -rf php-src/*
else
mkdir php-src
fi

tar zxf $PHP_SOURCE_FILE -C php-src && cd php-src/php*

./configure --prefix=/usr/local/php \
--enable-fpm --with-fpm-user=www --with-fpm-group=www \
--with-config-file-path=/usr/local/php/etc \
--with-pdo-mysql \
--with-mysqli \
--with-pgsql \
--with-pdo-pgsql \
--with-curl \
--with-libxml-dir \
--enable-soap \
--enable-calendar \
--with-mhash \
--enable-sockets \
--with-openssl \
--enable-bcmath \
--with-zlib-dir \
--enable-mbstring \
--enable-pcntl \
--with-gd --with-jpeg-dir --with-png-dir --with-xpm-dir \
--with-freetype-dir \
--without-sqlite3 --without-pdo-sqlite

make && make install
ln -sf /usr/local/php/bin/php /usr/local/sbin/php
ln -sf /usr/local/php/sbin/php-fpm /usr/local/sbin/php-fpm
cp php.ini-production /usr/local/php/etc/php.ini
sed -i '/;date.timezone/a date.timezone=PRC' /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sed -i 's/;pid\ =\ run\/php-fpm.pid/pid = \/var\/run\/php-fpm.pid/' /usr/local/php/etc/php-fpm.conf
sed -i 's/;error_log\ =\ log\/php-fpm.log/error_log = \/data\/log\/php-fpm.log/' /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

rm -rf /data/software/php-src

read -p "Whether to install phpredis(yes/no):" Install_PHPRedis
if [ "${Install_PHPRedis}" = "yes" -o "${Install_PHPRedis}" -o -z "${Install_PHPRedis}" ]; then
/usr/local/php/bin/pecl install redis
sed -i '$a\\nextension=redis.so\n' /usr/local/php/etc/php.ini
fi

echo "Install ${PHP_INFO[$PHP_Select]} success !"
sleep 1

}

Install_Swoole()
{
echo "Install Swoole"

cd /data/software
if [ ! -s ${SWOOLE_FILE} ]; then
wget ${SWOOLE_ADDR} -O ${SWOOLE_FILE}
fi

yum -y install autoconf

if [ -d swoole-src ]; then
rm -rf swoole-src/*
else
mkdir swoole-src
fi
tar zxf ${SWOOLE_FILE} -C swoole-src && cd swoole-src/swoole*
/usr/local/php/bin/phpize
./configure \
--enable-openssl \
--enable-http2 \
--with-php-config=/usr/local/php/bin/php-config
make && make install
rm -rf /data/software/swoole-src
sed -i '$a\\nextension=swoole.so\n' /usr/local/php/etc/php.ini

echo "Install Swoole success!"
}

if [ ! -s /usr/local/php/bin/php ]; then
Install_PHP
fi

if [ $(grep 'swoole' /usr/local/php/etc/php.ini | wc -l) -eq 0 ];
then
Install_Swoole
fi