#!/bin/bash

OPENRESTY_VERSION='1.9.7.1'
cd $OPENSHIFT_TMP_DIR
wget --no-check-certificate https://openresty.org/download/ngx_openresty-${OPENRESTY_VERSION}.tar.gz
tar xzf ngx_openresty-${OPENRESTY_VERSION}.tar.gz
wget http://exim.mirror.fr/pcre/pcre-8.38.tar.gz
tar xzf pcre-8.38.tar.gz
wget http://luajit.org/download/LuaJIT-2.1.0-beta1.tar.gz
tar xzf LuaJIT-2.1.0-beta1.tar.gz
cd LuaJIT-2.1.0-beta1
sed -i "s/export PREFIX= \/usr\/local/export PREFIX= \${OPENSHIFT_DATA_DIR}usr\/local/g" /tmp/LuaJIT-2.1.0-beta1/Makefile
make -j && make install
cd ${OPENSHIFT_TMP_DIR}ngx_openresty-${OPENRESTY_VERSION}
#export LUAJIT_LIB=${OPENSHIFT_DATA_DIR}usr/local/lib
#export LUAJIT_INC=${OPENSHIFT_DATA_DIR}usr/local/include/luajit-2.1
sed -i "s/\$prefix\/nginx/\$prefix/g" ${OPENSHIFT_TMP_DIR}ngx_openresty-${OPENRESTY_VERSION}/configure
./configure --prefix=${OPENSHIFT_DATA_DIR} --with-pcre=${OPENSHIFT_TMP_DIR}pcre-8.38 --with-pcre-jit --with-luajit=${OPENSHIFT_DATA_DIR}usr/local --with-threads --with-http_realip_module --with-http_sub_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_geoip_module --with-http_secure_link_module --without-lua_resty_memcached --without-mail_pop3_module --without-mail_smtp_module
make -j4 && make install
sed -i "s/default_type  application\/octet-stream;/default_type  application\/octet-stream;\n    port_in_redirect off;\n    server_tokens off;/g" ${OPENSHIFT_DATA_DIR}conf/nginx.conf
sed -i "s/listen       80;/listen       ${OPENSHIFT_DIY_IP}:8080;/g" ${OPENSHIFT_DATA_DIR}conf/nginx.conf
cd /tmp
rm -rf *
cd ${OPENSHIFT_DATA_DIR}html
rm -rf index.html
echo "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><title>Openshift Openresty 环境安装成功！</title><style>    body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Openshift Openresty 环境安装成功！</h1><p>这是使用 Hi kooker Proxy 提供的脚本安装 Openresty-${OPENRESTY_VERSION}</p><p><em><a href=\"https://blog.kooker.jp/\">Hi kooker</a></em></p></body></html>" >> ${OPENSHIFT_DATA_DIR}html/index.html
cd ${OPENSHIFT_REPO_DIR}.openshift/action_hooks
rm -rf start
wget --no-check-certificate https://www.kooker.jp/nginx-start
mv nginx-start start
chmod 755 start
cd ${OPENSHIFT_REPO_DIR}.openshift/cron/minutely
rm -rf restart.sh
wget --no-check-certificate https://www.kooker.jp/nginx-restart.sh
mv nginx-restart.sh restart.sh
chmod 755 restart.sh
touch nohup.out
chmod 755 nohup.out
gear stop
killall nginx
gear start
