-get-available-tz-versions = \
  $(eval _bf_platform := $(1)) \
  $(if $(filter teegris,$(_bf_platform)), \
    TEEGRIS-4.1 TEEGRIS-4.0 TEEGRIS-3.0, \
    TEEGRIS-2.0 TEEGRIS-3.0 BF-2.0 BF-1.0)

get-blowfish-nwd-include-paths = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _bf_platform := $(2)) \
  $(eval _available_tz_versions := $(call -get-available-tz-versions, $(_bf_platform))) \
  $(eval _blowfish_nwd_includes := ) \
  $(foreach _tz_version,$(_available_tz_versions), \
    $(eval _blowfish_nwd_includes += \
      $(_blowfish_sdk_path)/../../platforms/$(_tz_version)/nwd/android/includes)) \
  $(eval  _blowfish_nwd_includes += $(_blowfish_sdk_path)/nw_libs) \
  $(eval _blowfish_chosen_inc := \
    $(foreach _blowfish_nwd_inc_path,$(_blowfish_nwd_includes), \
      $(if $(wildcard $(_blowfish_nwd_inc_path)), \
        $(_blowfish_nwd_inc_path)))) \
  $(word 1,$(_blowfish_chosen_inc))

get-blowfish-swd-include-paths = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _bf_platform := $(2)) \
  $(eval _blowfish_arch := $(3)) \
  $(eval _available_tz_versions := $(call -get-available-tz-versions, $(_bf_platform))) \
  $(eval _blowfish_swd_includes := ) \
  $(foreach _tz_version,$(_available_tz_versions), \
    $(eval _blowfish_swd_includes += \
      $(_blowfish_sdk_path)/../../platforms/$(_tz_version)/swd/$(_blowfish_arch)/usr/include )) \
  $(eval _blowfish_chosen_inc := \
    $(foreach _blowfish_swd_inc_path,$(_blowfish_swd_includes), \
      $(if $(wildcard $(_blowfish_swd_inc_path)), \
        $(_blowfish_swd_inc_path)))) \
  $(word 1,$(_blowfish_chosen_inc))

-get-blowfish-nwd-libs = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _bf_platform := $(2)) \
  $(eval _blowfish_nwd_arch := $(3)) \
  $(eval _available_tz_versions := $(call -get-available-tz-versions, $(_bf_platform))) \
  $(eval _blowfish_client_libs := ) \
  $(foreach _tz_version,$(_available_tz_versions), \
    $(eval _blowfish_client_libs += \
      $(_blowfish_sdk_path)/../../platforms/$(_tz_version)/nwd/android/arch-arm$(_blowfish_nwd_arch)/libteecl.so)) \
  $(eval _blowfish_client_libs += $(_blowfish_sdk_path)/nw_libs/lib$(_blowfish_nwd_arch)/libteecl.so) \
  $(eval _blowfish_chosen_lib := \
    $(foreach _blowfish_client_lib,$(_blowfish_client_libs), \
      $(if $(wildcard $(_blowfish_client_lib)), \
        $(_blowfish_client_lib)))) \
  $(word 1,$(_blowfish_chosen_lib))

get-blowfish-armv7-nwd-libs = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _bf_platform := $(2)) \
  $(call -get-blowfish-nwd-libs,$(_blowfish_sdk_path),$(_bf_platform),)

get-blowfish-aarch64-nwd-libs = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _bf_platform := $(2)) \
  $(call -get-blowfish-nwd-libs,$(_blowfish_sdk_path),$(_bf_platform),64)

get-blowfish-swd-libdir = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _bf_platform := $(2)) \
  $(eval _browfish_arch := $(3)) \
  $(eval _available_tz_versions := $(call -get-available-tz-versions, $(_bf_platform))) \
  $(eval _blowfish_swd_libdirs := ) \
  $(foreach _tz_version,$(_available_tz_versions), \
    $(eval _blowfish_swd_libdirs += \
      $(_blowfish_sdk_path)/../../platforms/$(_tz_version)/swd/$(_browfish_arch)/usr/lib/)) \
  $(eval _blowfish_chosen_libdir := \
    $(foreach _blowfish_libdir,$(_blowfish_swd_libdirs), \
      $(if $(wildcard $(_blowfish_libdir)), \
        $(_blowfish_libdir)))) \
  $(word 1,$(_blowfish_chosen_libdir))

-get-blowfish-auth-scripts-path = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _blowfish_auth_dirs := \
    $(_blowfish_sdk_path)/../../tools/bf_authority_scripts \
    $(_blowfish_sdk_path)/../../tools/teegris_authority_scripts \
    $(_blowfish_sdk_path)/bf_authority_scripts) \
  $(eval _blowfish_chosen_dir := \
    $(foreach _blowfish_auth_dir,$(_blowfish_auth_dirs), \
      $(if $(wildcard $(_blowfish_auth_dir)), \
        $(_blowfish_auth_dir)))) \
  $(word 1,$(_blowfish_chosen_dir))

get-blowfish-local-cert = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _blowfish_board := $(2)) \
  $(strip $(call -get-blowfish-auth-scripts-path,$(_blowfish_sdk_path))/$(_blowfish_board)/cert.pem)

get-blowfish-local-key = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _blowfish_board := $(2)) \
  $(strip $(call -get-blowfish-auth-scripts-path,$(_blowfish_sdk_path))/$(_blowfish_board)/private/key.pem)

get-blowfish-local-sign-script = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _sign_scripts := \
    $(strip $(call -get-blowfish-auth-scripts-path,$(_blowfish_sdk_path))/ta_auth_scripts/sign_file.sh) \
    $(strip $(call -get-blowfish-auth-scripts-path,$(_blowfish_sdk_path))/sign_file.sh)) \
  $(eval _chosen_script := \
    $(foreach _sign_script,$(_sign_scripts), \
      $(if $(wildcard $(_sign_script)), \
        $(_sign_script)))) \
  $(word 1,$(_chosen_script))

get-blowfish-local-sign-output-name = \
  $(eval _local_uuid := $(1)) \
  $(shell bash -c 's='$(_local_uuid)' && echo $${s:0:8}-$${s:8:4}-$${s:12:4}-$${s:16:4}-$${s:20:12}')

get-blowfish-local-parsed-uuid = \
  $(eval _local_uuid := $(1)) \
  $(shell bash -c 's='$(_local_uuid)' && echo {0x$${s:0:8}, 0x$${s:8:4}, 0x$${s:12:4}, {0x$${s:16:2}, 0x$${s:18:2}, 0x$${s:20:2}, 0x$${s:22:2}, 0x$${s:24:2}, 0x$${s:26:2}, 0x$${s:28:2}, 0x$${s:30:2}}}')

get-blowfish-toolchain-prefix = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(eval _blowfish_prefixes := \
    aarch64-secureos-gnueabi- \
    arm-secureos-gnueabi- \
    arm-v7a9-secureos-gnueabi-) \
  $(eval _blowfish_chosen_prefix := \
    $(foreach _prefix,$(_blowfish_prefixes), \
      $(if $(wildcard $(_blowfish_sdk_path)/bin/$(_prefix)gcc), \
        $(_prefix)))) \
  $(word 1,$(_blowfish_chosen_prefix))

get-blowfish-swd-arch = \
  $(eval _blowfish_sdk_path := $(1)) \
  $(if $(filter $(strip $(patsubst %-secureos-gnueabi-,%,$(call get-blowfish-toolchain-prefix,$(_blowfish_sdk_path)))),aarch64),\
    arch-arm64,\
    arch-arm)

