#!/bin/bash
action=""
ak=""
sk=""
systemupdate=false

isZh=false
if [ $(echo ${LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then isZh=true; fi

function Echo() {
	if [ "${isZh}" = true ]; then
	  echo -e "$2";
	else
	  echo -e "$1";
	fi
}

function check_smc_agent_running() {
    if [ $(ps aux | grep smc_agent | grep -v grep | wc -l) -gt 0 ]; then
        true
    else
        false
    fi
}

while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        start)
            action="start"
            shift
            ;;
        stop)
            action="stop"
            shift
            ;;
        -ak)
            ak="$2"
            shift
            shift
            ;;
        -sk)
            sk="$2"
            shift
            shift
            ;;
        -systemupdate)
            systemupdate=true
            shift
            ;;
        *)
            Echo "please use $0 start/stop to execute" "请使用 $0 start/stop 执行"
            Echo "Usage: $0 start [-ak <value>] [-sk <value>]" "Usage: $0 start [-ak <value>] [-sk <value>]"
            Echo "       $0 stop" "       $0 stop"
            exit 1
            ;;
    esac
done

cd $(dirname $0)
if [ "$action" == "start" ]; then
    if check_smc_agent_running; then
        Echo "The migration agent is running and does not need to be started again" "迁移agent正在运行，无需再次启动"
        exit 1
    fi
    Echo "Please note that the migration agent will collect system configuration information and upload it to the SMC console to create a migration task" "请注意，迁移agent将会收集系统配置信息并上传至SMC控制台以便创建迁移任务"
    if [ "${ak}" = "" ]; then
        ak=$(cat user_cfg.json | grep '"ak"' | awk '{print $2}' | sed 's/"//g' | sed 's/,//g')
    fi
    if [ "${sk}" = "" ]; then
        sk=$(cat user_cfg.json | grep '"sk"' | awk '{print $2}' | sed 's/"//g' | sed 's/,//g')
    fi
    if [ "${ak}" = "" ] || [ "${sk}" = "" ]; then
      read -p "$(Echo "Please enter AK: " "请输入AK：")" ak
      if [ "${ak}" = "" ]; then
        Echo "AK can not be empty" "AK不能为空"
        exit 1
      fi
      read -p "$(Echo "Please enter SK: " "请输入SK：")" sk
      if [ "${sk}" = "" ]; then
          Echo "SK can not be empty" "SK不能为空"
          exit 1
      fi
    fi
    sed -i "s/\"ak\":\x20*\".*\"/\"ak\":\x20\"${ak}\"/"  user_cfg.json
    sed -i "s/\"sk\":\x20*\".*\"/\"sk\":\x20\"${sk}\"/"  user_cfg.json
    Echo "The migration agent is started, please go to the SMC console later to view the migration source and create a migration task" "迁移agent启动，请稍后前往SMC控制台查看迁移源，创建迁移任务"
    if [ "${systemupdate}" = true ]; then
      nohup ./smc_agent -systemupdate > smc_manage.log 2>&1 &
    else
      nohup ./smc_agent > smc_manage.log 2>&1 &
    fi
elif [ "$action" == "stop" ]; then
    if ! check_smc_agent_running; then
        Echo "The migration agent is not running, so there is no need to stop it" "迁移agent未运行，无需停止"
        exit 1
    fi
    Echo "Migration agent stopped" "迁移agent停止"
    kill -9 `ps -A -o pid,pgid,cmd | grep smc_agent | grep -v grep | awk '{print "-"$2}'`
    if check_smc_agent_running; then
        ps aux | grep smc_agent | grep -v grep | awk '{print $2}' | xargs kill -9
    fi
else
    Echo "please use $0 start/stop to execute" "请使用 $0 start/stop 执行"
fi
