#!/bin/bash
#
#*****************************************************
#File Name    : install_jdk.sh
#Author       : guokai
#Mail         : xxx@163.com
#Create Time  : 2022-04-11 11:10:28
#Description  : jdk-8u291
#*****************************************************
DIR=`pwd`
JDK_FILE="jdk-8u291-linux-x64.tar.gz"
JDK_DIR="/usr/local"
color () {
	RES_COL=60
	MOVE_TO_COL="echo -en \\033[${RES_COL}G"
	SETCOLOR_SUCCESS="echo -en \\033[1;32m"
	SETCOLOR_FAILURE="echo -en \\033[1;31m"
	SETCOLOR_WARNING="echo -en \\033[1;33m"
	SETCOLOR_NORMAL="echo -en \E[0m"
	echo -n "$2" && $MOVE_TO_COL
	echo -n "["
	if [ $1 = "success" -o $1 = "0" ] ;then
		${SETCOLOR_SUCCESS}
		echo -n $"  OK  "
	elif [ $1 = "failure" -o $1 = "1" ] ;then
		${SETCOLOR_FAILURE}
		echo -n $"FAILED"
	else 
		${SETCOLOR_WARNING}
		echo -n $"WARNING"
	fi
	${SETCOLOR_NORMAL}
	echo -n "]"
	echo
}


install_jdk () {
if ! [ -f "$DIR/$JDK_FILE" ] ;then
	color 1 "$JDK_FILE 文件不存在"
	wget http://58.220.50.162:7777/download/jdk-8u291-linux-x64.tar.gz && { color 0 "JDK 下载完成";[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR ; } || { color 1 "JDK 下载失败" ; exit; }
elif [ -d $JDK_DIR/jdk ];then
	color 1 "JDK 已经安装"
	exit
else
	[ -d $JDK_DIR ] || mkdir -pv $JDK_DIR
fi
tar xf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8.* jdk

cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\$JAVA_HOME/jre
export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib/
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
sleep 5
source /etc/profile
java -version && color 0 "JDK 安装完成" || { color 1 "JDK 安装失败" ; exit; }

}

install_jdk
