#!/usr/bin/make -f

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)

%:
	dh $@ --with bash-completion

override_dh_clean:
	dh_clean

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

	@echo "Update debian/control file"
	@debian/scripts/build_control_file.sh

	@echo "Update debian/tests/control file"
	@debian/scripts/build_tests_control_file.sh

	debconf-updatepo

execute_after_dh_prep:
	@echo "Generating files for components"
	debian/scripts/run_after_dh_prep.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/build-tools.alternatives.in | 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/cmdline-tools.alternatives.in | 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

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
