#!/bin/bash

# Prerequisite: 
# 1. Put the offline package under /opt/ and assign the privileges for this package to all users.
	# Example command: 
	# cd /opt/ && wget http://172.16.5.148:8080/packages/tiem-enterprise-server-v1.0.0-beta.3-linux-amd64.tar.gz
# 2. Install TiUP as root
	# Example command: 
	# curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
# 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 offline_deploy_tiem.sh /opt/tiem-enterprise-server-v1.0.0-beta.3-linux-amd64.tar.gz /opt/tiem-mini.yaml https://tiup-mirrors.pingcap.com

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

TIEM_PACKAGE_PATH=$1
TIEM_DEPLOYMENT_TOPO=$2
TIDB_MIRROR=$3
TIEM_PACKAGE_FILE_BASENAME_NO_EXTENSION=$(basename -s .tar.gz $TIEM_PACKAGE_PATH)
TIEM_PACKAGE_MIRROR_PATH=/home/tidb/$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 package to all users started #####"
chmod 777 $TIEM_PACKAGE_PATH
echo "##### assign privileges for offline 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 tidb
echo "##### add user tiem finished #####"

echo "##### install TiUP to /usr/local/bin/ started #####"
install -Dm755 ~/.tiup/bin/tiup /usr/local/bin/
echo "##### install TiUP to /usr/local/bin/ finished #####"

su - tidb << EOF

echo "##### unpack package started #####"
tar zvxf $TIEM_PACKAGE_PATH
echo "##### unpack package finished #####"

echo "##### mkdir & set mirror for TiEM installer started #####"
mkdir -p /home/tidb/.tiuptiem/bin
TIUP_HOME=/home/tidb/.tiuptiem tiup uninstall tiem --all
TIUP_HOME=/home/tidb/.tiuptiem tiup mirror set $TIEM_PACKAGE_MIRROR_PATH
echo "##### mkdir & set mirror for TiEM installer finished #####"

echo "##### TODO: set official TiUP mirror and install cluster component #####"
echo "##### mkdir & set mirror for TiDB installer started #####"
mkdir -p /home/tidb/.tiup/bin
TIUP_HOME=/home/tidb/.tiup tiup mirror set $TIDB_MIRROR
echo "##### mkdir & set mirror for TiDB installer finished #####"


EOF

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

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

