#!/usr/bin/make -f
# -*- makefile -*-

SHELL := /bin/sh

export DH_VERBOSE := 1
export DEB_BUILD_OPTIONS := nocheck

# OBS paths
OBS_TOPDIR := /usr/src/packages
OBS_BUILDDIR := $(OBS_TOPDIR)/BUILD
OBS_SOURCEDIR := $(OBS_TOPDIR)/SOURCES

export _topdir := $(OBS_TOPDIR)
export _builddir := $(OBS_BUILDDIR)
export _sourcedir := $(OBS_SOURCEDIR)
export uploaddir := $(OBS_SOURCEDIR)
export srcdir := $(OBS_SOURCEDIR)

export pkgdir_head := $(OBS_BUILDDIR)/debian
export pkgdir := $(pkgdir_head)/tmp
export buildroot := $(pkgdir)

# Build-machine information. These are evaluated once as Make reads this file.
export ARCHITECTURE := $(shell uname -m)
export DEBIAN_ARCHITECTURE := $(shell dpkg --print-architecture)
export DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
export DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export numberOfProcessorsOnMachine := $(shell nproc)
export jobsIget := $(shell awk -v n="$(numberOfProcessorsOnMachine)" 'BEGIN { j=int(1.25*n+0.5); if (j<1) j=1; print j }')

export TOTAL_DISK := $(shell df -h --total | awk 'END {print $$2}')
export FREEMEM := $(shell free --mega)
export TOTAL_MEM := $(shell free --total --mega | awk '/^Mem:/ {print $$2 "MB"}')
export TOTAL_RAM := $(shell free --mega | awk '/^Mem:/ {print $$2 "MB"}')
export TOTAL_SWAP := $(shell free --mega | awk '/^Swap:/ {print $$2 "MB"}')

export PRETTY_NAME := $(shell . /etc/os-release; printf '%s' "$$PRETTY_NAME")
export VERSION_CODENAME := $(shell . /etc/os-release; printf '%s' "$$VERSION_CODENAME")
export VERSION_ID := $(shell . /etc/os-release; printf '%s' "$$VERSION_ID")

# Debian-native build and library paths
export BUILDDIR := obj-$(DEB_HOST_GNU_TYPE)
export LIBDIR := $(DEB_HOST_MULTIARCH)
export SOURCES_ARE_HERE := $(OBS_BUILDDIR)
export BUILD_IT_HERE := $(OBS_BUILDDIR)/$(BUILDDIR)

%:
	dh $@ --buildsystem=cmake --builddirectory=$(BUILDDIR) --parallel --max-parallel=$(jobsIget)

override_dh_update_autotools_config:
	@echo "Skipping dh_update_autotools_config (CMake build)"

override_dh_autoreconf:
	@echo "Skipping dh_autoreconf (CMake build)"

override_dh_auto_clean:
	@echo "Removing CMake build directory: $(BUILD_IT_HERE)"
	rm -rf "$(BUILD_IT_HERE)"

override_dh_auto_configure:
	@echo "----- Build-machine information -----"
	@echo "PRETTY_NAME=$(PRETTY_NAME)"
	@echo "VERSION_ID=$(VERSION_ID)"
	@echo "VERSION_CODENAME=$(VERSION_CODENAME)"
	@echo "uname architecture=$(ARCHITECTURE)"
	@echo "Debian architecture=$(DEBIAN_ARCHITECTURE)"
	@echo "DEB_BUILD_ARCH=$(DEB_BUILD_ARCH)"
	@echo "DEB_HOST_GNU_TYPE=$(DEB_HOST_GNU_TYPE)"
	@echo "DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH)"
	@echo "Processors=$(numberOfProcessorsOnMachine)"
	@echo "Build jobs=$(jobsIget)"
	@echo "Total disk=$(TOTAL_DISK)"
	@echo "Memory report:"
	@printf '%s\n' "$(FREEMEM)"
	@echo "Total memory=$(TOTAL_MEM)"
	@echo "Total RAM=$(TOTAL_RAM)"
	@echo "Total SWAP=$(TOTAL_SWAP)"
	@echo "----- Build paths -----"
	@echo "_topdir=$(_topdir)"
	@echo "_builddir=$(_builddir)"
	@echo "_sourcedir=$(_sourcedir)"
	@echo "uploaddir=$(uploaddir)"
	@echo "srcdir=$(srcdir)"
	@echo "pkgdir_head=$(pkgdir_head)"
	@echo "pkgdir=$(pkgdir)"
	@echo "buildroot=$(buildroot)"
	@echo "BUILDDIR=$(BUILDDIR)"
	@echo "LIBDIR=$(LIBDIR)"
	@echo "SOURCES_ARE_HERE=$(SOURCES_ARE_HERE)"
	@echo "BUILD_IT_HERE=$(BUILD_IT_HERE)"
	@echo "Current directory=$(CURDIR)"
	@echo "----- End build information -----"

	mkdir -p "$(BUILD_IT_HERE)"
	cmake -S "$(SOURCES_ARE_HERE)" -B "$(BUILD_IT_HERE)" \
		-DCMAKE_INSTALL_LIBDIR="lib/$(LIBDIR)" \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DBUILD_SHARED_LIBS=ON \
		-DENABLE_STRICT=OFF \
		-DENABLE_STATIC=OFF \
		-DCMAKE_POLICY_VERSION_MINIMUM=3.5

override_dh_auto_test:
	@echo "Skipping tests"

execute_after_dh_auto_install:
	@echo "--- Start execute_after_dh_auto_install ---"
	@echo "buildroot=$(buildroot)"
	@echo "--- Files installed to buildroot ---"
	@if test -d "$(buildroot)"; then \
		find "$(buildroot)" -mindepth 1 -printf '%P\n' | sort; \
	else \
		echo "Buildroot directory not found: $(buildroot)"; \
	fi
	@echo "--- End files installed to buildroot ---"
	@echo "--- End execute_after_dh_auto_install ---"