#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CARGO_PACKAGE = nginx-x402
export DEB_CARGO_BUILD_PROFILE = release

%:
	dh $@

override_dh_auto_build:
	# Detect system nginx version and use it for building if available
	# This ensures the module is built against the same nginx version as the system
	NGINX_VERSION=; \
	NGINX_SOURCE_DIR=; \
	USE_VENDORED=1; \
	# Try to detect nginx version from installed package
	if command -v nginx >/dev/null 2>&1; then \
		NGINX_VERSION=$$(nginx -v 2>&1 | sed -n 's/.*nginx\/\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' || echo ""); \
	elif dpkg -l nginx 2>/dev/null | grep -q "^ii"; then \
		NGINX_VERSION=$$(dpkg -l nginx | grep "^ii" | awk '{print $$3}' | cut -d'-' -f1 | cut -d':' -f2 || echo ""); \
	fi; \
	# If nginx version detected, try to use system nginx source
	if [ -n "$$NGINX_VERSION" ]; then \
		echo "Detected system nginx version: $$NGINX_VERSION"; \
		if [ -d /usr/src/nginx-$$NGINX_VERSION ]; then \
			NGINX_SOURCE_DIR=/usr/src/nginx-$$NGINX_VERSION; \
			USE_VENDORED=0; \
			echo "Using nginx source from /usr/src/nginx-$$NGINX_VERSION"; \
		elif [ -d /usr/share/nginx-$$NGINX_VERSION ]; then \
			NGINX_SOURCE_DIR=/usr/share/nginx-$$NGINX_VERSION; \
			USE_VENDORED=0; \
			echo "Using nginx source from /usr/share/nginx-$$NGINX_VERSION"; \
		else \
			echo "System nginx source not found, using vendored feature"; \
			USE_VENDORED=1; \
		fi; \
	fi; \
	# Set environment variables based on detection result
	if [ "$$USE_VENDORED" = "0" ] && [ -n "$$NGINX_SOURCE_DIR" ]; then \
		export NGINX_SOURCE_DIR=$$NGINX_SOURCE_DIR; \
		export CARGO_FEATURES="--no-default-features"; \
		echo "Building with system nginx source: $$NGINX_SOURCE_DIR"; \
	else \
		export CARGO_FEATURES=""; \
		echo "Building with vendored nginx (default)"; \
	fi; \
	# Set required environment variables for vendored feature
	# Note: NGX_CONFIGURE_ARGS may be set by parent process (e.g., GitHub Actions)
	# Set libclang path if available (Linux) and not already set
	if [ -z "$$LIBCLANG_PATH" ] && [ -d /usr/lib/llvm-*/lib ]; then \
		export LIBCLANG_PATH=$$(ls -d /usr/lib/llvm-*/lib | head -1); \
	fi; \
	# Set NGX_CONFIGURE_ARGS if not already set
	if [ -z "$$NGX_CONFIGURE_ARGS" ]; then \
		export NGX_CONFIGURE_ARGS="--without-http_rewrite_module"; \
	fi; \
	# Determine Rust target based on DEB_HOST_ARCH or DEB_BUILD_ARCH
	if [ -n "$$DEB_HOST_ARCH" ]; then \
		case "$$DEB_HOST_ARCH" in \
			amd64) RUST_TARGET=x86_64-unknown-linux-gnu ;; \
			arm64) RUST_TARGET=aarch64-unknown-linux-gnu ;; \
			armhf) RUST_TARGET=armv7-unknown-linux-gnueabihf ;; \
			*) RUST_TARGET=$$(dpkg-architecture -qDEB_HOST_GNU_TYPE) ;; \
		esac; \
		if [ "$$RUST_TARGET" != "x86_64-unknown-linux-gnu" ]; then \
			rustup target add $$RUST_TARGET || true; \
			cargo build --release --target $$RUST_TARGET $$CARGO_FEATURES; \
		else \
			cargo build --release $$CARGO_FEATURES; \
		fi; \
	else \
		cargo build --release $$CARGO_FEATURES; \
	fi

override_dh_auto_install:
	# Install files manually
	mkdir -p debian/nginx-x402/usr/lib/nginx/modules
	mkdir -p debian/nginx-x402/usr/share/doc/nginx-x402
	mkdir -p debian/nginx-x402/etc/nginx/modules-available
	# Copy the built module (find the correct architecture-specific path)
	# Try architecture-specific target directory first, then fallback to release
	if [ -n "$$DEB_HOST_ARCH" ]; then \
		case "$$DEB_HOST_ARCH" in \
			amd64) RUST_TARGET=x86_64-unknown-linux-gnu ;; \
			arm64) RUST_TARGET=aarch64-unknown-linux-gnu ;; \
			armhf) RUST_TARGET=armv7-unknown-linux-gnueabihf ;; \
			*) RUST_TARGET=$$(dpkg-architecture -qDEB_HOST_GNU_TYPE) ;; \
		esac; \
		if [ -f "target/$$RUST_TARGET/release/libnginx_x402.so" ]; then \
			cp target/$$RUST_TARGET/release/libnginx_x402.so debian/nginx-x402/usr/lib/nginx/modules/libnginx_x402.so; \
		else \
			find target -name "libnginx_x402.so" -type f | head -1 | xargs -I {} cp {} debian/nginx-x402/usr/lib/nginx/modules/libnginx_x402.so || \
			cp target/release/libnginx_x402.so debian/nginx-x402/usr/lib/nginx/modules/ || true; \
		fi \
	else \
		find target -name "libnginx_x402.so" -type f | head -1 | xargs -I {} cp {} debian/nginx-x402/usr/lib/nginx/modules/libnginx_x402.so || \
		find target -name "libnginx_x402.dylib" -type f | head -1 | xargs -I {} cp {} debian/nginx-x402/usr/lib/nginx/modules/libnginx_x402.so || \
	cp target/release/libnginx_x402.so debian/nginx-x402/usr/lib/nginx/modules/ || \
		cp target/*/release/libnginx_x402.so debian/nginx-x402/usr/lib/nginx/modules/ || true; \
	fi
	# Copy documentation
	cp README.md debian/nginx-x402/usr/share/doc/nginx-x402/
	cp LICENSE debian/nginx-x402/usr/share/doc/nginx-x402/
	cp nginx/example.conf debian/nginx-x402/usr/share/doc/nginx-x402/example.conf
	# Create module configuration snippet
	echo "load_module /usr/lib/nginx/modules/libnginx_x402.so;" > debian/nginx-x402/etc/nginx/modules-available/x402.conf

override_dh_auto_test:
	# Run tests (vendored feature is enabled by default)
	cargo test || true
