#!/bin/bash
#Network test
echo "<<=========== 数据源更新[开始] ==========>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/start.log
echo "<<=========== 数据源更新[开始] ==========>>" >> /home/start.log
echo "##########" >> /home/start.log

echo "Network test"
IUSER=$1

ping -c 1 -W 1 baidu.com &>/dev/null
if [ $? -ne 0 ];then echo "Network err!" ; exit 1; fi
#User testing
if [ `whoami` != "root" ];then  
	echo "Is not the root user !"
	echo "Please switch the root user !"
	exit 1;
fi

#Version testing
echo "SYS_version test"
sysVer="`cat /etc/issue.net`"
nowVer="Ubuntu 22.04.2"
if [[  ${sysVer} =~ ${nowVer} ]];then
        echo "SYS_version ok"
else
        echo "SYS_version err"
	exit 1;
fi

if [ ! -f ./sh/rar/jdk.7z ] ;then
echo "download jdk" 
	wget -P ./sh/rar/ http://simba.whhsn.cn:7374/init/doc/jdk.7z
        if [ $? -eq 0 ];then
          echo -e "jdk安装包下载成功                    \033[32m 完成 \033[0m"
          date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/start.log
          echo "jdk安装包下载成功--------------------[完成]" >> /home/start.log
          echo "###########################################" >> /home/start.log
        else
          echo -e "jdk安装包下载失败                    \033[31m 失败 \033[0m"
          date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/stop.log
          echo "jdk安装包下载失败--------------------[失败]" >> /home/stop.log
          echo "###########################################" >> /home/stop.log
          exit 1
        fi
fi

if [ ! -f ./sh/rar/mysql-5.7.zip ] ;then
echo "download mysql" 
	wget -P ./sh/rar/ http://simba.whhsn.cn:7374/init/doc/mysql-5.7.zip
        if [ $? -eq 0 ];then
          echo -e "mysql安装包下载成功               \033[32m 完成 \033[0m"
          date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/start.log
          echo "mysql安装包下载成功------------------[完成]" >> /home/start.log
          echo "###########################################" >> /home/start.log
        else
          echo -e "mysql安装包下载失败               \033[31m 失败 \033[0m"
          date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/stop.log
          echo "mysql安装包下载失败------------------[失败]" >> /home/stop.log
          echo "###########################################" >> /home/stop.log
          exit 1
        fi
fi

#File detection
#if [ ! -f ./sh/rar/jdk.7z -o ! -f ./sh/doc/javahome -o ! -f ./sh/doc/whhsn.x -o ! -f ./sh/doc/whhsn.y ] ;then
#		
#		if [ ! -f ./sh/rar/jdk.7z ];then
#			FA=" sh/rar/jdk \n"
#		fi
#		if [ ! -f ./sh/doc/javahome ];then
#			FB=" sh/doc/javahome \n"
#		fi		
#		if [ ! -f ./sh/doc/whhsn.x ] ;then
#			FC=" sh/doc/whhsn.x \n"
#		fi
#		if [ ! -f ./sh/doc/whhsn.y ] ;then
#			FD=" sh/doc/whhsn.y \n"
#		fi
#		echo -e "file not found :\n${FA}${FB}${FC}${FD}"
#		echo 'check_ubuntu install ok' >>/home/start.log
#		exit 1;
#fi

mv /etc/apt/sources.list /etc/apt/sources.list-bak
cp /root/init.ubuntu22/sh/doc/sources.list /etc/apt/
apt update 
if [ $? -eq 0 ];then
  echo -e "数据源更新成功                    \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/start.log
  echo "指定serve版本安装成功----------------[完成]" >> /home/start.log
  echo "###########################################" >> /home/start.log
else
  echo -e "数据源更新失败                    \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/stop.log
  echo "指定serve版本安装失败-----------------[失败]" >> /home/stop.log
  echo "###########################################" >> /home/stop.log
  exit 1
fi
 
echo "update ok" 
echo "###########################################" >> /home/start.log
echo "#################  update ok ##############" >> /home/start.log
echo "###########################################" >> /home/start.log

