#!/usr/bin/make -f
# -*- makefile -*-
# Last Update March 15, 2025
# Copyright (C) 2017 - 2025 Trevor SANDY

# output every command that modifies files on the build system.
export DH_VERBOSE=1

# use Qt5
export QT_SELECT=qt5

# for 3rd party apps install
export LP3D_BUILD_PKG=yes

# get CPU arch - DEB_BUILD_ARCH returns amd64 for x86_64, arm for armv7l or arm64 for aarch64
export TARGET_CPU=${DEB_BUILD_ARCH}

# get OS name
export TARGET_VENDOR := $(shell . /etc/os-release 2>/dev/null; [ -n "$${ID}" ] && echo "$${ID}" || echo 'undefined')

# get OS version
export PLATFORM_VER := $(shell . /etc/os-release 2>/dev/null; [ -n "$${VERSION_ID}" ] && echo "$${VERSION_ID}" || echo 'undefined')

# check if is Univention 5.0 using shameless hack because the proud creator of this OBS project added no way to correctly ID the OS
export LP3D_UCS_5 := $(shell [ $$(which ucslint) ] && echo "5.0" || :)

# check if is Debian
export LP3D_DEBIAN_10 := $(shell [ $$(echo ${TARGET_VENDOR} | awk '{print tolower($$0)}') = "debian" ] && [ ${PLATFORM_VER} = "10" ] && echo "10" || :)

# get app version suffix
LP3D_VER_SUFFIX := $(shell cat builds/utilities/version.info | cut -d " " -f 1-2 | sed s/" "//g)

# get release directory name
LP3D_RELEASE_DIR := $(shell [ ${TARGET_CPU} = amd64 ] || [ ${TARGET_CPU} = x86_64 ] || [ ${TARGET_CPU} = aarch64 ] || [ ${TARGET_CPU} = arm64 ] && echo "64bit_release" || echo "32bit_release")

# get lpub3d executable path
LP3D_VALID_EXE := $(shell echo "mainApp/${LP3D_RELEASE_DIR}/lpub3d${LP3D_VER_SUFFIX}")

# set compatibility level - KO, building renderers twice and failing after
# export DH_COMPAT := $(shell [ ${PLATFORM_VER} = 10 ] && echo 10 || echo 9)

%:
	dh $@

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Start custom configuration commands
	echo "Current working directory: `pwd`"
	if [ -f ../SOURCES/complete.zip ]; then \
		cp ../SOURCES/complete.zip mainApp/extras && echo "LDraw archive library complete.zip copied to $$(readlink -e mainApp/extras)"; \
		cp ../SOURCES/complete.zip ../ &&	echo "LDraw archive library complete.zip copied to $$(readlink -e ../)"; \
	else \
		echo "LDraw archive library complete.zip not found at $$(readlink -e ../SOURCES)!"; \
	fi
	if [ -f ../SOURCES/lpub3dldrawunf.zip ]; then \
		cp ../SOURCES/lpub3dldrawunf.zip mainApp/extras && echo "LDraw archive library lpub3dldrawunf.zip copied to $$(readlink -e mainApp/extras)"; \
	else \
		echo "LDraw archive library lpub3dldrawunf.zip not found at $$(readlink -e ../SOURCES)!"; \
	fi
	if [ -f ../SOURCES/tenteparts.zip ]; then \
		cp ../SOURCES/tenteparts.zip mainApp/extras && echo "LDraw archive library tenteparts.zip copied to $$(readlink -e mainApp/extras)"; \
	else \
		echo "LDraw archive library tenteparts.zip not found at $$(readlink -e ../SOURCES)!"; \
	fi
	if [ -f ../SOURCES/vexiqparts.zip ]; then \
		cp ../SOURCES/vexiqparts.zip mainApp/extras && echo "LDraw archive library vexiqparts.zip copied to $$(readlink -e mainApp/extras)"; \
	else \
		echo "LDraw archive library vexiqparts.zip not found at $$(readlink -e ../SOURCES)!"; \
	fi
	for TarballFile in \
		../SOURCES/ldglite.tar.gz \
		../SOURCES/ldview.tar.gz \
		../SOURCES/povray.tar.gz; do \
		if [ -f "$${TarballFile}" ]; then \
			mv -f $${TarballFile} ../ && echo "$$(basename $${TarballFile}) copied to $$(readlink -e ../)"; \
		fi; \
	done
	if [ -n "$${LP3D_UCS_5}" ] || [ -n "$${LP3D_DEBIAN_10}" ]; then \
		for TarballFile in \
			../SOURCES/mesa-18.3.5.tar.gz \
			../SOURCES/glu-9.0.0.tar.bz2; do \
			if [ -f "$${TarballFile}" ]; then \
				mv -f $${TarballFile} ../ && echo "$$(basename $${TarballFile}) copied to $$(readlink -e ../)"; \
			fi; \
		done; \
	fi
	BINARY_FILE_PATH=$$(readlink -e ../)
	BINARY_FILE_LIST=builds/linux/obs/debian/source/include-binaries; \
	sed -i -e "s/__path__/$${BINARY_FILE_PATH}/g" "$${BINARY_FILE_LIST}" \
		   -e "s/__arch__/${TARGET_CPU}/g" "$${BINARY_FILE_LIST}"; \
	echo "Updated `pwd`/$${BINARY_FILE_LIST}:\n`cat $${BINARY_FILE_LIST}`"
	# build 3rd-party renderers
	export LPUB3D=lpub3d; \
	export WD=$$(readlink -e ../); \
	export LDRAWDIR=${HOME}/ldraw; \
	export LP3D_LOG_PATH=${LP3D_LOG_PATH:-$${WD}}; \
	export LP3D_CPU_CORES=${LP3D_CPU_CORES:-$$(nproc)}; \
	export LP3D_3RD_DIST_DIR=${LP3D_3RD_DIST_DIR:-lpub3d_linux_3rdparty}; \
	export build_osmesa=$$($$([ -n "$${LP3D_UCS_VER}" ] || [ -n "$${LP3D_DEBIAN_10}" ]) && echo 1 || :); \
	chmod a+x builds/utilities/CreateRenderers.sh && ./builds/utilities/CreateRenderers.sh
override_dh_auto_configure:
	# LDraw directory - for build check
	export LDRAWDIR=${HOME}/ldraw; \
	if which qmake >/dev/null 2>/dev/null ; then \
		qmake -v; \
		qmake -makefile -nocache QMAKE_STRIP=: CONFIG+=release CONFIG+=build_check CONFIG-=debug_and_release CONFIG+=deb; \
	else \
		echo "ERROR - qmake not found"; \
	fi
	# End custom configuration commands
	touch configure-stamp

build: configure-stamp
override_dh_auto_test:
	# Start custom test commands
	[ -f "${LP3D_VALID_EXE}" ] && echo "LDD check lpub3d${LP3D_VER_SUFFIX}..." && ldd ${LP3D_VALID_EXE} 2>/dev/null || \
	echo "ERROR - LDD check failed for $$(realpath ${LP3D_VALID_EXE})"
	# End custom test commands
	dh_auto_test

# override and shut down shared library dependency checks
override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

clean:
	dh_testdir
	dh_testroot
	rm -f configure-stamp
	dh_lintian

	dh_clean

override_dh_strip_nondeterminism:

.PHONY: configure build clean
