loop_process()
{
	echo;
	echo $1;
	while [ 1 ]
	do
		job=$(jobs | gawk '!/Running/{print 0}')
		if [ "$job" == "0" ];
		then
			break;
		fi
		echo -e "..\c";
		sleep 0.5
	done
	echo;
}


#if [ -z $1 ] 
#then 
#	echo "usage:: ./install_java.sh {download_url}"
#	exit;
#else
#	D_URL="$1"
#fi
#
#curl -o jdk-x64.tar.gz "$D_URL"

java_home_path="/usr/local/java"

if [ ! -d $java_home_path ]
then
	mkdir $java_home_path
else
	echo "file path has exist:$java_home_path"
fi

cp jdk-x64.tar.gz $java_home_path

cd $java_home_path

tar zxvf jdk-x64.tar.gz >> /dev/null & 

loop_process "unpack from jdk-x64.tar.gz to $java_home_path"

jdkname=`ls |gawk "/jdk/ {print $1}"|gawk '!/tar/ {print $1}'|gawk "NR==1{print $1}" `

ln -s "$java_home_path/$jdkname/bin/java" /bin/

java_home="/usr/local/java/${jdkname}";

e_pro=/etc/profile;

cat $e_pro |gawk '!/JAVA_HOME/{print $0}' > "${e_pro}.new"

echo 'export JAVA_HOME=/usr/local/java/'${jdkname}'
export JRE_HOME=${JAVA_HOME}/jre  
export CLASSPATH=.:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib  
export PATH=${JAVA_HOME}/bin:$PATH' >> "${e_pro}.new"

if [ ! -f "${e_pro}.back" ]
then
	cp $e_pro "${e_pro}.back"
fi

mv "${e_pro}.new" $e_pro

echo "install $jdkname success"

