#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatibility version to use.
# export DH_COMPAT=7

CFLAGS = -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

DEST=$(shell if [ -f /usr/src/packages/BUILD/QT/LDView.pro ] ; then echo /usr/src/packages/BUILD/debian/ldview ; else echo $(shell pwd)/debian/ldview ; fi )
export Q_LDFLAGS="-Wl,-z,relro"
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

build-arch: build
build-indep: build
build: build-stamp
build-stamp:
	dh_testdir

	# Add here commands to compile the package.
	make -C OSMesa STATIC="$(LDFLAGS)" USE_CPP11=YES -j$(shell nproc) all
	lrelease QT/LDView.pro
	cd QT;qmake6 -config release USE_CPP11=YES
	make -C QT -j$(shell nproc) all
	cp -f QT/LDView QT/LDView.qt6
	export QT_SELECT=5
	cd QT ;	qmake -config release USE_CPP11=YES
	make -C QT -j$(shell nproc) compiler_clean all
	cp -f QT/LDView QT/LDView.qt5
	# --- end custom part for compiling

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	make -C QT clean || true
	make -C OSMesa clean || true
	# --- end custom part for cleaning up
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package
	# The DESTDIR Has To Be Exactly  /usr/src/packages/BUILD/debian/<nameOfPackage>
	make -C QT INSTALL_ROOT=$(DEST) install
	cp -f QT/LDView.qt6 $(DEST)/usr/bin/LDView
	make -C QT INSTALL_ROOT=$(DEST)-qt5 install
	cp -f QT/LDView.qt5 $(DEST)-qt5/usr/bin/LDView
	rm -f $(DEST)/usr/share/ldview/license.txt
	rm -f $(DEST)-qt5/usr/share/ldview/license.txt
	make -C OSMesa PREFIX=$(DEST)-osmesa install	

	# --- end custom part for installing

# Build architecture-independent files here.
binary-indep: build install
	# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installman
	dh_installinfo
#	dh_gconf
#	dh_undocumented
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
