#!/usr/bin/make -f
export deb_udevdir = $(shell pkg-config --variable=udevdir udev | sed s,^/,,)

include /usr/share/dpkg/pkg-info.mk

# Since we use regex tests with =~, we need bash
SHELL := /bin/bash

DH_VERBOSE=1

PLATFORMS_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh PLATFORMS_VERSIONS_TO_PACKAGE)
BUILD_TOOLS_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh BUILD_TOOLS_VERSIONS_TO_PACKAGE)
PATCHER_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh PATCHER_VERSIONS_TO_PACKAGE)
CMDLINE_TOOLS_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh CMDLINE_TOOLS_VERSIONS_TO_PACKAGE)
PLATFORM_TOOLS_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh PLATFORM_TOOLS_VERSIONS_TO_PACKAGE)
NDK_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh NDK_VERSIONS_TO_PACKAGE)
SOURCES_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh SOURCES_VERSIONS_TO_PACKAGE)
EMULATOR_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh EMULATOR_VERSIONS_TO_PACKAGE)
EXTRAS_GOOGLE_AUTO_VERSIONS_TO_PACKAGE=$(shell debian/scripts/components_to_package.sh EXTRAS_GOOGLE_AUTO_VERSIONS_TO_PACKAGE)

WITH_ZIPS := $(shell if grep "XS-With-Zips: yes" debian/control.in > /dev/null; then echo yes; else echo no; fi)

%:
	dh $@ --with bash-completion

override_dh_clean:
	dh_clean

	@echo "Remove per package autogenerated files"
	@debian/scripts/run_dh_clean.sh

	@echo "Run debian/scripts/update_debian.sh"
	@debian/scripts/update_debian.sh

	debconf-updatepo

ifneq ($(WITH_ZIPS),yes)
	rm -fr zips/
endif

execute_after_dh_prep:
	@echo "Generating files for components"
	debian/scripts/run_after_dh_prep.sh

execute_after_dh_installdirs:
	@debian/scripts/zip_file_install.sh

override_dh_gencontrol:
	debian/scripts/run_dh_gencontrol.sh "$(DEB_VERSION)"

execute_before_dh_installalternatives:
	# build-tools: create files used in $package.alternatives file so that dh_installalternatives can run
	@echo "Prepare dh_installalternatives for build-tools"
	@for version in ${BUILD_TOOLS_VERSIONS_TO_PACKAGE}; do \
	  version=$$(echo $$version | sed "s/,/;/g");\
	  VER=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f3) ;\
	  PATH_DIRNAME=$$(dirname $$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | sed "s,;,/,g")) ;\
	  PATH_BASENAME=$$(basename $$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | sed "s,;,/,g")) ;\
	  for file in $$(grep "Name:" debian/google-android-build-tools-$$VER-installer.alternatives | cut -d ' ' -f2); do \
	    mkdir -p debian/google-android-build-tools-$$VER-installer/usr/lib/android-sdk/$$PATH_DIRNAME/$$PATH_BASENAME ;\
	    touch debian/google-android-build-tools-$$VER-installer/usr/lib/android-sdk/$$PATH_DIRNAME/$$PATH_BASENAME/$$file ;\
	  done ;\
	done

	# cmdline-tools: create files used in $package.alternatives file so that dh_installalternatives can run
	@echo "Prepare dh_installalternatives for cmdline-tools"
	@for version in ${CMDLINE_TOOLS_VERSIONS_TO_PACKAGE}; do \
	  version=$$(echo $$version | sed "s/,/;/g");\
	  VER=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f3) ;\
	  PATH_DIRNAME=$$(dirname $$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | sed "s,;,/,g")) ;\
	  PATH_BASENAME=$$(basename $$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | sed "s,;,/,g")) ;\
	  for file in $$(grep "Name:" debian/google-android-cmdline-tools-$$VER-installer.alternatives | cut -d ' ' -f2); do \
	    mkdir -p debian/google-android-cmdline-tools-$$VER-installer/usr/lib/android-sdk/$$PATH_DIRNAME/$$PATH_BASENAME/bin ;\
	    touch debian/google-android-cmdline-tools-$$VER-installer/usr/lib/android-sdk/$$PATH_DIRNAME/$$PATH_BASENAME/bin/$$file ;\
	  done ;\
	done

	# ndk: create files used in $package.alternatives file so that dh_installalternatives can run
	@echo "Prepare dh_installalternatives for ndk"
	@for version in ${NDK_VERSIONS_TO_PACKAGE}; do \
	  version=$$(echo $$version | sed "s/,/;/g");\
	  VER=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f3) ;\
	  ver_major=$$(echo "$$VER" | cut -d . -f 1) ;\
	  ver_letter=$$(echo "$$VER" | cut -d . -f 2 | tr '1-9' 'b-z' | tr -d '0') ;\
	  VER="r$${ver_major}$${ver_letter}" ;\
	  PATH_DIRNAME=$$(dirname $$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | sed "s,;,/,g")) ;\
	  PATH_BASENAME=$$(basename $$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | sed "s,;,/,g")) ;\
	  for file in $$(grep "Name:" debian/google-android-ndk-$$VER-installer.alternatives | cut -d ' ' -f2); do \
	    mkdir -p debian/google-android-ndk-$$VER-installer/usr/lib/android-sdk/$$PATH_DIRNAME ;\
	    touch debian/google-android-ndk-$$VER-installer/usr/lib/android-sdk/$$PATH_DIRNAME/$$PATH_BASENAME ;\
	  done ;\
	done

execute_after_dh_installalternatives:
	# build-tools: delete files created so that dh_installalternatives can run
	@echo "Cleanup preparation of dh_installalternatives for build-tools"
	@for version in ${BUILD_TOOLS_VERSIONS_TO_PACKAGE}; do \
	  version=$$(echo $$version | sed "s/,/;/g");\
	  VER=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f3) ;\
	  PATH_FIRSTDIR=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | cut -d ";" -f 1) ;\
	  rm -rf debian/google-android-build-tools-$$VER-installer/usr/lib/android-sdk/$$PATH_FIRSTDIR ;\
	  rmdir -p --ignore-fail-on-non-empty debian/google-android-build-tools-$$VER-installer/usr/lib/android-sdk ;\
	done

	# cmdline-tools: delete files created so that dh_installalternatives can run
	@echo "Cleanup preparation of dh_installalternatives for cmdline-tools"
	@for version in ${CMDLINE_TOOLS_VERSIONS_TO_PACKAGE}; do \
	  version=$$(echo $$version | sed "s/,/;/g");\
	  VER=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f3) ;\
	  PATH_FIRSTDIR=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | cut -d ";" -f 1) ;\
	  rm -rf debian/google-android-cmdline-tools-$$VER-installer/usr/lib/android-sdk/$$PATH_FIRSTDIR ;\
	  rmdir -p --ignore-fail-on-non-empty debian/google-android-cmdline-tools-$$VER-installer/usr/lib/android-sdk ;\
	done

	# ndk: delete files created so that dh_installalternatives can run
	@echo "Cleanup preparation of dh_installalternatives for ndk"
	@for version in ${NDK_VERSIONS_TO_PACKAGE}; do \
	  version=$$(echo $$version | sed "s/,/;/g");\
	  VER=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f3) ;\
	  ver_major=$$(echo "$$VER" | cut -d . -f 1) ;\
	  ver_letter=$$(echo "$$VER" | cut -d . -f 2 | tr '1-9' 'b-z' | tr -d '0') ;\
	  VER="r$${ver_major}$${ver_letter}" ;\
	  PATH_FIRSTDIR=$$(grep --perl-regex "^$$version\t" debian/version_list.txt | cut -d "	" -f1 | cut -d ";" -f 1) ;\
	  rm -rf debian/google-android-ndk-$$VER-installer/usr/lib/android-sdk/$$PATH_FIRSTDIR ;\
	  rmdir -p --ignore-fail-on-non-empty debian/google-android-ndk-$$VER-installer/usr/lib/android-sdk ;\
	done

override_dh_builddeb:
	# On Ubuntu, by default, compression format is "zstd" while it is "xz" in Debian.
	# However dpkg in Debian supports unpacking zstd deb's only since 1.21.18 which
	# is only released since bookworm (debian 12). So that the deb can be used on older
	# Debian, especially when taken from Ubuntu, set the compression format to xz.
	dh_builddeb -- -Zxz
