#!/bin/bash

OBS_URL="https://cloudtable-publish.obs.myhwclouds.com/"

#step 0 使用说明
if [ $# -ne 1 -a $# -ne 4 -o x$1 = x"help" ];then
	echo "usage:"
	echo "	source quick_start_hbase_shell '\$ZOOKEEPRT_ADDRESS'"
	echo "	source quick_start_hbase_shell '\$ZOOKEEPRT_ADDRESS' '\$USER_NAME' '\$ACCESS_KEY' '\$SECRET_KEY'"
	echo "	source quick_start_hbase_shell help"
	return 0
else
	#考虑是否检查地址有效性？
	ZOOKEEPRT_ADDRESS=$1
	USER_NAME=$2
	ACCESS_KEY=$3
	SECRET_KEY=$4
fi

#step 1 配置/etc/hosts
echo "Configurate /etc/hosts."
if [ ! -f "/etc/hostname" ];then
	HOST_NAME=`hostname`
else
	HOST_NAME=`cat /etc/hostname`
fi

#避免文件存在，文件中无主机名
if [ x$HOST_NAME = x ];then
	HOST_NAME=`hostname`.novalocal
	echo "$HOST_NAME" > /etc/hostname
fi

hostname $HOST_NAME

sed -i "/$HOST_NAME/d" /etc/hosts
for item in `ifconfig | sed -En 's/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'`
do 
	echo "$item $HOST_NAME" >> /etc/hosts
done

#step 2 安装JDK
echo "install jdk-8u151-linux."
if [ ! -d "/usr/java" ];then
	mkdir /usr/java
fi

if [ -d "/usr/java/jdk1.8.0_151" ];then 
	rm -rf /usr/java/jdk1.8.0_151
fi

BIT=`getconf LONG_BIT`
if [ $BIT -eq "32" ];then
	PKG_NAME="jdk-8u151-linux-i586.tar.gz"
else
	PKG_NAME="jdk-8u151-linux-x64.tar.gz"
fi

#下载包
if [ ! -f $PKG_NAME ];then
	curl -O -k $OBS_URL$PKG_NAME
fi
#tar安装jdk
tar zxvf $PKG_NAME -C /usr/java > /dev/null

echo "Configurate /etc/profile."

#检查/etc/profile是否存在JAVA环境变量配置，如果存在删除
sed -i '/JAVA_HOME.*/d' /etc/profile
sed -i '/PATH=.*JAVA_HOME.*/d' /etc/profile
sed -i '/CLASSPATH=.*JAVA_HOME.*/d' /etc/profile
sed -i '/export.*JAVA_HOME.*/d' /etc/profile

#配置JAVA最新环境变量
sed -i '$a\JAVA_HOME="/usr/java/jdk1.8.0_151"\nPATH=$JAVA_HOME/bin:$PATH\nCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar\nexport JAVA_HOME PATH CLASSPATH' /etc/profile
source /etc/profile

#step 3 下载CloudTable客户端，
if [ ! -f "hbase-1.3.1-bin.tar.gz" ];then
	curl -O -k $OBS_URL"hbase-1.3.1-bin.tar.gz"
fi

echo "Configurate hbase/conf/hbase-site.xml"
tar zxvf hbase-1.3.1-bin.tar.gz > /dev/null
\cp -r hbase-1.3.1-bin/* .
rm -rf hbase-1.3.1-bin/

#step 4 配置HBase客户端hbase-site.xml
sed -i '/.*hbase.zookeeper.quorum.*/{n;s/<value><\/value>/'"<value>$ZOOKEEPRT_ADDRESS<\/value>"'/}' ./hbase/conf/hbase-site.xml
sed -i -r ':1;N;$!b1;/mapreduce.cluster.local.dir/!s/<\/configuration>/\n    <property>\n\t<name>mapreduce.cluster.local.dir<\/name>\n\t<value>\${hadoop.tmp.dir}\/mapred\/local<\/value>\n    <\/property>\n&/' ./hbase/conf/hbase-site.xml

if [ $# -ne 1 ];then
	sed -i '30,${/<!--/d}' ./hbase/conf/hbase-site.xml
	sed -i '30,${/-->/d}' ./hbase/conf/hbase-site.xml
	sed -i '/.*cloudtable.iam.username.*/{n;s/<value><\/value>/'"<value>$USER_NAME<\/value>"'/}' ./hbase/conf/hbase-site.xml
	sed -i '/.*cloudtable.iam.accesskey.*/{n;s/<value><\/value>/'"<value>$ACCESS_KEY<\/value>"'/}' ./hbase/conf/hbase-site.xml
	sed -i '/.*cloudtable.iam.secretkey.*/{n;s/<value><\/value>/'"<value>$SECRET_KEY<\/value>"'/}' ./hbase/conf/hbase-site.xml
fi

#step 5 配置GeoMesa客户端hbase-site.xml
echo "Configurate geomesa-hbase_2.11-1.3.4/conf/hbase-site.xml"
\cp hbase/conf/hbase-site.xml geomesa-hbase_2.11-1.3.4/conf/

#step 6 启动HBase shell
hbase/bin/hbase shell

