#!/bin/bash

# Prerequisite: 
# 1. Put the offline TiDB package under /opt/
	# Example command: 
	# cd /opt/ && wget http://172.16.5.148:8080/packages/tidb-community-server-v5.2.2-linux-amd64.tar.gz
# 2. Put the offline TiDB package under /opt/
	# Example command: 
	# cd /opt/ && wget http://172.16.5.148:8080/packages/tiem-enterprise-server-v1.0.0-beta.9-linux-amd64.tar.gz
# 3. Prepare a tiem deployment topology yaml to /opt/tiem-mini.yaml
# 4. Put current script in /root/ path of TiEM machine and run it as root privilege, and leave the rest to the script.
	# Example command: 
	# sh local.sh /opt/tidb-community-server-v5.2.2-linux-amd64.tar.gz /opt/tiem-enterprise-server-v1.0.0-beta.9-linux-amd64.tar.gz /opt/tiem-mini.yaml

if [ $# -ne 3 ]
then
    echo "usage: $0 <TiDB package-path> <TiEM package-path> <tiem-deployment-topology-path>"
    exit
fi

TIDB_PACKAGE_PATH=$1
TIEM_PACKAGE_PATH=$2
TIEM_DEPLOYMENT_TOPO=$3

TIDB_PACKAGE_FILE_BASENAME_NO_EXTENSION=$(basename -s .tar.gz $TIDB_PACKAGE_PATH)
TIEM_PACKAGE_FILE_BASENAME_NO_EXTENSION=$(basename -s .tar.gz $TIEM_PACKAGE_PATH)
TIDB_PACKAGE_MIRROR_PATH=/home/tiem/$TIDB_PACKAGE_FILE_BASENAME_NO_EXTENSION
TIEM_PACKAGE_MIRROR_PATH=/home/tiem/$TIEM_PACKAGE_FILE_BASENAME_NO_EXTENSION
TIEM_PACKAGE_VERSION=$(echo $TIEM_PACKAGE_PATH | sed 's/.*tiem-enterprise-server-v\(.*\)-linux-amd64.tar.gz/\1/')

echo "##### assign privileges for offline TiDB package to all users started #####"
chmod 777 $TIDB_PACKAGE_PATH
echo "##### assign privileges for offline TiDB package to all users finished #####"
echo "##### assign privileges for offline TiEM package to all users started #####"
chmod 777 $TIEM_PACKAGE_PATH
echo "##### assign privileges for offline TiEM package to all users finished #####"
echo "##### assign privileges for deployment topology file to all users started #####"
chmod 777 $TIEM_DEPLOYMENT_TOPO
echo "##### assign privileges for deployment topology file to all users finished #####"

echo "##### add user tiem started #####"
useradd tiem
echo "##### add user tiem finished #####"

echo "##### generate identity key started, simply press enter or respond yes if needed #####"
su - tiem -c "ssh-keygen -t rsa"
su - tiem -c "cp /home/tiem/.ssh/id_rsa /home/tiem/.ssh/tiup_rsa"
echo "##### generate identity key finished #####"

su - tiem << EOF

echo "##### install TiDB started #####"
tar zvxf $TIDB_PACKAGE_PATH
sh $TIDB_PACKAGE_FILE_BASENAME_NO_EXTENSION/local_install.sh
source /home/tiem/.bash_profile
echo "##### install TiDB finished #####"

echo "##### install TiEM started #####"
tar zvxf $TIEM_PACKAGE_PATH
mkdir -p /home/tiem/.tiuptiem/bin
TIUP_HOME=/home/tiem/.tiuptiem tiup uninstall tiem --all
TIUP_HOME=/home/tiem/.tiuptiem tiup mirror set $TIEM_PACKAGE_MIRROR_PATH
echo "##### install TiEM finished #####"

EOF

echo "##### deploy TiEM started #####"
su - tiem -c "TIUP_HOME=/home/tiem/.tiuptiem tiup tiem deploy tiem-test $TIEM_PACKAGE_VERSION $TIEM_DEPLOYMENT_TOPO -u root -p"
su - tiem -c "TIUP_HOME=/home/tiem/.tiuptiem tiup tiem start tiem-test"
echo "##### deploy TiEM finished #####"

