#!/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; each command runs 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")

export BUILDDIR := obj-$(DEB_HOST_GNU_TYPE)
export LIBDIR := $(DEB_HOST_MULTIARCH)

%:
	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_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 directory=$(BUILDDIR)"
	@echo "Library directory=lib/$(LIBDIR)"
	@echo "Current directory=$(CURDIR)"
	@echo "----- End build-machine information -----"
	dh_auto_configure --builddirectory=$(BUILDDIR) -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib/$(LIBDIR) \
		-DBUILD_SHARED_LIBS=ON

override_dh_auto_test:
	@echo "Skipping tests"

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