require 'fileutils'

platform :ios, '15.0'
install! 'cocoapods', :warn_for_unused_master_specs_repo => false

target 'CentovaPanel' do
  use_frameworks!

  # Networking (replaces vendored AFNetworking 2.x)
  pod 'AFNetworking', '~> 4.0.1'

  # HUD (replaces vendored MBProgressHUD ~0.9)
  pod 'MBProgressHUD', '~> 1.2.0'

  # Ads and consent
  pod 'Google-Mobile-Ads-SDK'

end

post_install do |installer|
  privacy_manifests = {
    'AFNetworking' => File.join(__dir__, 'PrivacyManifests', 'AFNetworking', 'PrivacyInfo.xcprivacy'),
    'MBProgressHUD' => File.join(__dir__, 'PrivacyManifests', 'MBProgressHUD', 'PrivacyInfo.xcprivacy')
  }

  installer.pods_project.targets.each do |t|
    t.build_configurations.each do |c|
      c.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.0'
      c.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
    end

    # These source pods predate Apple's privacy-manifest packaging rules. Copy
    # the audited manifest into generated support files and add it to the pod
    # framework's Resources phase after every `pod install`.
    manifest_source = privacy_manifests[t.name]
    next unless manifest_source

    support_dir = File.join(installer.sandbox.root, 'Target Support Files', t.name)
    manifest_destination = File.join(support_dir, 'PrivacyInfo.xcprivacy')
    FileUtils.mkdir_p(support_dir)
    FileUtils.cp(manifest_source, manifest_destination)

    project_path = File.join('Target Support Files', t.name, 'PrivacyInfo.xcprivacy')
    manifest_reference = installer.pods_project.files.find { |file| file.path == project_path }
    manifest_reference ||= installer.pods_project.main_group.new_file(project_path)
    unless t.resources_build_phase.files_references.include?(manifest_reference)
      t.resources_build_phase.add_file_reference(manifest_reference)
    end
  end
  # AFNetworking 4.0.1 uses the now-private <netinet6/in6.h>; comment it out (types come from <netinet/in.h>).
  Dir.glob(File.join(installer.sandbox.root, 'AFNetworking', '**', '*.m')).each do |f|
    src = File.read(f)
    patched = src.gsub(/^(\s*#\s*(?:import|include)\s*<netinet6\/in6\.h>)/, '//\1  /* patched: private header on modern SDK */')
    File.write(f, patched) if patched != src
  end
end
