#!/bin/bash

set -u -e
export http_port=7618

dir=$(dirname $0)

function init() {
  error=`wget -q -O - http://127.0.0.1:${http_port}/initialize/initomp`
  echo -e $error
  errcode=`echo $error|grep -q '"errcode": 0'; echo $?`
  # 0表示存在
  if [ $errcode -ne 0 ]; then
    echo -e "init omp failed"
    exit 1
  fi

  echo -e "init omp success"
}

function sync() {
  error=`wget -q -O - http://127.0.0.1:${http_port}/initialize/sync`
  echo -e $error
  errcode=`echo $error|grep -q '"errcode": 0'; echo $?`
  # 0表示存在
  if [ $errcode -ne 0 ]; then
    echo -e "sync cosmic failed"
    exit 2
  fi

  echo -e "sync cosmic success"
}

function sync_middleware() {
  error=`wget -q -O - http://127.0.0.1:${http_port}/initialize/syncmid`
  echo -e $error
  errcode=`echo $error|grep -q '"errcode": 0'; echo $?`
  # 0表示存在
  if [ $errcode -ne 0 ]; then
    echo -e "sync middleware failed"
    exit 3
  fi

  echo -e "sync middleware success"
}

function init_eye() {
  error=`wget -q -O - http://127.0.0.1:${http_port}/initialize/initeye`
  echo -e $error
  errcode=`echo $error|grep -q '"errcode": 0'; echo $?`
  # 0表示存在
  if [ $errcode -ne 0 ]; then
    echo -e "init eye failed"
    exit 3
  fi

  echo -e "init eye success"
}

echo -e "\033[38;4;94m>>>>开始初始化omp\033[0m"
sleep 60
init
# 只有x86才执行中间件托管
if [ $(arch) == "x86_64" ]; then
  echo -e "\033[38;4;94m>>>>开始托管middleware\033[0m"
  sync_middleware
fi
echo -e "\033[38;4;94m>>>>开始同步cosmic\033[0m"
sync
# 只有x86才执行中间件托管
if [ $(arch) == "x86_64" ]; then
  echo -e "\033[38;4;94m>>>>开始初始化监控服务\033[0m"
  init_eye
fi
echo -e "\033[38;5;35m>>>>初始化gpaas完成\033[0m"