######################################################################
#### function: install jdk
####author:danial_chen
####create date:2019-11-07
####modify date:2019-11-07
######################################################################
#/bin/bash
#useage: sh installjdk.sh

set -e
# 目前都是oracle jdk
version="1.8.0_201"
package="jdk-8u201-linux-x64.tar.gz"
arch=$(arch)
if [[ ${arch} = "aarch64" ]]; then
  package="jdk-8u201-linux-arm64-vfp-hflt.tar.gz"
fi

if [ "$JAVA_HOME" = "$USER_INSTALL_DIR$/jdk/jdk${version}" ]; then
  exit
fi

## ENV ##
export LANG=C
pd=$PWD

## install jdk ##
time=$(date +%Y%m%d%H%M%S)
if [ -d "./jdk${version}" ]; then
  rm -rf jdk${version}
fi
tar -zxvf ${package}
cp -rf /etc/profile /etc/profile-$time
# 先删除原来的
sed -i '/JAVA_HOME/d' /etc/profile
export JAVA_HOME=$pd/jdk${version}
if ! grep -q "JAVA_HOME=$pd/jdk${version}" /etc/profile; then
  echo "export JAVA_HOME=$pd/jdk${version}" >> /etc/profile
  echo "PATH=\$JAVA_HOME/bin:\$PATH:\$HOME/bin" >> /etc/profile
fi

set +e
source /etc/profile
set -e

## echo install resault ##
echo "JDK install success"
echo "JDK path: JAVA_HOME=$JAVA_HOME"
