# 说明: 本脚本为docker的启动脚本, 执行此脚本请传入2个参数
# 参数个数为3: $1->环境, $2->端口 $3->域名前缀（metro-test.go2yd.com）
# 环境：外网两个环境 内网两个环境 内网环境且不对外test_internal
# 外网环境 prod/perf/test
# 内网环境 prod_internal/perf_internal/test_internal

if [ $# -lt 3 ]; then
    echo "env cannot empty"
    exit -1
fi

environment=${1}
port=${2}
domain_prefix=${3}
#运行时配置内核参数
sysctl -w net.core.somaxconn=65535 net.ipv4.ip_local_port_range='1024 65530' vm.nr_hugepages=512

#close transparent_hugepage 是否启用透明大页[never]表示透明大页禁用
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

#start_script.done is the mark when service start
rm -f /home/services/api.go2yd.com/logs/start_script.done

#load the config file
if [ ! -d "/etc/php-fpm.d/" ]; then
  mkdir /etc/php-fpm.d/
fi
if [ ! -d "/var/run/php-fpm/" ]; then
  mkdir /var/run/php-fpm/
fi

\cp -f /home/services/api.go2yd.com/htdocs/Website/deploy/start_env/php-fpm.conf /etc/php-fpm.conf
\cp -f /home/services/api.go2yd.com/htdocs/Website/deploy/start_env/api.go2yd.com.conf /etc/php-fpm.d/api.go2yd.com.conf

# 宿主机ip不一定为172.17.0.1
# -z string长度是否为0
if [ -z "${YIDIAN_LOCAL_IP}" ]; then
    printf "\nenv[YIDIAN_LOCAL_IP]=172.17.0.1\n" >> /etc/php-fpm.d/api.go2yd.com.conf
else
    printf "\nenv[YIDIAN_LOCAL_IP]=%s\n" "${YIDIAN_LOCAL_IP}" >> /etc/php-fpm.d/api.go2yd.com.conf
fi

#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

# choose the php ini file for different env
\cp -f ini/php.ini /etc/php.ini

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

#code collect coverage for FT
if [ "$collect_coverage" = "true" ]; then
    sh /home/services/collect_coverage/start_collect_coverage.sh
fi

#set the pool of php-fpm
sed -i "/^\[.*\]$/s#api#${domain_prefix}#g" /etc/php-fpm.d/api.go2yd.com.conf
#rsyslogd启动日志 系统中的绝大多数日志文件是由 rsyslogd 服务来统一管理的只要各个进程将信息给予这个服务，它就会自动地把日志按照特定的格式记录到不同的日志文件中。也就是说，采用 rsyslogd 服务管理的日志文件，它们的格式应该是统一的。
rsyslogd > /dev/null 2>&1

cp -f /home/services/logrotate.conf /etc/logrotate.d/php-fpm

mv /etc/anacrontab /etc/anacrontab.bak  # 取消系统调度logrotate，采用自定义的crontab时间执行
#crontab
nohup /usr/sbin/crond >crond.nohup &
crontab /home/services/crontab.conf

#create the log file, fix dir permissions
if [ ! -d "/etc/php-fpm.d/" ]; then
    mkdir /home/services/api.go2yd.com/logs/
fi
if [ ! -f "/home/services/api.go2yd.com/logs/php-error.log" ];then
    touch /home/services/api.go2yd.com/logs/php-error.log
fi
if [ ! -f "/home/services/api.go2yd.com/logs/access.log" ];then
    touch /home/services/api.go2yd.com/logs/access.log
fi
if [ ! -f "/home/services/api.go2yd.com/logs/fpm-error.log" ];then
    touch /home/services/api.go2yd.com/logs/fpm-error.log
fi
if [ ! -f "/home/services/api.go2yd.com/logs/fpm-slow.log" ];then
    touch /home/services/api.go2yd.com/logs/fpm-slow.log
fi
chmod -R 777 /home/services/api.go2yd.com/logs
chown -R nobody:nobody /home/services/api.go2yd.com/logs

#add apc.php to the web root
#可选PHP缓存 APC的缓存分两部分:系统缓存和用户数据缓存
cp -f /home/services/api.go2yd.com/htdocs/Website/deploy/start_env/apc.php /home/services/api.go2yd.com/htdocs/Website/debug/apc.php

# 线上需要删除这个标记的方法if not for internal use, remove flagged codes
if [[ X"${environment}" != X"test_internal" && X"${environment}" != X"prod_internal" ]];then
    grep -l '@mark FOR_INTERNAL_USE_ONLY' /home/services/api.go2yd.com/htdocs/Website/*/*/*/*/* | xargs rm
fi

#  日志
sed -i "s/service_name: 'api.go2yd.com'/service_name: '${domain_prefix}.go2yd.com'/g" filebeat-log.yml
# filebeat start
chmod go-w /home/services/filebeat-log.yml
nohup /filebeat-6.3.1-linux-x86_64/filebeat -c /home/services/filebeat-log.yml &

while true; do
    nohup /usr/sbin/php-fpm -F &
    fpm_pid=$!
    sleep 5
    #docker stop优雅关闭php-fpm
    trap "kill -3 $fpm_pid;exit 0" 15
    # 检测php-fpm是否启动成功 端口9000是容器内固定的值
    sh api_checker/api_checker.sh 9000 {$domain_prefix}
    if [ "$?" != 0 ]; then
        if [ ! -f /home/services/api.go2yd.com/logs/start_script.done ]; then
            exit 2
        else
            kill $fpm_pid
            sleep 10
        fi
    else
        touch /home/services/api.go2yd.com/logs/start_script.done
    fi
    wait $fpm_pid
    exit 3
done;