#!/bin/bash

SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
thanos_dir=$(dirname "$SCRIPT_DIR")
user='compact'
module='compact'
log_dir="logs/thanos-${module}.log"


ps -ef | grep -w "${thanos_dir}/thanos[[:space:]]*${module}" | grep -v 'grep' &>/dev/null
if [ $? -eq 0 ];then
  echo "error: !!!Thanos-${module} server runing!!!"
  exit 10
fi


if [ "`whoami`" != "${user}" ];then
  echo "error: !!!Excute user not ${user}!!!"
  exit 10
fi

if [ ! -f "${thanos_dir}/thanos" ];then
  echo "error: !!!Thanos binary file not exist!!!"
  exit 10
fi

cd ${thanos_dir}

start_para=`cat ${thanos_dir}/script/running_paraments`


if [ -n "${start_para}" ];then
  nohup ${thanos_dir}/thanos ${module} \
  --wait \
  ${start_para} >> ${log_dir} 2>&1 &
else
  nohup ${thanos_dir}/thanos ${module} \
  --wait \ >> ${log_dir}  2>&1 &
fi

sleep 2

ps -ef | grep -w "${thanos_dir}/thanos[[:space:]]*${module}" | grep -v 'grep' &>/dev/null
if [ $? -eq 0 ];then
  echo "success: Thanos-${module} server start"
else
  echo "error: !!!Thanos-${module} server start failed!!!"
  exit 10
fi
