transIp="http://54.168.164.26:8080/server"
# npmRepoUrl="https://registry.npm.taobao.org"
npmRepoUrl="https://mirrors.huaweicloud.com/repository/npm/"
option=$1
server=$2
env=$3
branch=$4

version=''
portEx=''
serverEnv=''

echo "$(tput setaf 4) $(tput bold)"
echo "Option: $option      --[start(启动), stop(停止), init(初始化+启动), update(更新)]"
echo "Server: $server      --[admin(管理), agent(代理), control(录入), game(游戏), account(手工帐), gate(网关), gate_control(远程录入网关) rpc(RPC), inner(测试集合包)]"
echo "Env: $env            --[prod(正式), slave(正式从服务)), alpha(测试), single(单机配置), phone(电投)]"
echo "Branch: $branch      --[w2(管理), w3(代理)]"

echo "$(tput setaf 1)"
if [ "$option" != "start" ] && [ "$option" != "stop" ] && [ "$option" != "init" ] && [ "$option" != "update" ] && [ "$option" != "restart" ]; then 
    echo "Option 参数错误: $option..."
    exit 0;
fi

if [ "$server" != "admin" ] && [ "$server" != "agent" ] && [ "$server" != "control" ] && [ "$server" != "game" ] && [ "$server" != "payment" ] && [ "$server" != "account" ] && [ "$server" != "tools" ] && [ "$server" != "gate" ] && [ "$server" != "rpc" ] && [ "$server" != "gate_control" ] && [ "$server" != "gate_account" ] && [ "$server" != "file_upload" ] && [ "$server" != "inner" ] && [ "$server" != "share" ] && [ "$server" != "homecms" ]; then 
    echo "Server 参数错误: $server..."
    exit 0;
fi

if [ "$env" != "prod" ] && [ "$env" != "alpha" ] && [ "$env" != "single" ] && [ "$env" != "phone" ] && [ "$env" != "slave" ]; then 
    echo "Env 参数错误: $env..."
    exit 0;
fi

if [ "$branch" == "w2" ]; then
    portEx='02'
    serverEnv=$env.'w2'
    version='master'
elif [ "$branch" == "w3" ]; then
    portEx='03'
    serverEnv=$env.'w3'
    version='master'
elif [ "$branch" == "w2d" ]; then
    portEx='12'
    serverEnv='dev'
    version='develop'
elif [ "$branch" == "w3d" ]; then
    portEx='13'
    serverEnv='dev'
    version='develop'
elif [ "$branch" == "w2f" ]; then
    portEx='22'
    serverEnv='feature'
    version='feature'
elif [ "$branch" == "w3f" ]; then
    portEx='23'
    serverEnv='feature'
    version='feature'
else
    echo "\033[31m Branch 参数错误: $branch..."
    exit 0;
fi

echo "$(tput setaf 3) CmdInfo: $option $server $env $branch $version ... $(tput sgr0)"

currentDate=`date +"-%m-%d"`

start(){
    serverName=$1
    startOption='start'
    stopOption='stop'
    
    cd $serverName
    echo "$(tput setaf 2)$(tput bold) Stop Server...$serverName $(tput sgr0)"

    if [ "$env" == "alpha" ]; then  
        if [ "$serverName" == "gate" ]; then
            startOption='startEx -- --port=30'$portEx' --title=ninth-'$serverName$branch
            stopOption='stopEx -- --title=ninth-'$serverName$branch
        elif [ "$serverName" == "rpc" ]; then
            startOption='startEx -- --port=20'$portEx' --title=ninth-'$serverName$branch
            stopOption='stopEx -- --title=ninth-'$serverName$branch
        fi
    fi

    if [ ! -d "framework" ]; then     
        EGG_SERVER_ENV=$serverEnv npm run $stopOption 
        echo "$(tput setaf 2)$(tput bold) Start Server...$serverName $(tput sgr0)"
        EGG_SERVER_ENV=$serverEnv npm run $startOption
        cd "../"
        return 1
    else
        cd "server/$serverName/"
        if [ "$serverName" == "gate" ]; then
            nginx -s stop
            sleep 10
            EGG_SERVER_ENV=$serverEnv npm run $stopOption 
        else
            nginx -s stop
            sleep 5
            EGG_SERVER_ENV=$serverEnv npm run $stopOption 
        fi
        
        echo "$(tput setaf 2)$(tput bold) Start Server...$serverName $(tput sgr0)"
        nginx
        EGG_SERVER_ENV=$serverEnv npm run $startOption    
        cd "../../../"
    fi    
}
stop(){
    serverName=$1   
    cd $serverName 
    echo "$(tput setaf 2)$(tput bold) Stop Server...$serverName $(tput sgr0)"

    daemonOption='daemon'
    stopOption='stop'
    if [ "$env" == "alpha" ]; then
        if [ "$serverName" == "gate" ]; then
            stopOption='stopEx -- --title=ninth-'$serverName$branch
            daemonOption='daemonEx -- --port=33'$portEx' --title=ninth-'$serverName$branch
        elif [ "$serverName" == "rpc" ]; then
            stopOption='stopEx -- --title=ninth-'$serverName$branch
        fi
    fi

    if [ ! -d "framework" ]; then
        EGG_SERVER_ENV=$serverEnv npm run $stopOption 
        cd "../"
        return 1
    else
        cd "server/$serverName/"
        if [ "$serverName" == "gate" ]; then
            nginx -s stop
            sleep 10
            EGG_SERVER_ENV=$serverEnv npm run $stopOption
            nginx
            EGG_SERVER_ENV=$serverEnv npm run $daemonOption        
            sleep 30
            nginx -s stop
        else        
            nginx -s stop
            sleep 5
            EGG_SERVER_ENV=$serverEnv npm run $stopOption 
            nginx
        fi
        cd "../../../"
    fi    
}
init(){
    serverName=$1
    rm -rf $serverName
    tar -xf $serverName.tar
    npm config set registry $npmRepoUrl

    cd $serverName
    if [ ! -d "framework" ]; then
        echo "$(tput setaf 2)$(tput bold) Init Server...$serverName $(tput sgr0)"
        EGG_SERVER_ENV=$serverEnv npm run init
        cd "../"
        return 1
    fi
    cd "framework/"
    echo "$(tput setaf 2)$(tput bold) Init Server Framework...$serverName $(tput sgr0)"
    EGG_SERVER_ENV=$serverEnv npm run init
    cd "../server/$serverName/"
    echo "$(tput setaf 2)$(tput bold) Init Server...$serverName $(tput sgr0)"
    EGG_SERVER_ENV=$serverEnv npm run init
    cd "../../../"
}

if  [ "$option" == "restart" ]; then
    start $server
elif  [ "$option" == "start" ]; then
    start $server    
elif [ "$option" == "stop" ]; then    
    stop $server
elif [ "$option" == "init" ]; then
    init $server
    start $server
elif [ "$option" == "update" ]; then
    rm -rf $server.tar
    wget -O $server.tar $transIp/$version/$server.tar
    cp $server.tar $server$currentDate.tar

    if [ "$server" == "inner" ]; then
        rm -rf $server
        tar -xf $server.tar
        cd $server/

        init "admin"
        start "admin"

        init "agent"
        start "agent"

        init "control"
        start "control"

        init "game"
        start "game"

        init "payment"
        start "payment"

        init "account"
        start "account"

        init "gate_account"
        start "gate_account"
    else
        init $server
        start $server
    fi
fi
