#!/usr/bin/make -f

major-rubyversion=$(shell ruby -v | cut -d ' ' -f 2 | cut -d '.' -f 1)
minor-rubyversion=$(shell ruby -v | cut -d ' ' -f 2 | cut -d '.' -f 2)
minormax-rubyversion=$(shell echo $$(( $(minor-rubyversion) + 1 )) )

%:
	dh $@

override_dh_clean:
	dh_clean --exclude=external/serialport/README.orig
	rm -rf vendor/bundle
	# remove Gemfile.lock to avoid conflict / issues when we modify
	# the required versions in gemspec and to remove the Bundler
	# version specification (see
	# https://bugs.kali.org/view.php?id=6110)
	rm Gemfile.lock

override_dh_auto_configure:
	# Do nothing

override_dh_auto_build:
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),amd64 i386))
	patch -p1 <debian/oci8-gemspec.patch
endif

	# Build binary gems
	bundle config set --local path vendor/bundle
	bundle config set --local without 'development test coverage'
	# GCC 14 is stricter, and sqlite3 1.4.4 fails to build. Please remove the
	# line below when the patch Force-sqlite-3-version-1.4.4.patch is dropped.
	bundle config --local build.sqlite3 --with-cflags="'-Wno-error=incompatible-pointer-types -Wno-error=int-conversion'"
	bundle install
	bundle config --local frozen true

override_dh_gencontrol:
	dh_gencontrol -- -Vruby:Depends="ruby (>= 1:$(major-rubyversion).$(minor-rubyversion)), ruby (<< 1:$(major-rubyversion).$(minormax-rubyversion))"

override_dh_install:
	dh_install --exclude=data/gui --exclude=LICENSE --exclude=LICENCE --exclude=LICENSE.txt
	# Hard code shebang for ruby scripts
	for binfile in $$(ls -d -1 debian/metasploit-framework/usr/share/metasploit-framework/tools/exploit/*.rb debian/metasploit-framework/usr/share/metasploit-framework/msf*); do \
	  sed -i $${binfile} -e '1,1s|#!.*ruby|#!/usr/share/metasploit-framework/ruby|' ; \
	done
	chmod -x debian/metasploit-framework/usr/share/metasploit-framework/data/wordlists/*

	# install bash completion for msfvenom
	mkdir -p debian/metasploit-framework/usr/share/bash-completion/completions
	HOME=/tmp RUBYLIB=debian/metasploit-framework/usr/share/metasploit-framework/lib debian/generate-msfvenom-bash-completion.rb > debian/metasploit-framework/usr/share/bash-completion/completions/msfvenom

override_dh_shlibdeps:
	dh_shlibdeps --exclude=/data/ --exclude=/modules/

override_dh_makeshlibs:
	dh_makeshlibs --exclude=/data/ --exclude=/metasploit_payloads-mettle

override_dh_strip:
	dh_strip --exclude=/data/ --exclude=/modules/ --exclude=/vendor/bundle/

override_dh_compress:
	dh_compress --exclude=/modules/

override_dh_dwz:
	# do nothing: it fails because of multiple errors on bundle's
	# binaries or other binaries
