#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

#DEB_HOST_MULTIARCH =

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

#export http_proxy=http://192.168.8.254:3130
export no_proxy="localhost"
export ftp_proxy=http://192.168.8.254:3138

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
#DEB_HOST_GNU_TYPE   = i686-linux-gnu
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
#DEB_BUILD_GNU_TYPE  = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


# original
#export CFLAGS = -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export SERVERCFLAGS = -Wall -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export CXXFLAGS = -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -std=gnu++0x -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export LDFLAGS = -Wl,--as-needed -Wl,--allow-multiple-definition -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=stringop-overread

# GCC 8 / Buster
#export CFLAGS = -lzstd -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -march=x86-64-v2 -mtune=generic -fno-lto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr
#export SERVERCFLAGS = -lzstd -Wall -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -march=x86-64-v2 -mtune=generic -fno-lto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr
#export CXXFLAGS = -lzstd -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -std=gnu++0x -march=x86-64-v2 -mtune=generic -fno-lto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr
#export LDFLAGS = -lzstd -Wl,--as-needed -Wl,--allow-multiple-definition -march=x86-64-v2 -mtune=generic -fno-lto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr
# Buster with lto
#export CFLAGS = -lzstd -Wall -Werror -g -O3 -Wl,-z,lazy -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr
#export SERVERCFLAGS = -lzstd -Wall -g -O3 -Wl,-z,lazy -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr
#export CXXFLAGS = -lzstd -Wall -Werror -g -O3 -Wl,-z,lazy -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -std=gnu++0x -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr
#export LDFLAGS = -lzstd -lunistring -Wl,--as-needed -Wl,--allow-multiple-definition -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr
# Bookworm really with lto
export CFLAGS = -lzstd -Wall -Werror -g -O3 -Wl,-z,lazy -march=x86-64-v2 -mtune=generic -flto=16 -ffat-lto-objects -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
export SERVERCFLAGS = -lzstd -Wall -g -O3 -Wl,-z,lazy -march=x86-64-v2 -mtune=generic -flto=16 -ffat-lto-objects -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
export CXXFLAGS = -lzstd -Wall -Werror -g -O3 -Wl,-z,lazy -march=x86-64-v2 -mtune=generic -flto=16 -ffat-lto-objects -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -std=gnu++0x -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
export LDFLAGS = -lzstd -lunistring -Wl,--as-needed -Wl,--allow-multiple-definition -march=x86-64-v2 -mtune=generic -flto -ffat-lto-objects -fPIE -D_GLIBCXX_ASSERTIONS -fstack-clash-protection -Wno-stringop-overflow -Wno-error=stringop-truncation -Wno-error=odr -march=x86-64-v2 -mtune=generic -flto=16 -ffat-lto-objects

#export CFLAGS = -nolibc -static-pie -lc -lgcc -lunwind -llzma -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export SERVERCFLAGS = -nolibc -static-pie -lc -lgcc -lunwind -llzma -Wall -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export CXXFLAGS = -nolibc -static-pie -lc -lgcc -lunwind -llzma -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -std=gnu++0x -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export LDFLAGS = -nolibc -static-pie -lc -lgcc -lunwind -llzma -Wl,--as-needed -Wl,--allow-multiple-definition -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=stringop-overread
#export CFLAGS = -nodefaultlibs -static-pie -lc -lm -lgcc -lgcc_eh -lunwind -llzma -ldl -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export SERVERCFLAGS = -nodefaultlibs -static-pie -lc -lm -lgcc -lgcc_eh -lunwind -llzma -ldl -Wall -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export CXXFLAGS = -nodefaultlibs -static-pie -lc -lm -lgcc -lgcc_eh -lunwind -llzma -ldl -Wall -Werror -g -O3 -Wl,-z,lazy -Wno-deprecated -Wno-deprecated-declarations -fstack-protector-all -std=gnu++0x -mtune=corei7 -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=return-local-addr -Wno-error=stringop-overread
#export LDFLAGS = -nodefaultlibs -static-pie -lc -lm -lgcc -lgcc_eh -lunwind -llzma -ldl -Wl,--as-needed -Wl,--allow-multiple-definition -flto=8 -ffat-lto-objects -fPIE -Wno-stringop-overflow -Wno-alloc-size-larger-than -Wno-error=stringop-truncation -Wno-error=odr -Wno-error=stringop-overread

#LDFLAGS += -Wl,-Bstatic -lheif -lx265 -lde265 -laom -lopenh264 -Wl,-Bdynamic -Wno-error=return-local-addr
CMAKE_BUILD_TYPE = Debug
INCLDIR = /usr/include/

libsearch = $(shell find /lib /usr/lib -iname $(1) -print -quit)

TURBOJPEG = $(call libsearch,libturbojpeg.a)
GNUTLS = $(call libsearch,libgnutls.a)
NETTLE = $(call libsearch,libnettle.a)
TASN1 = $(call libsearch,libtasn1.a)
HOGWEED = $(call libsearch,libhogweed.a)
CRYPT = $(call libsearch,libcrypt.a)
GSASL = $(call libsearch,libgsasl.a)
IDN = $(call libsearch,libidn.a)
NTLM = $(call libsearch,libntlm.a)
Z = $(call libsearch,libz.a)
P11-KIT = $(call libsearch,libp11-kit.so)
GSSAPI_KRB5 = $(call libsearch,libgssapi_krb5.a)
KRB5 = $(call libsearch,libkrb5.a)
KRB5SUPPORT = $(call libsearch,libkrb5support.a)
K5CRYPTO = $(call libsearch,libk5crypto.a)
COM_ERR = $(call libsearch,libcom_err.a)
RT = $(call libsearch,librt.so)
GMP = $(call libsearch,libgmp.a)
KEYUTILS = $(call libsearch,libkeyutils.a)
RESOLV = $(call libsearch,libresolv.so)
GCRYPT = $(call libsearch,libgcrypt.a)
GPG-ERROR = $(call libsearch,libgpg-error.a)
PAM = $(call libsearch,libpam.a)
DL = $(call libsearch,libdl.a)

#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#	CFLAGS += -O0
#else
#	CFLAGS += -Os
#endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

export VNCCFGFLAGS = --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE)
export XORGCFGFLAGS = --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE)

#export CC=gcc-8
#export CXX=g++-8

#  -DGNUTLS_LIBRARY="$(GNUTLS);$(NETTLE);$(TASN1);$(HOGWEED);$(CRYPT);$(GSASL);$(IDN);$(NTLM);$(Z);$(P11-KIT);$(GSSAPI_KRB5);$(KRB5);$(KRB5SUPPORT);$(K5CRYPTO);$(COM_ERR);$(RT);$(GMP);$(KEYUTILS);$(RESOLV)" \

SOLIST = heif x265 de265 dav1d x264 openh264 zstd webp gnutls freetype \
	unistring nettle hogweed gmp \
	com_err keyutils resolv webpmux webpdemux \
	unistring jxl jxl_threads jxl_dec hwy lcms2 brotlidec brotlienc brotlicommon \
	jpeg Xrender Xext X11 Xi pixman-1 gsasl pcre2-8 gcrypt stdc++ \
	png1 gcc_s Xcursor Xfixes Xft fontconfig Xinerama xcb idn gpg-error \
	freetype expat uuid Xau Xdmcp com_err \
	brotlidec bsd keyutils resolv brotlicommon ntlm png16 rsbac
SOLIST2 = gsasl

config.status: patch
	dh_testdir
	@for i in $(SOLIST) ; do rm -v -f /usr/lib/*/lib$${i}.so ; done
	@for i in $(SOLIST2) ; do rm -v -f /usr/lib/lib$${i}.so ; done

	LDFLAGS="$(LDFLAGS)" CFLAGS="-D_FILE_OFFSET_BITS=64 $(CFLAGS)" CXXFLAGS="-D_FILE_OFFSET_BITS=64 $(CXXFLAGS)" cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) -DCMAKE_INSTALL_PREFIX=$(CURDIR)/debian/tmp/usr/ -DBUILD_STATIC=1 \
		-DJPEG_INCLUDE_DIR="$(INCLDIR)/" \
		-DGNUTLS_LIBRARY="$(GNUTLS)" \
		-DJPEG_LIBRARY="$(TURBOJPEG)" \
		-DGCRYPT_LIBRARY="$(GCRYPT)" \
		-DGPG_ERROR_LIBRARY="$(GPG-ERROR)" \
		-DFL="$(PAM)" -DENABLE_NLS=1\
		-DZLIB_LIBRARY="$(Z)" \
		-DFLTK_LIBRARIES="$(DL)" .

# -DICONV_LIBRARIES="$(LIBDIR)/libc.a" \
#  -DFLTK_MATH_LIBRARY="$(LIBDIR)/libm.a" \

#Architecture
build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: config.status
#	for i in gssapi_krb5 krb5 k5crypto krb5support mount ; do ln -s lib$$i.so /usr/lib/$(DEB_HOST_MULTIARCH)/lib$$i.a ; done
	$(MAKE) -j16
#	strip ./vncviewer/vncviewer
	touch build-arch-stamp

build-server-stamp: build-arch-stamp
	unix/build-xorg init -version 7.5
	unix/build-xorg build -version 7.5

	touch build-server-stamp

build-indep: build-indep-stamp
build-indep-stamp:  config.status

	# Add here commands to compile the indep part of the package.
	#$(MAKE) doc
	touch build-indep-stamp

clean: unpatch
	dh_testdir
	dh_testroot
#	rm -rf $(CURDIR)/debian/icons
	-rm -Rf $(CURDIR)/debian/tmp
	rm -f build-arch-stamp build-indep-stamp #CONFIGURE-STAMP#

	# Add here commands to clean up after the build process.
	-rm -f config.log config.status config.cache config.guess config.sub
	-rm -Rf buildtemp
	-$(MAKE) -C common distclean
	-$(MAKE) -C common clean
	-$(MAKE) -C unix distclean
	-$(MAKE) -C unix clean
	-$(MAKE) -C unix/xserver distclean
	-$(MAKE) -C unix/xserver clean

	dh_clean

install: install-indep install-arch
install-indep:
##	dh_testdir
##	dh_testroot
##	dh_clean -k -i
##	dh_installdirs -i
##
##	# Add here commands to install the indep part of the package into
##	# debian/<package>-doc.
##	#INSTALLDOC#
##
##	dh_install -i

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k -s
	dh_installdirs -s

	# Add here commands to install the arch part of the package into
	# debian/tmp.
	#$(MAKE) install prefix=$(CURDIR)/debian/vnc/usr
	#cd unix && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
	#cd unix/xserver && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
	$(MAKE) install
	#prefix=/usr DESTDIR=$(CURDIR)/debian/tmp/
	#no install target yet
#	cp -r xorg.build/* $(CURDIR)/debian/tmp/usr
	rename "s/vncviewer/tightgateviewer/;" \
		debian/tmp/usr/local/bin/* \
		debian/tmp/usr/bin/* \
		debian/tmp/usr/share/man/man1/*
#	rename "s/tiger/tightgate/;" \
#		debian/tmp/usr/share/locale/*/LC_MESSAGES/*
	rename "s/vnc/tightgatevnc/;" \
		debian/tmp/usr/local/bin/* \
		debian/tmp/usr/bin/* \
		debian/tmp/usr/share/man/man1/*
#	strip -only-keep-debug --keep-file-symbols debian/tightgateviewer/usr/bin/tightgateviewer
	mkdir -p debian/tightgateviewer/usr/share/applications
	cp debian/tightgateviewer.desktop debian/tightgateviewer/usr/share/applications/
	debian/buildicons.sh tightgateviewer.svg apps
	dh_install -s
#	mv debian/tightgateviewer/usr/bin/tightgateviewer debian/tightgateviewer/usr/bin/tightgateviewer.bin
#	sed -e "s|+ARCH+|$(DEB_HOST_MULTIARCH)|g" < debian/tightgateviewer-wrapper > debian/tightgateviewer/usr/bin/tightgateviewer
#	chmod 755 debian/tightgateviewer/usr/bin/tightgateviewer
#	mkdir -p debian/tightgateviewer/usr/lib/$(DEB_HOST_MULTIARCH)/tightgateviewer
#	cp -a /lib/$(DEB_HOST_MULTIARCH)/ld-*.so debian/tightgateviewer/usr/lib/$(DEB_HOST_MULTIARCH)/tightgateviewer/ld-linux-x86-64.so.2
#	cp -a /lib/$(DEB_HOST_MULTIARCH)/libc-2.33.so debian/tightgateviewer/usr/lib/$(DEB_HOST_MULTIARCH)/tightgateviewer/libc.so.6
#	cp -a /lib/$(DEB_HOST_MULTIARCH)/libdl-2.33.so debian/tightgateviewer/usr/lib/$(DEB_HOST_MULTIARCH)/tightgateviewer/libdl.so.2
#	cp -a /lib/$(DEB_HOST_MULTIARCH)/libm-2.33.so debian/tightgateviewer/usr/lib/$(DEB_HOST_MULTIARCH)/tightgateviewer/libm.so.6
#	cp -a /lib/$(DEB_HOST_MULTIARCH)/libpthread-2.33.so debian/tightgateviewer/usr/lib/$(DEB_HOST_MULTIARCH)/tightgateviewer/libpthread.so.0

patch: patch-stamp
patch-stamp:
#	patch /usr/lib/fltk/FLTK-Targets.cmake debian/mp-FLTK-Targets.diff
	dpatch apply-all
	dpatch cat-all >patch-stamp
#	tar xjf xorg-1.6.tar.bz2
#	cp -r xorg-1.6/* unix/xserver/
#	cd unix/xserver && patch -p1 < ../xserver16.patch
#	cd unix/xserver && patch -p1 < ../debian/patches/tls/xorg-tls-vnc-build.patch
	#little on the fly patching for a configure bug
	#probably not needed with debian squeeze
#	sed -i 's/@MKINSTALLDIRS@/..\/mkinstalldirs/' unix/po/Makefile.in.in
        #some more Amon on the fly patching for some gcc problem
	rm /usr/lib/x86_64-linux-gnu/crt1.o || true ; ln -s Scrt1.o /usr/lib/x86_64-linux-gnu/crt1.o || true

unpatch:
	dpatch deapply-all
	rm -rf patch-stamp debian/patched
	patch -R /usr/lib/fltk/FLTK-Targets.cmake debian/mp-FLTK-Targets.diff || true

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_install debian/icons/* usr/share/icons
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_fixperms
	dh_strip
#	strip debian/tmp/usr/bin/tightgateviewer
#	strip debian/tmp/usr/bin/Xtightgatevnc
#	strip debian/tmp/usr/bin/tightgatevncconfig
#	strip debian/tmp/usr/bin/tightgatevncpasswd
#	strip debian/tmp/usr/bin/x0tightgatevncserver
	mp_rsbac
	dh_compress
#	dh_perl
#	dh_python
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums || true
	dh_builddeb -- -Zxz
# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
##	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

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