#!/usr/bin/env bash
# 文件名: nginx_tarball_install.sh
# 描述: 安装最新版本 nginx 

### 定义变量 开始
BUILD_DIR=/data/build                  # 二制包下载目录
VAR_DIR=/data/nginx_temp　　　　　　　 # nginx 临时目录 
OWNER=www                              # 运行nginx 专属用户
uid=2000                               # OWNER 用户 uid
gid=2000                               # OWNER 用户 gid
PREFIX={1:-/usr/local}                 # 安装目录,若未传入参数则缺省安装到 /usr/local 目录

pcre_ver=`curl -sL ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ | grep -o -P "pcre-\d+\.+\d+" | cut -c6- | sort -u -t. -k1,1n -k2,2n | tail -n 1`
pcre_url="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-${pcre_ver}.tar.bz2"

zlib_ver=1.2.10
zlib_url="http://zlib.net/zlib-${zlib_ver}.tar.gz"

nginx_ver=`curl -sL http://mirrors.sohu.com/nginx/ | grep -o -P "nginx-\d.\d+\.+\d" | cut -c7- | sort -u -t. -k1,1n -k2,2n -k3,3n | tail -n 1`
nginx_url="http://mirrors.sohu.com/nginx/nginx-${nginx_ver}.tar.gz"

openssl_ver=`curl -sKL https://www.openssl.org/source/ | grep -o -P "openssl-1.0.2+[a-z]" | cut -c9- | sort -u | tail -n 1`
openssl_url="https://www.openssl.org/source/openssl-${openssl_ver}.tar.gz"
### 定义变量 结束

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

fi

# 创建 $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

# 创建临时目录
[ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR}
for dir in client_temp proxy_temp fastcgi_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 ]] || curl -sSfL ${zlib_url} -O
[[ -f ${BUILD_DIR}/pcre-${pcre_ver}.tar.bz2 ]] || curl -sSfL ${pcre_url} -O
[[ -f ${BUILD_DIR}/openssl-${openssl_ver}.tar.gz ]] || curl -sSL ${openssl_url} -O
[[ -f ${BUILD_DIR}/nginx-${nginx_ver}.tar.gz ]] || curl -sSL ${nginx_url} -O

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

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

cd ${BUILD_DIR}/nginx-${nginx_ver}
./configure \
	--build=vqiu.cn \
	--prefix=${PREFIX}/nginx-${nginx_ver} \
	--user=${OWNER} \
	--group=${OWNER} \
	--http-client-body-temp-path=${VAR_DIR}/client_temp \
	--http-proxy-temp-path=${VAR_DIR}/proxy_temp \
	--http-fastcgi-temp-path=${VAR_DIR}/fastcgi_temp \
	--without-http_uwsgi_module \
	--without-http_scgi_module \
	--without-http_browser_module \
	--with-zlib=${BUILD_DIR}/zlib-${zlib_ver} \
	--with-pcre=${BUILD_DIR}/pcre-${pcre_ver} \
	--with-openssl=${BUILD_DIR}/openssl-${openssl_ver} \
	--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-file-aio \
	--with-threads \
	--with-pcre-jit && \
	make -j${nproc} && \
	make install &&
	if [ -L ${PREFIX}/nginx ]
	then
		rm -f ${PREFIX}/nginx
	else
		ln -sv ${PREFIX}/nginx-${nginx_ver} ${PREFIX}/nginx
	fi
cat >/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /var/run/nginx/pid
ExecStartPre=${PREFIX}/nginx/nginx -t
ExecStart=${PREFIX}/nginx/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl enable nginx.service
