#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# Get version and tag from environment or changelog
DEB_VERSION := $(shell dpkg-parsechangelog -S Version | cut -d- -f1)
SOURCE_TAG := v$(DEB_VERSION)

# Architecture map for dpkg 
# "Arch in dpkg" -> "Arch in openlist"
# See: https://wiki.debian.org/SupportedArchitectures and https://www.debian.org/ports/
# Note: armel will be removed in Debian 14
amd64_mapped   := amd64
i386_mapped    := 386
arm64_mapped   := arm64
armel_mapped   := arm-5
armhf_mapped   := arm-7
ppc64el_mapped := ppc64le
riscv64_mapped := riscv64
s390x_mapped   := s390x
loong64_mapped := loong64

# Get arch value, return original value if not found
get_mapped_arch = $(if $(value $(1)_mapped),$(value $(1)_mapped),$(1))

OPENLIST_ARCH := $(call get_mapped_arch,$(DEB_HOST_ARCH))

%:
	dh $@

override_dh_auto_build:
	@echo "=== Debian Rules: override_dh_auto_build ==="
	@echo "Package version: $(DEB_VERSION)"
	@echo "Source tag: $(SOURCE_TAG)"
	@echo "Dpkg Architecture: $(DEB_HOST_ARCH)"
	@echo "OpenList Architecture: $(OPENLIST_ARCH)"
	@echo "Build phase - preparing for installation"

override_dh_auto_install:
	@echo "=== Debian Rules: override_dh_auto_install ==="
	@echo "Looking for binary for architecture: $(DEB_HOST_ARCH)"
	# First check if binary is pre-downloaded in debian/binaries/ (for PPA builds)
	if [ -f "debian/binaries/openlist-linux-$(OPENLIST_ARCH).tar.gz" ]; then \
		echo "Found pre-downloaded binary in debian/binaries/"; \
		cp "debian/binaries/openlist-linux-$(OPENLIST_ARCH).tar.gz" "openlist-linux-$(OPENLIST_ARCH).tar.gz"; \
	elif [ ! -f "openlist-linux-$(OPENLIST_ARCH).tar.gz" ]; then \
		echo "Binary archive not found, downloading..."; \
		echo "Download URL: https://github.com/OpenListTeam/OpenList/releases/download/$(SOURCE_TAG)/openlist-linux-$(OPENLIST_ARCH).tar.gz"; \
		wget -O "openlist-linux-$(OPENLIST_ARCH).tar.gz" \
			"https://github.com/OpenListTeam/OpenList/releases/download/$(SOURCE_TAG)/openlist-linux-$(OPENLIST_ARCH).tar.gz" || \
		(echo "Failed to download binary archive"; exit 1); \
	fi
	mkdir -p temp_extract
	if [ -f "openlist-linux-$(OPENLIST_ARCH).tar.gz" ]; then \
		echo "Found binary archive: openlist-linux-$(OPENLIST_ARCH).tar.gz"; \
		echo "Archive size: $$(ls -lh openlist-linux-$(OPENLIST_ARCH).tar.gz | awk '{print $$5}')"; \
		echo "Extracting to temporary directory"; \
		tar -xzf "openlist-linux-$(OPENLIST_ARCH).tar.gz" -C temp_extract; \
		echo "Extraction completed"; \
	else \
		echo "Error: Binary archive openlist-linux-$(OPENLIST_ARCH).tar.gz not found"; \
		exit 1; \
	fi
	@echo "Checking extracted contents:"
	@ls -la temp_extract/
	if [ ! -f "temp_extract/openlist" ]; then \
		echo "Error: openlist binary not found after extraction"; \
		echo "Contents of extraction directory:"; \
		find temp_extract/ -type f -exec ls -la {} \; || echo "Directory is empty"; \
		echo "Checking what was in the archive:"; \
		tar -tzf "openlist-linux-$(OPENLIST_ARCH).tar.gz" || echo "Cannot list archive contents"; \
		exit 1; \
	fi
	mkdir -p debian/openlist/var/lib/openlist
	mkdir -p debian/openlist/usr/bin
	mkdir -p debian/openlist/usr/lib/systemd/system
	cp temp_extract/openlist debian/openlist/var/lib/openlist/
	chmod +x debian/openlist/var/lib/openlist/openlist
	cp debian/openlist.service debian/openlist/usr/lib/systemd/system/
	echo '#!/bin/bash' > debian/openlist/usr/bin/openlist
	echo '# OpenList wrapper script' >> debian/openlist/usr/bin/openlist
	echo '# Automatically adds --force-bin-dir to all commands' >> debian/openlist/usr/bin/openlist
	echo '' >> debian/openlist/usr/bin/openlist
	echo 'BINARY="/var/lib/openlist/openlist"' >> debian/openlist/usr/bin/openlist
	echo '' >> debian/openlist/usr/bin/openlist
	echo '# Check if the binary exists' >> debian/openlist/usr/bin/openlist
	echo 'if [ ! -x "$$BINARY" ]; then' >> debian/openlist/usr/bin/openlist
	echo '    echo "Error: OpenList binary not found at $$BINARY"' >> debian/openlist/usr/bin/openlist
	echo '    exit 1' >> debian/openlist/usr/bin/openlist
	echo 'fi' >> debian/openlist/usr/bin/openlist
	echo '' >> debian/openlist/usr/bin/openlist
	echo '# Check if --force-bin-dir is already present in arguments' >> debian/openlist/usr/bin/openlist
	echo 'if [[ "$$*" != *"--force-bin-dir"* ]]; then' >> debian/openlist/usr/bin/openlist
	echo '    # Add --force-bin-dir to all commands' >> debian/openlist/usr/bin/openlist
	echo '    exec "$$BINARY" "$$@" --force-bin-dir' >> debian/openlist/usr/bin/openlist
	echo 'else' >> debian/openlist/usr/bin/openlist
	echo '    # --force-bin-dir already present, pass through as-is' >> debian/openlist/usr/bin/openlist
	echo '    exec "$$BINARY" "$$@"' >> debian/openlist/usr/bin/openlist
	echo 'fi' >> debian/openlist/usr/bin/openlist
	chmod +x debian/openlist/usr/bin/openlist
	rm -rf temp_extract
	@echo "Files installed to package directory:"
	@find debian/openlist -type f -exec ls -la {} \;
	@echo "Wrapper script content:"
	@cat debian/openlist/usr/bin/openlist

override_dh_install:
	@echo "Skipping dh_install - files already installed manually"

override_dh_usrlocal:
	@echo "Skipping dh_usrlocal - not using /usr/local"

override_dh_strip:
	@echo "Skipping dh_strip - Go binaries are already optimized and don't need stripping"

override_dh_dwz:
	@echo "Skipping dh_dwz - Go binaries don't need DWARF compression"

override_dh_makeshlibs:
	@echo "Skipping dh_makeshlibs - no shared libraries to process"

override_dh_shlibdeps:
	@echo "Skipping dh_shlibdeps - Go binaries are statically linked"

override_dh_auto_clean:
	@echo "=== Debian Rules: override_dh_auto_clean ==="
	rm -f openlist-linux-*.tar.gz
	rm -rf temp_extract
	# Preserve debian/binaries directory during build process
	# It contains pre-downloaded binaries for PPA builds
	@if [ -d "debian/binaries" ]; then \
		echo "Preserving debian/binaries (contains pre-downloaded binaries for PPA)"; \
		echo "Contents of debian/binaries:"; \
		ls -la debian/binaries/ || true; \
	else \
		echo "No debian/binaries directory found"; \
	fi
	dh_auto_clean

override_dh_auto_test:
	@echo "Skipping tests for cross-compilation"
