fastlane_version "1.106.0"

default_platform :ios

actions_path '../Pods/Apalon_FastlaneActions/'

def profiles(profileType)
    return {
        "com.apalonapps.stories" => "match #{profileType} com.apalonapps.stories"
    }
end

def buildName(scheme, configuration, exportMethod)
    build_time = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
    version = get_version_number(xcodeproj:"instaStories.xcodeproj", target: scheme)
    ci_build_number_postfix = if is_ci? then "_(# #{ENV['BUILD_NUMBER'] || '_'})" else "" end
    return "#{scheme}_#{version}_#{configuration}_#{exportMethod}_#{build_time}_#{ci_build_number_postfix}"
end

def build(configuration, exportMethod, profileType, crashlytics)

    archives_folder = "./archives"
    scheme = "instaStories"

    apn_clean_archive(path: archives_folder)

    plistPath = "./instaStories/Info.plist"

    output_name = buildName(scheme, configuration, exportMethod)

    app_name = get_info_plist_value(path: plistPath, key: "CFBundleDisplayName")
    build_version = get_info_plist_value(path: plistPath, key: "CFBundleShortVersionString")
    bundle_id = get_info_plist_value(path: plistPath, key: "CFBundleIdentifier")
    ENV["APP_NAME"] = app_name
    ENV["APP_VERSION"] = build_version
    ENV["APP_BUNDLE_ID"] = bundle_id

#    filestring = "APN_BUILD_VERSION=#{build_version}\n" + "APN_BUILD_NUMBER=#{build_number}\n" + "APN_BUILD_NAME=#{output_file}\n" + "APN_BUILD_XCODE_SCHEME=#{xcode_scheme_name}\n" + "APN_BUILD_XCODE_CONFIGURATION=#{configuration}\n" + "APN_BUILD_PROVISIONING_PROFILES_TYPE=#{export}\n"
#    File.open("../propsfile_wlf", "w") { |f| f.write(filestring) }

    gym(
        scheme: scheme,
        configuration: configuration,
        export_method: exportMethod,
        output_name: output_name,
        output_directory: archives_folder,
        xcargs: "ARCHIVE=YES",
        clean: true,
        include_bitcode: false,
        export_options: { provisioningProfiles: profiles(profileType) })

        ipa_path = "./archives/" + output_name + ".ipa"

        if crashlytics
            crashlytics({
                        emails: "e@apalon.com",
                        api_token: "d58a87e4592d2f634666dc368af82d259c5d1367",
                        build_secret: "be31602c42ed88e6ddd7a778c97832801ac223b1b3296b557a8b8867152b302d",
                        ipa_path: ipa_path,
                        notifications: true
                        })
        end

        apn_sea_upload(libraryname: scheme)
end

lane :adhoc do
    build("AdHoc", "ad-hoc", "AdHoc", false)
    apn_save_ci_variables()
end

lane :distrAdhoc do
    build("PreRelease", "ad-hoc", "AdHoc", false)
    apn_save_ci_variables()
end

lane :release do
    build("Distr", "app-store", "AppStore", false)
    apn_validate_ipa()
    apn_upload_ipa()
    apn_save_ci_variables()
end

lane :update_localisations do
    lokalise(api_token:"e6ccaa4ae708b84d780ba7cca97a0b304e61edba",
             project_identifier:"468181325ceff630e0cf89.39807146",
             destination:"./instaStories/Resources/Localisations",
             clean_destination:true,
             include_comments:false,
             use_original:true)
end

lane :Update_Profiles do
    ENV["MATCH_PASSWORD"] = "LuM2FkkCVV9YjxPCJuue"
    ids = [ "com.apalonapps.stories"]
    types = ["development", "adhoc", "appstore"]
    git = "git@git.herewetest.com:alexandr.tatarinchik/apalon-code-signing.git"
    for type in types do
        match(type: type, app_identifier: ids, readonly: true, git_url: git, clone_branch_directly:true, shallow_clone: true)
    end
    end

    before_all do

    end

      lane :testLocalizations do |options|

      test_names = options[:TEST_NAMES]
      test_names = test_names.split(",")
      do_frame = options[:DO_FRAME]

      tests = Array.new()
      test_prefix = "-only-testing:instaStoriesUITests/LocalizationUITests/"

      for test_name in test_names
          tests.push(test_prefix + test_name)
      end

      # test_name = options[:TEST_NAME]

      scan(
           scheme: "instaStories",
           derived_data_path: "my_derived_data",
           app_identifier: "com.apalonapps.stories",
           only_testing: "instaStoriesUITests/LocalizationUITests/testEmpty",
           build_for_testing: true,
           clean: true,
           devices: options[:devices]
           )
           capture_ios_screenshots(
                                   scheme: "instaStories",
                                   derived_data_path: "my_derived_data",
                                   app_identifier: "com.apalonapps.stories",
                                   test_without_building: true,
                                   # xcargs:["-only-testing:instaStoriesUITests/LocalizationUITests/#{test_name}"],
                                   clear_previous_screenshots: true,
                                   devices: options[:devices],
                                   languages: options[:languages],
                                   xcargs:tests
                                   # erase_simulator: true
                                   )
                                   frameit(white: true) if do_frame == true
  end
