package com.njcn.advance.utils;

import cn.hutool.core.io.resource.ClassPathResource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.net.URLDecoder;

/**
 * pqs
 *
 * @author cdf
 * @date 2023/6/20
 */
@Slf4j
public class JnaCallDllOrSo {
    private String path = "";
    private String nameDll;

    public static String jarPath = "";

    public JnaCallDllOrSo(String name) {
        super();
        String suffix = ".dll";
        try {
            String os = System.getProperty("os.name");
            // windows操作系统为1 否则为0
            int beginIndex = os != null && os.startsWith("Windows") ? 1 : 0;
            if (beginIndex == 0) {
                //linux操作系统
                this.nameDll = "lib" + name + "_dll";
                suffix = ".so";
            } else {
                this.nameDll = name;
            }

            String tem = "/usr/local/dllFile/"+this.nameDll.concat(suffix);
            File dockerFile = new File(tem);
            if(!dockerFile.exists()){
                boolean f = dockerFile.getParentFile().mkdirs();
                boolean d = dockerFile.createNewFile();
                System.out.println("文件夹创建："+f);
                System.out.println("文件创建："+d);

                try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(this.nameDll.concat(suffix))) {
                    try (FileOutputStream outputStream = new FileOutputStream(dockerFile)) {
                        byte[] buffer = new byte[1024];
                        int bytesRead;
                        while ((bytesRead = inputStream.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
            this.path = dockerFile.getAbsolutePath();
        } catch (Exception e) {
            log.error(e.getMessage());
        }


    }


    public String getStrpath() {
        return this.path;
    }

    public void setPath() {

    }
}
