#!/bin/sh
if [ $# != 1  ] ; then
  echo "需要1个参数"
  echo "stable: 同步代码到 stable 服务器上，作为正式服使用"
  echo "alpha: 同步代码到 alpha 服务器上，作为测试服供服务开发使用"
  echo "dev: 同步代码到 dev 服务器上，作为测试服供服务开发使用"
  echo "monitor: 同步代码到 monitor 服务器上，用做监控和压力测试"
  echo "distribute: 同步代码到 distribute 服务器上，用做监控和压力测试"
  echo "sq1: 同步代码到 sq1 服务器上，用做 37 功能测试服 game-server 部署"
  exit 1;
fi

port=22
env=${1}

if [ ${1} == 'stable' ] ; then
  destUrl="root@zyz:/root/zyz/"
elif [ ${1} == 'alpha' ] ; then
  destUrl="root@zyzalpha:/root/zyz/"
elif [ ${1} == 'dev' ] ; then
  destUrl="root@zyzdev:/root/zyz/"
elif [ ${1} == 'isbn' ] ; then
  destUrl="root@zyzisbn:/root/zyz/"
elif [ ${1} == 'monitor' ] ; then
  destUrl="root@zyzmon:/root/zyz/"
elif [ ${1} == 'monitor2' ] ; then
  destUrl="root@zyzmon2:/root/zyz/"
elif [ ${1} == 'distribute' ] ; then
  destUrl="root@zyzdistribute:/root/zyz/"
elif [ ${1} == 'sq1' ] ; then
  destUrl="root@sq1:/root/zyz/"
  port=3737
elif [ ${1} == 'sq2' ] ; then
  destUrl="root@sq2:/root/zyz/"
  port=3737
  env='sq2'
elif [ ${1} == 'sq3' ] ; then
  destUrl="root@sq3:/root/zyz/"
  port=3737
  env='sq3'
elif [ ${1} == 'sq4' ] ; then
  destUrl="root@sq4:/root/zyz/"
  port=3737
  env='sq7'
elif [ ${1} == 'sq5' ] ; then
  destUrl="root@sq5:/root/zyz/"
  port=3737
  env='sq7'
elif [ ${1} == 'sq6' ] ; then
  destUrl="root@sq6:/root/zyz/"
  port=3737
  env='sq4'
elif [ ${1} == 'sq7' ] ; then
  destUrl="root@sq7:/root/zyz/"
  port=3737
  env='sq7'
elif [ ${1} == 'sq8' ] ; then
  destUrl="root@sq8:/root/zyz/"
  port=3737
  env='sq7'
elif [ ${1} == 'sq9' ] ; then
  destUrl="root@sq9:/root/zyz/"
  port=22
  env='sq9'
elif [ ${1} == 'sq10' ] ; then
  destUrl="root@sq10:/root/zyz/"
  port=22
  env='sq9'
else
  echo "需要一个参数指明服务器"
  exit 1;
fi

echo "|*********************|\n"

if branch=$(git symbolic-ref --short -q HEAD)
then
  echo "git on branch $branch \n"
else
  echo "git not on any branch \n"
fi
echo "|*********************|\n"

echo "and config.json is: \n"
node ./config.js ${env}
cat './game-server/config.json'
echo "\n"
echo "|*********************|\n"

echo "and server_const is \n"
cat './shared/resource/jsons/server_const.json' | grep DEBUG_PAY
cat './shared/resource/jsons/server_const.json' | grep DEBUG_TIME
cat './shared/resource/jsons/server_const.json' | grep CHECK_WORD
cat './shared/resource/jsons/server_const.json' | grep CAN_PAY
cat './shared/resource/jsons/server_const.json' | grep SKIP_ENCODE
echo "\n"
echo "|*********************|"

read -r -p "Are You Sure? [Y/n]" input

case $input in
  [yY][eE][sS]|[yY])
    echo "Yes"
    rsync -av --include '.babelrc' --include '.eslintrc.js' --exclude-from='exclude-file.txt' --progress --inplace --no-owner --no-group --rsh='ssh -p'${port} . ${destUrl}
    ;;
  [nN][oO]|[nN])
    echo "No"
    ;;
  *)
    echo "Invalid input"
    ;;
esac

git checkout ./game-server/config.json
git checkout ./gm-server/config/env
git checkout ./web-server/config/env
