# Resolve react_native_pods.rb with node to allow for hoisting.
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

platform :ios, '15.1'
workspace 'jitsi-meet'

ENV['RCT_NEW_ARCH_ENABLED'] = '1'
ENV['RCT_HERMES_V1_ENABLED'] = '0'

# Build RN deps and Core from source so we don't depend on the prebuilt RN xcframeworks.
ENV['RCT_USE_RN_DEP'] = '0'
ENV['RCT_USE_PREBUILT_RNCORE'] = '0'

# hermes-engine bakes an absolute path into HERMES_CLI_PATH, which makes the
# Podfile.lock SPEC CHECKSUM differ on every machine. Rewrite it to a relative
# path so the lockfile is portable (e.g. `pod install --deployment` in CI).
module HermesCliPathRelativizer
  def store_attribute(name, value, platform_name = nil)
    if name == :user_target_xcconfig &&
       value.is_a?(Hash) &&
       value['HERMES_CLI_PATH'] &&
       self.name.to_s.start_with?('hermes-engine')
      value = value.merge(
        'HERMES_CLI_PATH' => '${REACT_NATIVE_PATH}/../hermes-compiler/hermesc/osx-bin/hermesc'
      )
    end
    super
  end
end
Pod::Specification.prepend(HermesCliPathRelativizer)

install! 'cocoapods', :deterministic_uuids => false

def cocoa_utilities
    pod 'CocoaLumberjack', '3.7.4'
end

# Giphy is excluded from iOS autolinking to keep it out of the lite SDK, 
# but the full SDK needs its TurboModule spec 
# and we re-add it to autolinking.json so react native codegen generates it.
def include_giphy_in_codegen!
  require 'json'
  autolinking_json = File.join(
    Pod::Config.instance.installation_root.to_s,
    'build/generated/autolinking/autolinking.json'
  )
  return unless File.exist?(autolinking_json)

  json = JSON.parse(File.read(autolinking_json))
  giphy_root = File.expand_path('../node_modules/@giphy/react-native-sdk', __dir__)
  json['dependencies'] ||= {}
  json['dependencies']['@giphy/react-native-sdk'] = {
    'name' => '@giphy/react-native-sdk',
    'root' => giphy_root,
    'platforms' => {
      'ios' => {
        'podspecPath' => File.join(giphy_root, 'giphy-react-native-sdk.podspec'),
        'scriptPhases' => [],
        'configurations' => []
      }
    }
  }
  File.write(autolinking_json, JSON.pretty_generate(json))
end

target 'JitsiMeet' do
  project 'app/app.xcodeproj'

  pod 'Firebase/Analytics', '~> 8.0'
  pod 'Firebase/Crashlytics', '~> 8.0'
end

target 'JitsiMeetSDK' do
  project 'sdk/sdk.xcodeproj'

  # React Native and its dependencies
  #

  config = use_native_modules!
  include_giphy_in_codegen!
  use_react_native!(
    :path => config[:reactNativePath],
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/.."
  )

  # We use auto linking, but some dependencies are excluded from the lite build
  # (see react-native.config.js) so we add them here.

  pod 'giphy-react-native-sdk', :path => '../node_modules/@giphy/react-native-sdk'
  pod 'RNCalendarEvents', :path => '../node_modules/react-native-calendar-events'
  pod 'RNGoogleSignin', :path => '../node_modules/@react-native-google-signin/google-signin'

  # Native pod dependencies
  #

  cocoa_utilities
  pod 'ObjectiveDropboxOfficial', '6.2.3'
end

target 'JitsiMeetSDKLite' do
    project 'sdk/sdk.xcodeproj'

    # This is a more lightweight target, which removes some functionality.
    # Check the react-native.config.js for the list of excluded packages.

    # React Native and its dependencies
    #

    config = use_native_modules!
    use_react_native!(
      :path => config[:reactNativePath],
      # An absolute path to your application root.
      :app_path => "#{Pod::Config.instance.installation_root}/.."
    )

    # Native pod dependencies
    #

    cocoa_utilities
end

post_install do |installer|

  react_native_post_install(
    installer,
    use_native_modules![:reactNativePath],
    :mac_catalyst_enabled => false,
    # :ccache_enabled => true
  )
  installer.pods_project.targets.each do |target|
    # https://github.com/CocoaPods/CocoaPods/issues/11402
    if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
      target.build_configurations.each do |config|
        config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
      end
    end

    target.build_configurations.each do |config|
      config.build_settings['SUPPORTS_MACCATALYST'] = 'NO'
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.1'
      config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -no-verify-emitted-module-interface'
    end
  end

  # Patch SocketRocket to support TLS 1.3
  %x(patch Pods/SocketRocket/SocketRocket/SRSecurityPolicy.m -N < patches/ws-tls13.diff)

  # GiphyUISDK.xcframework already embeds libwebp; Giphy's podspec also
  # declares libwebp as a dependency, which makes CocoaPods statically link
  # libwebp into JitsiMeetSDK as well. The two copies trigger a duplicate
  # `PodsDummy_libwebp` warning at runtime. Drop the redundant link.
  ['Pods-JitsiMeetSDK', 'Pods-JitsiMeet'].each do |aggregate_name|
    Dir.glob("Pods/Target Support Files/#{aggregate_name}/*.xcconfig").each do |path|
      contents = File.read(path)
      patched = contents.gsub(/ -l"?libwebp"?/, '')
      File.write(path, patched) if patched != contents
    end
  end

end
