# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#
require 'pathname'
require 'tmpdir'
import "Subroutings"

before_all do |lane, options|
  config = ENV["CONFIGURATION"]

  ENV["BUILD_PATH"]   = "#{ENV['REPO_ROOT']}/#{ENV['BUILD_DIR']}/#{config}"

  app_store_connect_api_key(
    key_id: ENV["bamboo_appStoreConnectApiKeyId"],
    issuer_id: ENV["bamboo_appStoreConnectApiKeyIssuerId"],
    key_content: ENV["bamboo_appStoreConnectApiKeyBase64Password"],
    is_key_content_base64: true,
  )
end


desc "Installs or updates certificates and provisioning profiles, which need for build product (native distribution)"
lane :certs do |options|

  app_id = "com.adguard.trusttunnel.cli"

  keychain_path = nil

  if ENV["KEYCHAIN_PATH_LOCAL"] == 'true'

    keychain_path = "#{ENV['BUILD_PATH']}/certs/#{ENV['MATCH_KEYCHAIN_NAME']}"

    create_local_keychain(keychain_path)

    UI.success "Keychain path: #{keychain_path}"
  end

  match(
    step_name: "Sync Developer id identity and provisioning profiles",
    app_identifier: [
        app_id,
    ],
    type: "developer_id",

    keychain_name: keychain_path,

    readonly: "true",
    force: "false",
    force_for_new_devices: "false",
    verbose: if options[:verbose].nil?; "false"; else options[:verbose]; end,

    git_branch: "standalone",
    clone_branch_directly: "true",
    shallow_clone: "true",
    platform: "macos",
    fail_on_name_taken: "true",
    skip_provisioning_profiles: "true",
  )

end

desc "Remove local keychain, which contains certificates"
lane :remove_certs do |options|

    step_name = "Remove local keychain, which contains certificates"
    keychain_path = "#{ENV['BUILD_PATH']}/certs/#{ENV['MATCH_KEYCHAIN_NAME']}"

  if !File.exist?(keychain_path)
    Actions.execute_action(step_name) do
      UI.success "No local keychain"
    end
    next
  end

  delete_keychain(
    keychain_path: keychain_path,
    step_name: step_name
  )
end

desc "Notarize bundle using default credentials"
desc "Required options:"
desc "  - bundle: STRING Path to bundle, must be defined relativelly to BUILD_DIR"
desc "  - id: STRING Bundle id, used for notary service"
lane :notari do |options|

  UI.user_error!("Missing argument: 'id:<BUNDLE_ID>'") if options[:id].nil?
  UI.user_error!("Missing argument: 'bundle:<BUNDLE_PATH_RELATIVE_TO_BUILD_DIR>'") if options[:bundle].nil?

  app_store_connect_api_key(
    key_id: ENV["bamboo_appStoreConnectApiKeyId"],
    issuer_id: ENV["bamboo_appStoreConnectApiKeyIssuerId"],
    key_content: ENV["bamboo_appStoreConnectApiKeyBase64Password"],
    is_key_content_base64: true,
  )

  bundle_path = "#{options[:bundle]}"
  bundle_id = options[:id]

  Dir.mktmpdir do |tempDir|
    notari_path = "#{tempDir}/to_notarize.zip"

    compress_bundle(bundle_path, notari_path)

    notarize(
      step_name: "Notarizing bundle",
      package: notari_path,
      use_notarytool: "true",
      bundle_id: bundle_id,
      skip_stapling: "true",
      print_log: "true",
      verbose: "false" # "true" is broken in Fastlane 2.227.x
    )

# Executables cannot be stapled, macOS will check online
#    staple_bundle(bundle_path)
  end 
end

ENV["FASTLANE_PROC"] = "true"
