#!/bin/sh
set -ex

# adapted from https://github.com/openresty/docker-openresty

# Docker Build Arguments
RESTY_VERSION=${RESTY_VERSION:-"1.15.8.1"}
RESTY_PREFIX=${HOME}/local/openresty
RESTY_CONFIG_OPTIONS="\
    --with-pcre-jit \
    --without-http_rds_json_module \
    --without-http_rds_csv_module \
    --without-lua_rds_parser \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-http_v2_module \
    --without-mail_pop3_module \
    --without-mail_imap_module \
    --without-mail_smtp_module \
    --with-http_stub_status_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_secure_link_module \
    --with-http_random_index_module \
    --with-http_gzip_static_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-threads \
    --with-dtrace-probes \
    --prefix=${RESTY_PREFIX} \
    "

# 1) Install dependencies

# use the system OpenSSL and PCRE for the moment

sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

# get zlib, openssl and pcre from the openresty repository

sudo yum -y install \
    GeoIP-devel \
    gd-devel \
    gettext \
    ccache \
    libxslt-devel \
    lcov \
    perl-ExtUtils-Embed \
    perl-Test-Nginx \
    readline-devel \
    voms-devel 

# 2) Download, unpack, configure, build and install OpenResty

# configuring, building and installing is not strictly necessary, since
# module development requires to rebuild nginx itself, but it's a good
# check to see if everything is ok
# configuring has the benefit that it builds and installs luajit, which
# can then be reused during development (see the additions to .bashrc)

RESTY_PACKAGES_PREFIX=/usr/local/openresty
ZLIB_PREFIX=/usr
OPENSSL_PREFIX=/usr
PCRE_PREFIX=/usr
#ZLIB_PREFIX=${RESTY_PACKAGES_PREFIX}/zlib
#OPENSSL_PREFIX=${RESTY_PACKAGES_PREFIX}/openssl
#PCRE_PREFIX=${RESTY_PACKAGES_PREFIX}/pcre

wget https://openresty.org/download/openresty-${RESTY_VERSION}.tar.gz
tar zxf openresty-${RESTY_VERSION}.tar.gz
cd openresty-${RESTY_VERSION}
./configure \
	--with-cc="ccache gcc -fdiagnostics-color=always" \
	--with-cc-opt="-DNGX_LUA_ABORT_AT_PANIC -I${ZLIB_PREFIX}/include -I${PCRE_PREFIX}/include -I${OPENSSL_PREFIX}/include" \
        --with-ld-opt="-L${ZLIB_PREFIX}/lib -L${PCRE_PREFIX}/lib -L${OPENSSL_PREFIX}/lib -Wl,-rpath,${ZLIB_PREFIX}/lib:${PCRE_PREFIX}/lib:${OPENSSL_PREFIX}/lib" \
	--with-luajit-xcflags="-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT" \
	${RESTY_CONFIG_OPTIONS}
make
make install
cd -

# add the location of openresty executables to the PATH
# save resty configuration options for future reuse during development

cat << EOF >> ${HOME}/openresty-env
PATH="${RESTY_PREFIX}/luajit/bin:${RESTY_PREFIX}/nginx/sbin:${RESTY_PREFIX}/bin:\${PATH}"

RESTY_PACKAGES_PREFIX=/usr/local/openresty

ZLIB_PREFIX=/usr
OPENSSL_PREFIX=/usr
PCRE_PREFIX=/usr

export RESTY_CONFIG_OPTIONS="${RESTY_CONFIG_OPTIONS} --with-luajit=${RESTY_PREFIX}/luajit"
EOF

cat << EOF >> ${HOME}/.bashrc
if [ -f "${HOME}/openresty-env" ]; then
    . ${HOME}/openresty-env
fi

if [ -n "\${PS1}" ]; then
    echo
    echo "To build and install openresty with the ngx_http_voms_module run './build-install-ngx-voms.sh'"
    echo
fi
EOF