#!/usr/bin/env -S PYTHONPATH=../../../../tools/extract-utils python3
#
# SPDX-FileCopyrightText: 2024 The LineageOS Project
# SPDX-License-Identifier: Apache-2.0
#

from extract_utils.extract import extract_fns_user_type
from extract_utils.extract_pixel import (
    extract_pixel_factory_image,
    extract_pixel_firmware,
    pixel_factory_image_regex,
    pixel_firmware_regex,
)
from extract_utils.file import FileArgs, FileList
from extract_utils.fixups_blob import (
    blob_fixup,
    blob_fixups_user_type,
)
from extract_utils.fixups_lib import (
    lib_fixups,
    lib_fixups_user_type,
)
from extract_utils.main import (
    ExtractUtils,
    ExtractUtilsModule,
)

namespace_imports = [
    'device/google/caimito',
    'hardware/google/av',
    'hardware/google/interfaces',
    'hardware/google/pixel',
]


def lib_fixup_vendor_suffix(lib: str, partition: str, *args, **kwargs):
    return f'{lib}_{partition}' if partition == 'vendor' else None


lib_fixups: lib_fixups_user_type = {
    **lib_fixups,
    (
        'com.google.edgetpu_app_service-V4-ndk',
        'com.google.edgetpu_vendor_service-V2-ndk',
        'vendor.google.whitechapel.audio.audioext@4.0',
        'vendor.google.whitechapel.audio.extension-V5-ndk',
    ): lib_fixup_vendor_suffix,
}

blob_fixups: blob_fixups_user_type = {
    'product/etc/felica/common.cfg': blob_fixup()
        .patch_file('osaifu-keitai.patch'),
    'vendor/etc/init/init.modem_logging_control.rc': blob_fixup()
        .regex_replace(' && property:ro.debuggable=0', ''),
    'vendor/lib64/libspeechenhancer.so': blob_fixup()
        .clear_symbol_version('AHardwareBuffer_allocate')
        .clear_symbol_version('AHardwareBuffer_describe')
        .clear_symbol_version('AHardwareBuffer_lock')
        .clear_symbol_version('AHardwareBuffer_release')
        .clear_symbol_version('AHardwareBuffer_unlock'),
}  # fmt: skip

extract_fns: extract_fns_user_type = {
    pixel_factory_image_regex: extract_pixel_factory_image,
    pixel_firmware_regex: extract_pixel_firmware,
}

module = ExtractUtilsModule(
    'komodo',
    'google',
    device_rel_path='device/google/caimito/komodo',
    blob_fixups=blob_fixups,
    lib_fixups=lib_fixups,
    namespace_imports=namespace_imports,
    add_generated_carriersettings_file=True,
    add_firmware_proprietary_file=True,
    extract_fns=extract_fns,
)


def fix_vendor_file_list(file_list: FileList):
    module_suffix_file_paths = [
        'vendor/lib64/com.google.edgetpu_app_service-V4-ndk.so',
        'vendor/lib64/com.google.edgetpu_vendor_service-V2-ndk.so',
        'vendor/lib64/vendor.google.whitechapel.audio.audioext@4.0.so',
        'vendor/lib64/vendor.google.whitechapel.audio.extension-V5-ndk.so',
    ]

    for file_path in module_suffix_file_paths:
        file_list.get_file(file_path).set_arg(FileArgs.MODULE_SUFFIX, '_vendor')


module.add_generated_proprietary_file(
    'proprietary-files-vendor.txt',
    partition='vendor',
    skip_file_list_name='skip-files-vendor.txt',
    fix_file_list=fix_vendor_file_list,
)

if __name__ == '__main__':
    utils = ExtractUtils.device(module)
    utils.run()
