#!/usr/bin/bash

#Tate:2022.4.23
#Author:Yingjian
#Function：一键编译安装python3

if [ -d "/usr/local/python3" ];then
echo "python3 已安装" 
exit 2
fi

ping -c 1 www.baidu.com &> /dev/null
if [ ! $? -eq 0 ];then
echo "请检测网络是否正常"
exit 2
fi

if [ ! -f "/usr/local/src/Python-3.8.12.tgz" ];then
curl -o /usr/local/src/Python-3.8.12.tgz -s http://8.210.62.122/python/Python-3.8.12.tgz
else
echo "Python包已安装"
fi

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel

tar xf /usr/local/src/Python-3.8.12.tgz -C /usr/local/

cd /usr/local/Python-3.8.12

./configure --prefix=/usr/local/python3
if [ ! $? -eq 0 ];then
echo "configure 失败 请清理环境 重新安装"
exit 3
fi

make -j `cat /proc/cpuinfo | grep processor| wc -l` && make install

if [ ! $? -eq 0 ];then
echo "make 失败 请清理环境 重新安装"
exit 4
fi

cat > /etc/profile.d/python.sh << EOF
export PYTHON_HOME=/usr/local/python3
export PATH=\$PYTHON_HOME/bin:\$PATH
EOF

rm -rf /usr/local/src/Python-3.8.12.tgz
echo "手动退出当前shell 执行python -V"
