import os
import sys
import subprocess
import shutil
import re
import getpass
import glob

def has_build(module_name):
    build_module = ['openssl']
    if module_name in build_module:
        return True
    else:
        return False

def build(module_name, build_utils, src_dir, bin_dir, install_dir, job_n):
    # 设置环境变量
    os.environ['ARCH'] = 'arm'
    root_dir = os.environ["PRODUCT_ROOT_DIR"]
    os.environ['CROSS_COMPILE'] = f'{root_dir}/buildtools/toolchains/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-'
    if module_name == 'openssl':
        build_utils.copy_files_ignoring_hidden(src_dir, bin_dir)
        build_utils.exec_command(['./config', 'no-asm', f'--prefix={install_dir}', f'--cross-compile-prefix={root_dir}/buildtools/toolchains/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-', f'--openssldir={install_dir}'], "config", bin_dir, exit=False)
        build_utils.exec_command(['sed', '-i', 's/-m64//g', f'{bin_dir}/Makefile'], "sed", bin_dir, exit=True)
        return build_utils.exec_command(['make', f'--directory={bin_dir}', f'--jobs={job_n}'], "make", bin_dir, exit=False)
    elif module_name == 'sx1302':
        os.environ['ARCH'] = 'arm'
        os.environ['CROSS_COMPILE'] = f'{root_dir}/buildtools/toolchains/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-'
        # os.environ["DESTDIR"] = f'{bin_dir}/Lora/sx1302_hal'
        os.environ["OBJDIR"] = f'{bin_dir}/Lora/sx1302_hal'
        # 调用make命令，指定工作目录
        # 注意：subprocess.run在Python 3.5及以上版本中可用
        # 如果你使用的是旧版本的Python，可能需要使用subprocess.call或其他方法
        os.makedirs(f'{bin_dir}/Lora/sx1302_hal/install', exist_ok=True)
        build_utils.replace_value_in_file(f'{root_dir}/Lora/sx1302_hal/target.cfg', "TARGET_DIR", f'{bin_dir}/Lora/sx1302_hal/install')
        build_utils.replace_value_in_file(f'{root_dir}/Lora/sx1302_hal/target.cfg', "TARGET_USR", getpass.getuser())

        return subprocess.check_call(['make', f'--directory={root_dir}/Lora/sx1302_hal', f'--jobs={job_n}'])
    # subprocess.check_call(['make', f'--directory={root_dir}/system/quectel-cm', f'--jobs={job_n}'])


def build_install(module_name, build_utils, bin_dir, install_dir, job_n):
    if module_name == 'openssl':
        build_utils.exec_command(['make', 'install', f'--directory={bin_dir}', f'--jobs={job_n}'], "make", bin_dir, exit=False)
    elif module_name == 'sx1302':
    # install test_loragw_*
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/libloragw/test_loragw_*', f'{install_dir}/Lora/sx1302_hal/')
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/tools/reset_lgw.sh', f'{install_dir}/Lora/sx1302_hal/', True)

        # install packet_forwarder
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/packet_forwarder/lora_pkt_fwd', f'{install_dir}/Lora/sx1302_hal/')
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/packet_forwarder/global_conf.json.sx1250.*', f'{install_dir}/Lora/sx1302_hal/', True)
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/packet_forwarder/global_config.json.sx1255.*', f'{install_dir}/Lora/sx1302_hal/', True)
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/packet_forwarder/global_config.json.sx1257.*', f'{install_dir}/Lora/sx1302_hal/', True)

        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/util_boot/boot', f'{install_dir}/Lora/sx1302_hal/')
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/util_chip_id/chip_id', f'{install_dir}/Lora/sx1302_hal/')
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/util_net_downlink/net_downlink', f'{install_dir}/Lora/sx1302_hal/')
        build_utils.move_matching_files(f'{root_dir}/Lora/sx1302_hal/util_spectral_scan/spectral_scan', f'{install_dir}/Lora/sx1302_hal/')
        return True
