# 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
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do
	desc "Build a new Mail prod release with AppStore configuration"
	lane :build_mail_prod do |options|
		match(
			app_identifier: ["de.tutao.tutanota", "de.tutao.tutanota.TutanotaShareExtension", "de.tutao.tutanota.TutanotaNotificationExtension"],
			type: "appstore",
			verbose: true,
			readonly: true,
			generate_apple_certs: false,
			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
		)

		clear_derived_data

		build_app(
			scheme: "tuta prod",
			output_directory: "releases",
			output_name: "tutanota-" + get_version_number(target: "tutanota", xcodeproj: "tutanota.xcodeproj"),
			include_symbols: true,
			verbose: true
		)
	end

   desc "Publish a Mail artifact to AppStore"
   lane :publish_mail_prod do |options|
        sh 'echo "Uploading mail artifact ' + options[:file] + '"'

        upload_to_app_store(
   			ipa: options[:file],
            skip_screenshots: true,
            submit_for_review: false,
            precheck_include_in_app_purchases: false,
            # must use force as long as we don't automatically create html previews
            force: true,
            api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
        )
   end

	desc "Build a new prod release for ad-hoc"
	lane :adhoc_prod do |options|
		match(
			app_identifier: ["de.tutao.tutanota", "de.tutao.tutanota.TutanotaShareExtension", "de.tutao.tutanota.TutanotaNotificationExtension"],
			type: "adhoc",
			verbose: true,
			readonly: true,
			generate_apple_certs: false,
			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
		)

		clear_derived_data

		ipa_name = "tutanota-" + get_version_number(target: "tutanota", xcodeproj: "tutanota.xcodeproj") + "-adhoc"
		build_app(
			scheme: "tuta prod",
			output_directory: "releases",
			output_name: ipa_name,
			export_options: {
				method: "ad-hoc" # if not specified export_method defaults to app-store
			},
			verbose: true
		)
	end


	desc "Push a new staging release to TestFlight"
	lane :build_mail_staging do
		match(
			app_identifier: ["de.tutao.tutanota.test", "de.tutao.tutanota.test.TutanotaShareExtension", "de.tutao.tutanota.test.TutanotaNotificationExtension"],
			type: "appstore",
			verbose: true,
			readonly: true,
			generate_apple_certs: false,
			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
		)

		clear_derived_data

		build_app(
			scheme: "tuta staging",
			output_directory: "releases",
			output_name: "tutanota-" + get_version_number(target: "tutanota", xcodeproj: "tutanota.xcodeproj") + "-test",
			verbose: true
		)
	end

   desc "Publish Mail staging to TestFlight"
   lane :publish_mail_staging do |options|
        sh 'echo "Uploading mail staging to TestFlight ' + options[:file] + '"'

		upload_to_testflight(
   			ipa: options[:file],
			app_identifier: "de.tutao.tutanota.test",
			skip_submission: true,
			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
		)
   end

	desc "Build a new staging release for ad-hoc"
	lane :adhoc_staging do
		match(
			app_identifier: ["de.tutao.tutanota.test", "de.tutao.tutanota.test.TutanotaShareExtension", "de.tutao.tutanota.test.TutanotaNotificationExtension"],
			type: "adhoc",
			verbose: true,
			readonly: true,
			generate_apple_certs: false,
			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
		)

		clear_derived_data

		build_app(
			scheme: "tuta staging",
			output_directory: "releases",
			output_name: "tutanota-" + get_version_number(target: "tutanota", xcodeproj: "tutanota.xcodeproj") + "-adhoc-test",
			export_options: {
				method: "ad-hoc" # if not specified export_method defaults to app-store
			},
			verbose: true
		)
	end

	desc "Run iOS mail test cases (Jenkins)"
	lane :test_tuta_app do
		# Create tutanota-3/build if it's not there because we try to copy it during build
		sh "mkdir -p ../../build"

		# Create tutanota-3/build-calendar-app if it's not there because we try to copy it during build
		sh "mkdir -p ../../build-calendar-app"

		sh 'xcodebuild -downloadPlatform iOS -buildVersion 18.2'
 		sh "if xcrun simctl list | grep iphone-15-ios-18-2; then echo 'Using existing simulator'; else xcrun simctl create iphone-15-ios-18-2 com.apple.CoreSimulator.SimDeviceType.iPhone-15 com.apple.CoreSimulator.SimRuntime.iOS-18-2; fi"

 		clear_derived_data

		# Test only one app because both link to the same test suite
		run_tests(
			scheme: "tuta debug",
			device: "iphone-15-ios-18-2"
		)
 	end

     desc "Run iOS shared framework test cases (Jenkins)"
     lane :test_tuta_shared_framework do
         # Create tutanota-3/build if it's not there because we try to copy it during build
         sh "mkdir -p ../../build"

         # Create tutanota-3/build-calendar-app if it's not there because we try to copy it during build
         sh "mkdir -p ../../build-calendar-app"

		sh 'xcodebuild -downloadPlatform iOS -buildVersion 18.2'
        sh "if xcrun simctl list | grep iphone-15-ios-18-2; then echo 'Using existing simulator'; else xcrun simctl create iphone-15-ios-18-2 com.apple.CoreSimulator.SimDeviceType.iPhone-15 com.apple.CoreSimulator.SimRuntime.iOS-18-2; fi"

        clear_derived_data

         run_tests(
			 scheme: "TutanotaSharedFramework",
			 device: "iphone-15-ios-18-2"
         )
      end

	desc "Run iOS test cases (Github actions version)"
	lane :test_github do
		run_tests(
				scheme: "tuta debug",
		)
		run_tests(
				scheme: "TutanotaSharedFramework",
		)
	end

 	desc "Renew prod adhoc cert"
 	lane :renew_adhoc_cert_prod do |options|
		match(
			app_identifier: ["de.tutao.tutanota", "de.tutao.tutanota.TutanotaShareExtension", "de.tutao.tutanota.TutanotaNotificationExtension"],
			type: "adhoc",
			verbose: true,
			readonly: false,
			generate_apple_certs: true,
			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
			api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
			platform: "ios",
			force: options[:force]
		)
 	end

	desc "Renew prod appstore cert"
	lane :renew_appstore_cert_prod do |options|
		match(
			app_identifier: ["de.tutao.tutanota", "de.tutao.tutanota.TutanotaShareExtension", "de.tutao.tutanota.TutanotaNotificationExtension"],
			type: "appstore",
			verbose: true,
			readonly: false,
			generate_apple_certs: true,
			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
			api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
			platform: "ios",
			force: options[:force]
		)
	end

	desc "Renew staging adhoc cert"
	lane :renew_adhoc_cert_staging do |options|
	   match(
		   app_identifier: ["de.tutao.tutanota.test", "de.tutao.tutanota.test.TutanotaShareExtension", "de.tutao.tutanota.test.TutanotaNotificationExtension"],
		   type: "adhoc",
		   verbose: true,
		   readonly: false,
		   generate_apple_certs: true,
		   keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
		   api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
		   platform: "ios",
		   force: options[:force]
	   )
	end

   desc "Renew staging appstore cert"
   lane :renew_appstore_cert_staging do |options|
	   match(
		   app_identifier: ["de.tutao.tutanota.test", "de.tutao.tutanota.test.TutanotaShareExtension", "de.tutao.tutanota.test.TutanotaNotificationExtension"],
		   type: "appstore",
		   verbose: true,
		   readonly: false,
		   generate_apple_certs: true,
		   keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
		   api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
		   platform: "ios",
		   force: options[:force]
	   )
   end

   desc "Run iOS test cases for Calendar app (Github actions version)"
   lane :test_calendar_github do
   		run_tests(
   				scheme: "calendar debug",
   		)
   		run_tests(
   				scheme: "TutanotaSharedFramework",
   		)
   end

   desc "Build a new staging Calendar release for ad-hoc"
   lane :calendar_adhoc_staging do
   		match(
   			app_identifier: ["de.tutao.calendar.test", "de.tutao.calendar.test.TutanotaShareExtension", "de.tutao.calendar.test.TutanotaNotificationExtension", "de.tutao.calendar.test.AgendaWidget", "de.tutao.calendar.test.WidgetConfigIntent"],
   			type: "adhoc",
   			verbose: true,
   			readonly: true,
   			generate_apple_certs: false,
   			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
   		)

   		clear_derived_data

   		build_app(
   			scheme: "calendar staging",
   			output_directory: "releases",
   			output_name: "calendar-" + get_version_number(target: "calendar", xcodeproj: "calendar.xcodeproj")  + "-adhoc-test",
   			export_options: {
   				method: "ad-hoc" # if not specified export_method defaults to app-store
   			},
   			verbose: true
   		)
   end

   desc "Build and push a new staging calendar release to TestFlight"
   lane :calendar_testflight_staging do
   		match(
   			app_identifier: ["de.tutao.calendar.test", "de.tutao.calendar.test.TutanotaShareExtension", "de.tutao.calendar.test.TutanotaNotificationExtension", "de.tutao.calendar.test.AgendaWidget", "de.tutao.calendar.test.WidgetConfigIntent"],
   			type: "appstore",
   			verbose: true,
   			readonly: true,
   			generate_apple_certs: false,
   			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
   		)

   		clear_derived_data

   		build_app(
   			scheme: "calendar staging",
   			output_directory: "releases",
   			output_name: "calendar-" + get_version_number(target: "calendar", xcodeproj: "calendar.xcodeproj")  + "-test",
   			verbose: true
   		)

   		upload_to_testflight(
   			app_identifier: "de.tutao.calendar.test",
   			skip_submission: true,
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
   		)
   end

   desc "Build a new Calendar prod release for ad-hoc"
   lane :calendar_adhoc_prod do |options|
   		match(
   			app_identifier: ["de.tutao.calendar", "de.tutao.calendar.TutanotaShareExtension", "de.tutao.calendar.TutanotaNotificationExtension", "de.tutao.calendar.AgendaWidget", "de.tutao.calendar.WidgetConfigIntent"],
   			type: "adhoc",
   			verbose: true,
   			readonly: true,
   			generate_apple_certs: false,
   			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
   		)

   		clear_derived_data

   		ipa_name = "calendar-" + get_version_number(target: "calendar", xcodeproj: "calendar.xcodeproj")  + "-adhoc"
   		build_app(
   			scheme: "calendar prod",
   			output_directory: "releases",
   			output_name: ipa_name,
   			export_options: {
   				method: "ad-hoc" # if not specified export_method defaults to app-store
   			},
   			verbose: true
   		)
   end

   desc "Build a new Calendar prod release with AppStore configuration"
   lane :build_calendar_prod do |options|
   		match(
   			app_identifier: ["de.tutao.calendar", "de.tutao.calendar.TutanotaShareExtension", "de.tutao.calendar.TutanotaNotificationExtension", "de.tutao.calendar.AgendaWidget", "de.tutao.calendar.WidgetConfigIntent"],
   			type: "appstore",
   			verbose: true,
   			readonly: true,
   			generate_apple_certs: false,
   			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"]
   		)

   		clear_derived_data

   		build_app(
   			scheme: "calendar prod",
   			output_directory: "releases",
   			output_name: "calendar-" + get_version_number(target: "calendar", xcodeproj: "calendar.xcodeproj") ,
   			include_symbols: true,
   			verbose: true
   		)
   end

   desc "Publish a calendar artifact to AppStore"
   lane :publish_calendar_prod do |options|
		sh 'echo "Uploading calendar artifact ' + options[:file] + '"'

   		upload_to_app_store(
   			ipa: options[:file],
   			skip_screenshots: true,
   			submit_for_review: false,
   			precheck_include_in_app_purchases: false,
   			# must use force as long as we don't automatically create html previews
   			force: true,
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
   			app_identifier: "de.tutao.calendar"
   		)
   end

   desc "Renew calendar prod adhoc cert"
   lane :renew_calendar_adhoc_cert_prod do |options|
   		match(
   			app_identifier: ["de.tutao.calendar", "de.tutao.calendar.TutanotaShareExtension", "de.tutao.calendar.TutanotaNotificationExtension", "de.tutao.calendar.AgendaWidget", "de.tutao.calendar.WidgetConfigIntent"],
   			type: "adhoc",
   			verbose: true,
   			readonly: false,
   			generate_apple_certs: true,
   			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
   			platform: "ios",
   			force: options[:force]
   		)
   end

   desc "Renew calendar prod appstore cert"
   lane :renew_calendar_appstore_cert_prod do |options|
   		match(
   			app_identifier: ["de.tutao.calendar", "de.tutao.calendar.TutanotaShareExtension", "de.tutao.calendar.TutanotaNotificationExtension", "de.tutao.calendar.AgendaWidget", "de.tutao.calendar.WidgetConfigIntent"],
   			type: "appstore",
   			verbose: true,
   			readonly: false,
   			generate_apple_certs: true,
   			keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
   			api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
   			platform: "ios",
   			force: options[:force]
   		)
   end

   desc "Renew calendar staging adhoc cert"
   lane :renew_calendar_adhoc_cert_staging do |options|
	   match(
		   app_identifier: ["de.tutao.calendar.test", "de.tutao.calendar.test.TutanotaShareExtension", "de.tutao.calendar.test.TutanotaNotificationExtension", "de.tutao.calendar.test.AgendaWidget", "de.tutao.calendar.test.WidgetConfigIntent"],
		   type: "adhoc",
		   verbose: true,
		   readonly: false,
		   generate_apple_certs: true,
		   keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
		   api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
		   platform: "ios",
		   force: options[:force]
	   )
   end

  desc "Renew calendar staging appstore cert"
  lane :renew_calendar_appstore_cert_staging do |options|
	   match(
		   app_identifier: ["de.tutao.calendar.test", "de.tutao.calendar.test.TutanotaShareExtension", "de.tutao.calendar.test.TutanotaNotificationExtension", "de.tutao.calendar.test.AgendaWidget", "de.tutao.calendar.test.WidgetConfigIntent"],
		   type: "appstore",
		   verbose: true,
		   readonly: false,
		   generate_apple_certs: true,
		   keychain_password: ENV["FASTLANE_KEYCHAIN_PASSWORD"],
		   api_key_path: ENV["API_KEY_JSON_FILE_PATH"],
		   platform: "ios",
		   force: options[:force]
	   )
  end
end
