set -e -x

echo "Extracting pcre..."
tar xzvf nginx/pcre-8.45.tar.gz

echo "Extracting headers-more module..."
tar xzvf nginx/headers-more-nginx-module-0.30.tar.gz

echo "Extracting nginx_upload module..."
tar xzvf nginx/nginx-upload-module-2.2.tar.gz

echo "Patching nginx upload module"
pushd nginx-upload-module-2.2
  patch -p1 < ../patches/nginx-upload-module.patch
popd

echo "Extracting nginx..."
tar xzvf nginx/nginx-1.21.6.tar.gz

echo "Building nginx..."

pushd nginx-1.21.6
  ./configure \
    --with-debug \
    --prefix=${BOSH_INSTALL_TARGET} \
    --with-pcre=../pcre-8.45 \
    --add-module=../headers-more-nginx-module-0.30 \
    --with-http_dav_module \
    --with-http_gunzip_module \
    --with-http_realip_module \
    --with-http_ssl_module \
    --with-http_sub_module \
    --with-http_v2_module \
    --with-stream \
    --with-threads \
    --with-stream_ssl_module \
    --with-http_slice_module \
    --with-http_stub_status_module \
    --add-module=../nginx-upload-module-2.2

  make
  make install
popd
