Import('env', 'arch')

templates = Glob('#rednose/templates/*')

sympy_helpers = "#rednose/helpers/sympy_helpers.py"
ekf_sym = "#rednose/helpers/ekf_sym.py"

to_build = {
    'live': ('live_kf.py', 'generated'),
    'car': ('car_kf.py', 'generated'),
}

if arch != "aarch64":
    to_build.update({
        'gnss': ('gnss_kf.py', 'generated'),
        'loc_4': ('loc_kf.py', 'generated'),
        'pos_computer_4': ('#rednose/helpers/lst_sq_computer.py', 'generated'),
        'pos_computer_5': ('#rednose/helpers/lst_sq_computer.py', 'generated'),
        'feature_handler_5': ('#rednose/helpers/feature_handler.py', 'generated'),
        'lane': ('#xx/pipeline/lib/ekf/lane_kf.py', 'generated'),
    })

found = {}

for target, (command, generated_folder) in to_build.items():
    if File(command).exists():
        found[target] = (command, generated_folder)

for target, (command, generated_folder) in found.items():
    target_files = File([f'{generated_folder}/{target}.cpp', f'{generated_folder}/{target}.h'])
    command_file = File(command)

    env.Command(target_files,
                [templates, command_file, sympy_helpers, ekf_sym],
                command_file.get_abspath() + " " + target + " " + Dir(generated_folder).get_abspath())

    env.SharedLibrary(f'{generated_folder}/' + target, target_files[0])
