#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/opt/homebrew/bin
export PATH=$PATH:/opt/homebrew/bin

curPath=`pwd`
rootPath=$(dirname "$curPath")
rootPath=$(dirname "$rootPath")
serverPath=$(dirname "$rootPath")
sourcePath=${serverPath}/source
sysName=`uname`

rapira_version=0.9.1
version=8.4.26
PHP_VER=84
md5_file_ok=32a2de53862ad44ed4a5005244ce4f1b50c271e74dced215449a4443b40569f1
Install_php()
{
#------------------------ install start ------------------------------------#
echo "安装php-${version} ..."
mkdir -p $sourcePath/rapira
mkdir -p $serverPath/rapira

if [ ! -d $sourcePath/rapira/php${PHP_VER} ];then

	# ----------------------------------------------------------------------- #
	# 中国优化安装
	cn=$(curl -fsSL -m 10 -s http://ipinfo.io/json | grep "\"country\": \"CN\"")
	LOCAL_ADDR=common
	if [ ! -z "$cn" ];then
		LOCAL_ADDR=cn
	fi

	if [ "$LOCAL_ADDR" = "cn" ];then
		if [ ! -f $sourcePath/rapira/php-${version}.tar.xz ];then
			wget --no-check-certificate -O $sourcePath/rapira/php-${version}.tar.xz https://mirrors.nju.edu.cn/php/php-${version}.tar.xz
		fi
	fi
	# ----------------------------------------------------------------------- #
	

	if [ ! -f $sourcePath/rapira/php-${version}.tar.xz ];then
		wget --no-check-certificate -O $sourcePath/rapira/php-${version}.tar.xz https://www.php.net/distributions/php-${version}.tar.xz
	fi

	#检测文件是否损坏.
	if [ -f $sourcePath/rapira/php-${version}.tar.xz ];then
		md5_file=`sha256sum $sourcePath/rapira/php-${version}.tar.xz  | awk '{print $1}'`
		if [ "${md5_file}" != "${md5_file_ok}" ]; then
			echo "PHP${version} 下载文件不完整,重新安装"
			rm -rf $sourcePath/rapira/php-${version}.tar.xz
		fi
	fi
	
	cd $sourcePath/rapira && tar -Jxf $sourcePath/rapira/php-${version}.tar.xz
	mv $sourcePath/rapira/php-${version} $sourcePath/rapira/php${PHP_VER}
fi

if [ ! -d $sourcePath/rapira/rapira-${rapira_version} ];then
	if [ ! -f $sourcePath/rapira/rapira-${rapira_version}.tar.gz ];then
		wget --no-check-certificate -O $sourcePath/rapira/rapira-${rapira_version}.tar.gz https://github.com/rapira-rs/rapira/archive/refs/tags/v${rapira_version}.tar.gz
	fi
	
	cd $sourcePath/rapira && tar -zxvf $sourcePath/rapira/rapira-${rapira_version}.tar.gz
fi

OPTIONS='--without-iconv'

# if [ $sysName = 'Darwin' ]; then
# 	OPTIONS="${OPTIONS} --with-curl"
# fi

argon_version=`pkg-config libargon2 --modversion`
if [ "$?" = "0" ];then
	OPTIONS="${OPTIONS} --with-password-argon2"
fi

IS_64BIT=`getconf LONG_BIT`
if [ "$IS_64BIT" = "64" ];then
	OPTIONS="${OPTIONS} --with-libdir=lib64"
fi

# ----- cpu start ------
if [ -z "${cpuCore}" ]; then
	cpuCore="1"
fi

if [ -f /proc/cpuinfo ];then
	cpuCore=`cat /proc/cpuinfo | grep "processor" | wc -l`
fi

MEM_INFO=$(which free > /dev/null && free -m|grep Mem|awk '{printf("%.f",($2)/1024)}')
if [ "${cpuCore}" != "1" ] && [ "${MEM_INFO}" != "0" ];then
    if [ "${cpuCore}" -gt "${MEM_INFO}" ];then
        cpuCore="${MEM_INFO}"
    fi
else
    cpuCore="1"
fi

if [ "$cpuCore" -gt "2" ];then
	cpuCore=`echo "$cpuCore" | awk '{printf("%.f",($1)*0.8)}'`
else
	cpuCore="1"
fi
# ----- cpu end ------

# OPTIONS="${OPTIONS} --enable-debug"
# OPTIONS="${OPTIONS} --enable-dtrace"

if [ "$sysName" = "Darwin" ];then
	BREW_DIR=`which brew`
	BREW_DIR=${BREW_DIR/\/bin\/brew/}

	LIB_DEPEND_DIR=`brew info openssl | grep ${BREW_DIR}/Cellar/openssl | cut -d \  -f 1 | awk 'END {print}'`
	OPTIONS="$OPTIONS --with-openssl=$(brew --prefix openssl)"
	export PKG_CONFIG_PATH=$LIB_DEPEND_DIR/lib/pkgconfig
	export OPENSSL_CFLAGS="-I${LIB_DEPEND_DIR}/include"
	export OPENSSL_LIBS="-L/${LIB_DEPEND_DIR}/lib -lssl -lcrypto -lz"
else
	# cd ${rootPath}/plugins/php/lib && /bin/bash openssl_35.sh
	# export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$serverPath/lib/openssl35/lib/pkgconfig
	OPTIONS="$OPTIONS --with-openssl"
fi

echo "$sourcePath/rapira/${PHP_VER}"

set PHP_SRC=$sourcePath/rapira/php${PHP_VER}
# make php PHP_SRC=/www/server/source/rapira/php84 PHP_PREFIX="/www/server/rapira/84/link"

#install php
# cd /www/server/rapira/84/link/bin/php-config
if [ ! -d ${serverPath}/rapira/${PHP_VER}/link ];then
	
	cd $sourcePath/rapira/php${PHP_VER}
	PHP_PREFIX="${serverPath}/rapira/${PHP_VER}/link"
	./buildconf --force
	./configure --prefix="${PHP_PREFIX}" \
		--enable-embed \
	    --enable-igbinary \
	    --enable-redis \
	    --enable-redis-igbinary $EXTRA
	make -j"$(getconf _NPROCESSORS_ONLN)"
	make install
fi

#install rapira
# cd /www/server/source/rapira/rapira-0.9.1
# export PHP_CONFIG=/www/server/rapira/84/link/bin/php-config
rapira_bindir=${serverPath}/rapira/${PHP_VER}/bin
rapira_srcdir=$sourcePath/rapira/rapira-${rapira_version}
mkdir -p $rapira_bindir
CARGO_BIN=/root/.cargo/bin/cargo
if [ -f /opt/homebrew/bin/cargo ];then
	CARGO_BIN=/opt/homebrew/bin/cargo
fi

if [ ! -f ${serverPath}/rapira/${PHP_VER}/bin/rapira ];then
	cd $rapira_srcdir
	export PHP_CONFIG=${serverPath}/rapira/${PHP_VER}/link/bin/php-config
	# cargo build --release
	# cargo +1.98.1 build --release
	if [ ! -f $CARGO_BIN ];then
		curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
	fi

	$CARGO_BIN +1.98.1 build --release --locked --bin rapira
	if [ "$?" == "0" ] && [ -f $rapira_srcdir/target/release/rapira ] ;then
		mv $rapira_srcdir/target/release/rapira $rapira_bindir
	fi
fi

#------------------------ install end ------------------------------------#
}

Uninstall_php()
{
	systemctl rapira${PHP_VER} stop
	rm -rf $serverPath/rapira/${PHP_VER}
	echo "卸载php-${version}..."
}

action=${1}
if [ "${1}" = 'install' ];then
	Install_php
else
	Uninstall_php
fi
