#!/bin/bash
# jdkPath="/usr/lib/jvm"
# mkdir -p $jdkPath

whhsn="/tmp/whhsn.x"
if [ -f "$whhsn" ]; then
  echo "<<============ JDK环境已安装 ============>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "<<============ JDK环境已安装 ============>>" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi

echo "<<============ 安装JDK[开始] ============>>"

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

username='simba'

HOME_PATH="/home/simba/simba-server/server"
mkdir -p ${HOME_PATH}/{backup,logs,etc}
cat ./doc/whhsn.x > /tmp/whhsn.x
cat ./doc/whhsn.y > /tmp/whhsn.y
cat /tmp/whhsn.x > ${HOME_PATH}/autostartup.sh
cat /tmp/whhsn.y > ${HOME_PATH}/stop.sh
chmod +x ${HOME_PATH}/*.sh

egrep "^$username" /etc/passwd 
if [ $? -eq 0 ]; then
    chown ${username}:${username} -R /home/simba
fi

jdkfile="./rar/jdk.7z"
yum -y remove java-1.7.0-openjdk* java-1.8.0-openjdk* 
if [ ! -f "$jdkfile" ]; then
  wget -P ./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' >> /root/init.centos7/init.log
    echo "JDK下载------------------------------[完成]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
  else
    echo -e "JDK下载                        \033[31m 失败 \033[0m"
    date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
    echo "JDK下载------------------------------[失败]" >> /root/init.centos7/init.log
    echo "###########################################" >> /root/init.centos7/init.log
    exit 1;
  fi
else 
  echo "已下载jdk"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "已下载jdk" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
fi
yum -y install p7zip 
# unzip  ./rar/jdk.zip -d /usr/lib/jvm 
7za x ./rar/jdk.7z -o/usr/lib/jvm 
cp /etc/profile /etc/profile.backup
cat ./doc/javahome >> /etc/profile
echo " " >> /etc/profile
chmod +x /usr/lib/jvm/java/bin/*
source /etc/profile

java -version
if [ $? -eq 0 ] ; then 
  echo "<<=========== 安装JDK[完成] =============>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "<<=========== 安装JDK[完成] =============>>" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
else 
  echo "<<=========== 安装JDK[失败] =============>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /root/init.centos7/init.log
  echo "<<=========== 安装JDK[失败] =============>>" >> /root/init.centos7/init.log
  echo "###########################################" >> /root/init.centos7/init.log
  exit 1;
fi
