#!/bin/bash

NGINX_VERSION=1.12.0
HEADERS_MORE_VERSION=0.31
OPENSSL=1.1.0f
AJP_VERSION=bf6cd93f2098b59260de8d494f0f4b1f11a84627

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
pushd $DIR

function download_archive {
  local dirname=$1
  local url=$2
  local archive="$dirname.tar.gz"

  if [ ! -d "$dirname" ]; then
    wget -O "$dirname.tar.gz" "$url"
    #sha1sum "$archive"
    tar -xzvf "$archive"
    mv `tar -ztf "$archive" | head -n 1` "$dirname"
    rm "$archive"
  fi
}

download_archive nginx_src http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz

#Headers More module
download_archive nginx-headers-more-module https://github.com/openresty/headers-more-nginx-module/archive/v$HEADERS_MORE_VERSION.tar.gz
download_archive nginx-ajp-module https://github.com/yaoweibin/nginx_ajp_module/archive/$AJP_VERSION.tar.gz
download_archive nginx-shib-module  https://github.com/nginx-shib/nginx-http-shibboleth/archive/v2.0.0.tar.gz
download_archive openssl-$OPENSSL http://www.openssl.org/source/openssl-$OPENSSL.tar.gz
ln -s openssl-$OPENSSL openssl

cd nginx_src
./configure --prefix=/opt/nginx \
   --add-module="$DIR/nginx-headers-more-module" \
   --add-module="$DIR/nginx-ajp-module" \
   --add-module="$DIR/nginx-shib-module" \
   --with-http_ssl_module \
   --with-http_stub_status_module \
   --with-openssl="$DIR/openssl" \
   --with-debug \
   --with-threads
make -j2
make install

popd
