#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE = 1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# Build system configuration
export CARGO_HOME = $(CURDIR)/debian/cargo
export CARGO_TARGET_DIR = $(CURDIR)/debian/cargo/target

# Detect architecture for cross-compilation
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

# Rust target for cross-compilation (simplified)
RUST_TARGET := $(DEB_HOST_ARCH)

# Build flags - Use safer flags to avoid Rust compiler SIGSEGV
CARGO_FLAGS := --release --config .cargo/config.toml

# Environment variables for safer builds
export RUSTFLAGS = -C opt-level=2 -C codegen-units=16 -C lto=false
export CARGO_INCREMENTAL = 1

override_dh_auto_build:
	@echo "Building apt-ostree for $(DEB_HOST_ARCH)..."
	# Ensure cargo is available
	@which cargo > /dev/null || (echo "Error: cargo not found. Please install Rust toolchain." && exit 1)
	# Ensure both binaries are built
	@echo "Building apt-ostree CLI..."
	cargo build $(CARGO_FLAGS) --bin apt-ostree
	@echo "Building apt-ostreed daemon..."
	cargo build $(CARGO_FLAGS) --bin apt-ostreed
	dh_auto_build

override_dh_auto_install:
	@echo "Installing apt-ostree binary..."
	# Check if binary exists
	@if [ ! -f "debian/cargo/target/release/apt-ostree" ]; then \
		echo "Error: apt-ostree binary not found. Build failed."; \
		exit 1; \
	fi
	# Install the apt-ostree binary to the correct location
	install -D -m 755 debian/cargo/target/release/apt-ostree \
		debian/apt-ostree/usr/bin/apt-ostree
	# Create additional directories
	mkdir -p debian/apt-ostree/usr/share/doc/apt-ostree
	mkdir -p debian/apt-ostree/usr/share/man/man1
	mkdir -p debian/apt-ostree/usr/share/bash-completion/completions
	mkdir -p debian/apt-ostree/usr/share/zsh/vendor-completions
	mkdir -p debian/apt-ostree/usr/share/apt-ostree
	@echo "apt-ostree package directories created successfully"
	# Install man pages if they exist
	@if [ -f "debian/apt-ostree.1" ]; then \
		install -D -m 644 debian/apt-ostree.1 debian/apt-ostree/usr/share/man/man1/apt-ostree.1; \
	fi
	@if [ -f "debian/man/apt-ostree.1" ]; then \
		install -D -m 644 debian/man/apt-ostree.1 debian/apt-ostree/usr/share/man/man1/apt-ostree.1; \
	fi
	@if [ -f "debian/man/apt-ostree-dev.1" ]; then \
		install -D -m 644 debian/man/apt-ostree-dev.1 debian/apt-ostree/usr/share/man/man1/apt-ostree-dev.1; \
	fi
	@if [ -f "debian/man/apt-ostree.conf.5" ]; then \
		install -D -m 644 debian/man/apt-ostree.conf.5 debian/apt-ostree/usr/share/man/man5/apt-ostree.conf.5; \
	fi
	# Install bash completion if it exists
	@if [ -f "debian/apt-ostree.bash-completion" ]; then \
		install -D -m 644 debian/apt-ostree.bash-completion \
			debian/apt-ostree/usr/share/bash-completion/completions/apt-ostree; \
	fi
	# Install zsh completion if it exists
	@if [ -f "debian/apt-ostree.zsh-completion" ]; then \
		install -D -m 644 debian/apt-ostree.zsh-completion \
			debian/apt-ostree/usr/share/zsh/vendor-completions/_apt-ostree; \
	fi

	@echo "Installing apt-ostreed daemon into apt-ostree package..."
	# Check if binary exists
	@if [ ! -f "debian/cargo/target/release/apt-ostreed" ]; then \
		echo "Error: apt-ostreed binary not found. Build failed."; \
		exit 1; \
	fi
	# Install the apt-ostreed binary into the apt-ostree package
	install -D -m 755 debian/cargo/target/release/apt-ostreed \
		debian/apt-ostree/usr/libexec/apt-ostreed
	# Check and install systemd service files
	@if [ ! -f "daemon/systemd/apt-ostreed.service" ]; then \
		echo "Error: apt-ostreed.service not found."; \
		exit 1; \
	fi
	@if [ ! -f "daemon/systemd/apt-ostreed.socket" ]; then \
		echo "Error: apt-ostreed.socket not found."; \
		exit 1; \
	fi
	install -D -m 644 daemon/systemd/apt-ostreed.service \
		debian/apt-ostree/lib/systemd/system/apt-ostreed.service
	install -D -m 644 daemon/systemd/apt-ostreed.socket \
		debian/apt-ostree/lib/systemd/system/apt-ostreed.socket
	
	# Check and install polkit policy
	@if [ ! -f "daemon/polkit/apt-ostree.policy" ]; then \
		echo "Error: apt-ostree.policy not found."; \
		exit 1; \
	fi
	install -D -m 644 daemon/polkit/apt-ostree.policy \
		debian/apt-ostree/usr/share/polkit-1/actions/org.projectatomic.aptostree1.policy
	# Check and install configuration files
	@if [ ! -f "src/daemon/apt-ostreed.conf" ]; then \
		echo "Error: apt-ostreed.conf not found."; \
		exit 1; \
	fi
	install -d -m 755 debian/apt-ostree/etc/apt-ostreed
	install -D -m 644 src/daemon/apt-ostreed.conf \
		debian/apt-ostree/etc/apt-ostreed/apt-ostreed.conf
	# Create additional directories
	mkdir -p debian/apt-ostree/usr/share/doc/apt-ostree
	# Create log directory
	mkdir -p debian/apt-ostree/var/log
	# Create cache directory
	mkdir -p debian/apt-ostree/var/cache/apt-ostree
	# Create state directory
	mkdir -p debian/apt-ostree/var/lib/apt-ostree
	@echo "apt-ostreed components installed into apt-ostree package successfully"

	# Skip dh_auto_install since we've handled installation manually
	@echo "Package installation completed successfully!"

override_dh_auto_clean:
	@echo "Cleaning cargo build artifacts..."
	# Clean cargo build artifacts
	rm -rf debian/cargo
	dh_auto_clean

override_dh_strip:
	# Strip debug symbols from binary
	dh_strip

override_dh_shlibdeps:
	# Handle shared library dependencies
	dh_shlibdeps

override_dh_fixperms:
	# Fix file permissions
	dh_fixperms

%:
	dh $@ 