#!/bin/bash

initPath=`pwd`

echo "<<======== nodeV20升级[开始] ============>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "<<======== nodeV20升级[开始] ============>>" >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log

node -v
if [ $? -eq 0 ];then
  echo "<<============ nodeV12已安装 ============>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "<<============ nodeV12已安装 ============>>" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  echo "<<=========== nodeV12开始卸载 ===========>>" >> /root/init.centos7/init.log
yum -y remove node*
  if [ $? -eq 0 ] ; then
    echo -e "nodeV12卸载                \033[32m 完成 \033[0m"
    date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
    echo "nodeV12卸载--------------------------[完成]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
  else
    echo -e "nodeV12卸载                \033[31m 失败 \033[0m"
    date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
    echo "nodeV12卸载--------------------------[失败]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
    exit 1;
  fi
fi

echo "###########################################" >> /root/init.centos7/init.log
echo "<<==== nodeV20编译依赖包下载[开始] ======>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
# 升级glibc需要的依赖
wget -O /opt/binutils-2.32.tar.gz http://simba.whhsn.cn:7374/init/doc/init/node-20/binutils-2.32.tar.gz
wget -O /opt/bison-3.2.1.tar.gz http://simba.whhsn.cn:7374/init/doc/init/node-20/bison-3.2.1.tar.gz
wget -O /opt/make-4.3.tar.gz http://simba.whhsn.cn:7374/init/doc/init/node-20/make-4.3.tar.gz
# 升级gcc需要的依赖
wget -O /opt/gmp-6.1.0.tar.bz2 http://simba.whhsn.cn:7374/init/doc/init/node-20/gmp-6.1.0.tar.bz2
wget -O /opt/mpfr-3.1.4.tar.bz2 http://simba.whhsn.cn:7374/init/doc/init/node-20/mpfr-3.1.4.tar.bz2
wget -O /opt/mpc-1.0.3.tar.gz http://simba.whhsn.cn:7374/init/doc/init/node-20/mpc-1.0.3.tar.gz
wget -O /opt/isl-0.18.tar.bz2 http://simba.whhsn.cn:7374/init/doc/init/node-20/isl-0.18.tar.bz2
# gcc-8.2.0安装包
wget -O /opt/gcc-8.2.0.tar.gz http://simba.whhsn.cn:7374/init/doc/init/node-20/gcc-8.2.0.tar.gz
# glibc-2.28安装包
wget -O /opt/glibc-2.28.tar.gz http://simba.whhsn.cn:7374/init/doc/init/node-20/glibc-2.28.tar.gz
# node-v20.12.1安装包
wget -O /opt/node-v20.12.1-linux-x64.tar.xz http://simba.whhsn.cn:7374/init/doc/init/node-20/node-v20.12.1-linux-x64.tar.xz

echo "###########################################" >> /root/init.centos7/init.log
echo "<<========= binutils升级[开始] ==========>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
tar -xf /opt/binutils-2.32.tar.gz -C /opt/
cd /opt/binutils-2.32/
./configure --prefix=/usr/local/binutils-2.32
make && make install 
if [ $? -eq 0 ] ; then 
  echo -e "binutils编译安装                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "binutils编译安装---------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "binutils编译安装                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "binutils编译安装---------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
#备份原有的binutils
mv /usr/bin/ld /usr/bin/ld_bak
mv /usr/bin/as /usr/bin/as_bak
#将新的软链接方式写入
ln -s /usr/local/binutils-2.32/bin/ld /usr/bin/ld
ln -s /usr/local/binutils-2.32/bin/as /usr/bin/as

echo "###########################################" >> /root/init.centos7/init.log
echo "<==<======= binutils升级[结束] ==========>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log



echo "###########################################" >> /root/init.centos7/init.log
echo "<<========== bison升级[开始] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
yum -y install bison
tar -xf /opt/bison-3.2.1.tar.gz -C /opt/
cd /opt/bison-3.2.1/
./configure --prefix=/usr/local/bison-3.2.1
make && make install

if [ $? -eq 0 ] ; then 
  echo -e "bison编译安装                        \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "bison编译安装------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "bison编译安装                        \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "bison编译安装------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
# 备份原有的bison
mv /usr/bin/bison /usr/bin/bison_bak
# 将新的软链接方式写入
ln -s /usr/local/bison-3.2.1/bin/bison /usr/bin/bison
echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== bison升级[结束] ===========>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== make升级[开始] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
tar -xf /opt/make-4.3.tar.gz -C /opt/
cd /opt/make-4.3/
./configure --prefix=/usr/local/make-4.3
make && make install
if [ $? -eq 0 ] ; then 
  echo -e "make编译安装                        \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "make编译安装-------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "make编译安装                        \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "make编译安装-------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
# 备份原有的make
mv /usr/bin/make /usr/bin/make_bak
# 将新的软链接方式写入
ln -s /usr/local/make-4.3/bin/make /usr/bin/make
echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== make升级[结束] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== gmp升级[开始] =============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
yum -y install m4
tar -xf /opt/gmp-6.1.0.tar.bz2 -C /opt/
cd /opt/gmp-6.1.0/
./configure --prefix=/usr/local/gmp
make && make install
if [ $? -eq 0 ] ; then 
  echo -e "gmp编译安装                         \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gmp编译安装--------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "gmp编译安装                         \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gmp编译安装--------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
echo "###########################################" >> /root/init.centos7/init.log
echo "<<============ gmp升级[结束] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== mpfr升级[开始] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
tar -xf /opt/mpfr-3.1.4.tar.bz2 -C /opt/
cd /opt/mpfr-3.1.4/
./configure --prefix=/usr/local/mpfr --with-gmp=/usr/local/gmp
make && make install
if [ $? -eq 0 ] ; then 
  echo -e "gmp编译安装                         \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gmp编译安装--------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "gmp编译安装                         \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gmp编译安装--------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== mpfr升级[结束] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== mpc升级[开始] =============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log

tar -xf /opt/mpc-1.0.3.tar.gz -C /opt/
cd /opt/mpc-1.0.3/
./configure --prefix=/usr/local/mpc --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr
make && make install
if [ $? -eq 0 ] ; then 
  echo -e "mpc编译安装                         \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "mpc编译安装--------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "mpc编译安装                         \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "mpc编译安装--------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
echo "###########################################" >> /root/init.centos7/init.log
echo "<<============ mpc升级[结束] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== isl升级[开始] =============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
yum -y install gmp-devel.x86_64
tar -xf /opt/isl-0.18.tar.bz2 -C /opt/
cd /opt/isl-0.18/
./configure --prefix=/usr/local/isl --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr --with-mpc=/usr/local/mpc
make && make install
if [ $? -eq 0 ] ; then 
  echo -e "isl编译安装                         \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "isl编译安装--------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "isl编译安装                         \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "isl编译安装--------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
echo "###########################################" >> /root/init.centos7/init.log
echo "<<============ isl升级[结束] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== gcc升级[开始] =============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
echo 'export LD_LIBRARY_PATH=/usr/local/isl/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
tar -xf /opt/gcc-8.2.0.tar.gz -C /opt/
cd /opt/gcc-8.2.0/
mkdir build && cd build
../configure --prefix=/usr/local/gcc-8.2.0 --enable-bootstrap --enable-checking=release --enable-languages=c,c++ --disable-multilib --with-gmp=/usr/local/gmp/ --with-mpfr=/usr/local/mpfr/ --with-mpc=/usr/local/mpc/ --with-isl=/usr/local/isl
make -j4
make install
# 备份原有的gcc
mv /usr/bin/gcc /usr/bin/gcc_bak
# 将新的软链接方式写入
ln -s /usr/local/gcc-8.2.0/bin/gcc /usr/bin/gcc
gcc -v
if [ $? -eq 0 ] ; then 
  echo -e "gcc编译安装                         \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gcc编译安装--------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "gcc编译安装                         \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "gcc编译安装--------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
echo "###########################################" >> /root/init.centos7/init.log
echo "<<============ gcc升级[结束] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<========== glibc升级[开始] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
tar -xf /opt/glibc-2.28.tar.gz -C /opt/
cd /opt/glibc-2.28/
mkdir build && cd build
# 执行export，设置环境变量为空
export LD_LIBRARY_PATH=
# 重新执行configure，这时候还是会报错，又将值重置回来
export LD_LIBRARY_PATH=/usr/local/isl/li
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
if [ $? -eq 0 ] ; then 
  echo -e "执行                         \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "执行---------------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "执行                         \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "执行---------------------------------[失败]" >> /root/init.centos7/init.log
  echo "##############重新设置环境变量#############" >> /root/init.centos7/init.log
export LD_LIBRARY_PATH=
export LD_LIBRARY_PATH=/usr/local/isl/lib
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
  if [ $? -eq 0 ] ; then 
    echo -e "第二次执行                         \033[32m 完成 \033[0m"
    date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
    echo "第二次执行---------------------------[完成]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
  else 
    echo -e "第二次执行                         \033[31m 失败 \033[0m"
    date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
    echo "第二次执行----000--------------------[失败]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
    exit 1;
  fi
fi
make -j4
make install 
strings /lib64/libc.so.6 |grep GLIBC_2.28
if [ $? -eq 0 ] ; then 
  echo -e "glibc编译安装                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "glibc编译安装------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "glibc编译安装                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "glibc编译安装------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== glibc升级[结束] ===========>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log


echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== node升级[开始] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
cp /opt/gcc-8.2.0/build/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.25 /usr/lib64/
mv /usr/lib64/libstdc++.so.6  /usr/lib64/libstdc++.so.6_bak
ln -s /usr/lib64/libstdc++.so.6.0.25 /usr/lib64/libstdc++.so.6
tar -xf /opt/node-v20.12.1-linux-x64.tar.xz -C /opt/
echo '#==node==' >> /etc/profile
echo 'export NODE_HOME=/opt/node-v20.12.1-linux-x64' >> /etc/profile
echo 'export PATH=$PATH:${NODE_HOME}/bin' >> /etc/profile
source /etc/profile
node -v
if [ $? -eq 0 ] ; then 
  echo -e "node升级                         \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "node升级-----------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "node升级                         \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "node升级-----------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== node升级[完成] ============>>" >> /root/init.centos7/init.log
date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
echo "###########################################" >> /root/init.centos7/init.log
echo "<<=========== node升级[完成] ============>>"
echo "############# 设置中文字符开始 ############" >> /root/init.centos7/init.log
echo "############# 设置中文字符开始 ############" 
localedef -v -c -i zh_CN -f UTF-8 zh_CN.UTF-8
if [ $? -eq 0 ] ; then 
  echo -e "设置中文字符                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "设置中文字符-------------------------[完成]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo -e "设置中文字符                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "设置中文字符-------------------------[失败]" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
