#!/bin/bash

# A script to compile NGINX dynamic modules against the currently installed NGINX version.

set -e #

# Update system packages and install build dependencies
apt-get update
apt-get -y upgrade
apt-get -y install build-essential cmake

# Get installed NGINX version
nginx -V
command="nginx -v"
nginxv=$( ${command} 2>&1 )
NGINXVERSION=$(echo $nginxv | grep -o '[0-9.]*$')

echo "Detected NGINX version: $NGINXVERSION"

# Check for a cached tarball of the modules to avoid re-compiling
modulecache=$NGINXVERSION-modules.tar.gz
if test -f $modulecache; then
    echo "Found cached modules for this version. Installing from cache."
    rm -rf /etc/nginx/modules
    mkdir -p /etc/nginx/modules
    tar zxvf $modulecache
    mv modules/* /etc/nginx/modules/
    rm -R modules
    echo "Testing NGINX configuration..."
    nginx -t
    echo "Cached modules installed successfully."
    exit 0
else
    echo "No cached modules found. Starting fresh build."
    rm -f *-modules.tar.gz
fi

# Download NGINX source matching the installed version
wget https://nginx.org/download/nginx-$NGINXVERSION.tar.gz
tar -xzvf nginx-$NGINXVERSION.tar.gz

# Copy module dependencies into the source directory
cp -R dependencies/* nginx-$NGINXVERSION/

cd nginx-$NGINXVERSION

echo "Compiling Brotli dependency libraries..."
cd ngx_brotli/deps/brotli
mkdir -p out
cd out
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=./installed ..
make
cd ../../../../
echo "Brotli libraries compiled."

# Configure the build environment for LuaJIT
export LUAJIT_LIB=/usr/local/lib
export LD_LIBRARY_PATH=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1

# Configure the build with all dynamic modules
./configure \
--with-ld-opt="-Wl,-rpath,/usr/local/lib" \
--with-compat \
--add-dynamic-module=naxsi/naxsi_src \
--add-dynamic-module=headers-more-nginx-module \
--add-dynamic-module=echo-nginx-module \
--add-dynamic-module=ngx_devel_kit \
--add-dynamic-module=lua-nginx-module \
--add-dynamic-module=ngx_brotli

# Compile the modules
echo "Compiling NGINX modules..."
make -j$(nproc) modules

echo "Compilation finished. Preparing to install and test..."

# Backup existing modules, if they exist
if [ -d "/etc/nginx/modules" ]; then
    echo "Backing up existing modules to /etc/nginx/modules.bak..."
    mv /etc/nginx/modules /etc/nginx/modules.bak
fi

echo "Installing new modules..."
mkdir -p /etc/nginx/modules
cp -r objs/*.so /etc/nginx/modules/

# Test the NGINX configuration
echo "Testing new modules with 'nginx -t'..."
if nginx -t; then
    echo "SUCCESS: 'nginx -t' passed."
    echo "Creating cache for future use..."
    tar -zcvf $modulecache -C /etc/nginx modules

    cd ..
    mv nginx-$NGINXVERSION/$modulecache .

    if [ -d "/etc/nginx/modules.bak" ]; then
        echo "Removing backup of old modules..."
        rm -rf /etc/nginx/modules.bak
    fi

    echo "Build and installation complete!"
else
    echo "------------------------------------------------------------"
    echo "ERROR: 'nginx -t' FAILED. The new modules are broken!"
    echo "Reverting to previous module configuration."
    echo "------------------------------------------------------------"

    rm -rf /etc/nginx/modules
    if [ -d "/etc/nginx/modules.bak" ]; then
        mv /etc/nginx/modules.bak /etc/nginx/modules
        echo "Previous modules have been restored."
    fi

    echo "Cache was NOT created. Please check the build logs for errors."
    cd ..
    rm -rf nginx-$NGINXVERSION*
    exit 1
fi

echo "Cleaning up build directory..."
cd .. # Should already be here from the success path, but being safe
rm -rf nginx-*.tar.gz
rm -rf nginx-$NGINXVERSION

exit 0
