#!/usr/bin/env bash
# 文件名: openresty_install.sh

### 定义变量 开始

BUILD_DIR=/data/build
PREFIX=/opt/openresty
VAR_DIR=${PREFIX}/tmp
CNF_DIR=${PREFIX}/conf
RUN_DIR=${PREFIX}/run
LOG_DIR=${PREFIX}/log

TAG=Unkown-Server
VERSION=3.14
OWNER=nginx
uid=2000
gid=2000

pcre_ver=8.45
pcre_url="https://dl.vqiu.cn/package/nginx/pcre-${pcre_ver}.tar.bz2"

zlib_ver=1.2.13
zlib_url="https://dl.vqiu.cn/package/nginx/zlib-${zlib_ver}.tar.gz"

openssl_ver=1.1.1t
openssl_url="https://www.openssl.org/source/openssl-${openssl_ver}.tar.gz"

openresty_ver=1.21.4.1
nginx_ver=${openresty_ver}
openresty_url="https://openresty.org/download/openresty-${openresty_ver}.tar.gz"

### 定义变量 结束

# 判断当前用户是否为 root,否则退出
if [ ${UID} != "0" ]; then
	echo "Error: Please use root role to install me!"
	exit 67;

fi

yum -y install gcc libxml2-devel libxslt-devel geoip-devel gcc-c++ git

# 创建 $OWNER
id ${OWNER} >/dev/null 2>&1
if [ $? -eq "1" ];
then
	groupadd ${OWNER} -g ${gid}
	useradd -g ${gid} -u ${uid} ${OWNER} -s /sbin/nologin -M
fi

for d in ${VAR_DIR} ${VAR_DIR} ${CNF_DIR} ${RUN_DIR} ${LOG_DIR} ${APP_DIR} ${PREFIX}
do
	mkdir -p ${d}
	chown -R ${OWNER}.${OWNER} ${d}
done

# 创建临时目录
[ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR}
for dir in client_temp proxy_temp fastcgi_temp uwsgi_temp scgi_temp
do
	mkdir -p ${VAR_DIR}/${dir} 2>/dev/null
done
chown -R ${OWNER}.${OWNER} ${VAR_DIR}


cd ${BUILD_DIR}
[[ -f ${BUILD_DIR}/zlib-${zlib_ver}.tar.gz ]] || echo "zlib Downloading..." && curl -sSfL ${zlib_url} -O
[[ -f ${BUILD_DIR}/pcre-${pcre_ver}.tar.bz2 ]] || echo "pcre Downloading..." && curl -sSfL ${pcre_url} -O
[[ -f ${BUILD_DIR}/openssl-${openssl_ver}.tar.gz ]] || echo "openssl Downloading..." && curl -sSL ${openssl_url} -O
[[ -f ${BUILD_DIR}/openresty-${openresty_ver}.tar.gz ]] || echo "openresty Downloading..." && curl -sSL ${openresty_url} -O

unpacket() {
	tar axvf $1 -C ${BUILD_DIR}
}

for pkg in `find ${BUILD_DIR} -type f -name "*tar.*"`
do
	unpacket ${pkg} &
done
wait


mkdir ${BUILD_DIR}/modules

cd ${BUILD_DIR}/modules && git clone https://github.com/google/ngx_brotli.git --recursive


cd ${BUILD_DIR}/openresty-${nginx_ver}
./configure \
    --build=vqiu-$(date +%Y%m%d) \
    --prefix=${PREFIX} \
    --modules-path=${PREFIX}/modules \
    --conf-path=${CNF_DIR}/nginx.conf \
    --user=${OWNER} \
    --group=${OWNER} \
    --error-log-path=${LOG_DIR}/error/error.log \
    --http-log-path=${LOG_DIR}/access/access.log \
    --pid-path=${RUN_DIR}/nginx.pid \
    --lock-path=${RUN_DIR}/nginx.lock \
    --http-client-body-temp-path=${VAR_DIR}/body \
    --http-proxy-temp-path=${VAR_DIR}/proxy \
    --http-fastcgi-temp-path=${VAR_DIR}/fastcgi \
    --http-uwsgi-temp-path=${VAR_DIR}/uwsgi \
    --http-scgi-temp-path=${VAR_DIR}/scgi \
    --lock-path=${RUN_DIR}/nginx.lock \
    --with-zlib=${BUILD_DIR}/zlib-${zlib_ver} \
    --with-pcre=${BUILD_DIR}/pcre-${pcre_ver} \
    --with-openssl=${BUILD_DIR}/openssl-${openssl_ver} \
    --with-openssl-opt=enable-ec_nistp_64_gcc_128 \
    --with-openssl-opt=no-nextprotoneg \
    --with-openssl-opt=no-weak-ssl-ciphers \
    --with-openssl-opt=no-ssl3 \
    --with-threads \
    --with-compat \
    --with-http_dav_module \
    --with-http_v2_module \
    --with-http_ssl_module \
    --with-http_gunzip_module \
    --with-http_secure_link_module \
    --with-http_gzip_static_module \
    --with-http_realip_module \
    --with-http_stub_status_module \
    --with-http_addition_module \
    --with-http_slice_module \
    --with-file-aio \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-stream_realip_module \
    --with-http_geoip_module \
    --with-stream_geoip_module \
    --with-pcre-jit \
    --add-module=${BUILD_DIR}/modules/ngx_brotli \
    --with-cc-opt='-g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -fPIC -D_FORTIFY_SOURCE=2'

sed -i "13s/^#define NGINX_VERSION.*/#define NGINX_VERSION      \"${VERSION}\"/" build/nginx-1.21.4/src/core/nginx.h
sed -i "14s/^#define NGINX_VER.*/#define NGINX_VER          \"${TAG}\/\" NGINX_VERSION/" build/nginx-1.21.4/src/core/nginx.h
    make -j${nproc}
    make install

cat > /etc/profile.d/openrestry.sh<<EOF
export PATH=\$PATH:${PREFIX}/bin/:${PREFIX}/nginx/sbin/
EOF



cat >/usr/lib/systemd/system/openresty.service<<EOF
[Unit]
Description=A dynamic web platform based on Nginx and LuaJIT.
After=network.target

[Service]
Type=forking
PIDFile=${PREFIX}/run/nginx.pid
ExecStartPre=${PREFIX}/bin/openresty -t -q -g 'daemon on; master_process on;'
ExecStart=${PREFIX}/bin/openresty -g 'daemon on; master_process on;'
ExecReload=${PREFIX}/bin/openresty -g 'daemon on; master_process on;' -s reload
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target
EOF
