#!/bin/bash
#
#set -xeuo pipefail # 参考: https://mp.weixin.qq.com/s/VmM_U4RefRBHwIw8NegC8Q


# 运行环境env
if [[ X"$1" == X"" ]]; then
    echo "env cannot be empty"
    exit 1
fi

# 任务名称
if [[ X"$2" == X"" ]]; then
    echo "controller name cannot be empty"
    exit 1
fi

# method名称
if [[ X"$3" == X"" ]]; then
    echo "method name cannot be empty"
    exit 1
fi


environment=${1}
#php.ini要根据环境去修改
if [[ X"${environment}" == X"prod" || X"${environment}" == X"prod_internal" ]];then
    sed -i "s#yaf.environ=dev#yaf.environ=prod#g" ini/php.ini
elif [[ X"${environment}" == X"perf" || X"${environment}" == X"perf_internal" ]];then
    sed -i "s#yaf.environ=dev#yaf.environ=perf#g" ini/php.ini
elif [[ X"${environment}" == X"test" || X"${environment}" == X"test_internal" ]];then
    sed -i "s#yaf.environ=dev#yaf.environ=test#g" ini/php.ini
fi

\cp -f ini/php.ini /etc/php.ini

rsyslogd >/dev/null 2>&1


if [[ -e "/usr/share/zoneinfo/Asia/Shanghai" ]]; then
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
fi

# fix dir permissions
chmod -R 755 /home/services/api.go2yd.com/logs

sysctl -w net.core.somaxconn=65535


php api.go2yd.com/htdocs/Website/public/job.php "$2" "$3" "$4"

ret=$?

if [ $ret -ne 0 ]; then
  echo "Build failed"
  exit $ret
else
  echo "Build success"
fi

exit 0