package api.common.util;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

@Slf4j
public class LinuxUtil {

    @SneakyThrows
    public static void kill(String command) {
        execute("echo kill $(ps -ef | grep '" + command + "' | awk '{ print $2 }')");
    }

    @SneakyThrows
    public static String execute(String command) {
        String result;
        log.info("开始执行 linux 命令：" + command);
        Runtime run = Runtime.getRuntime();
        Process process = run.exec(command);
        InputStream in = new BufferedInputStream(process.getInputStream());
        StringBuilder out = new StringBuilder();
        byte[] b = new byte[8192];
        for (int n; (n = in.read(b)) != -1; ) {
            out.append(new String(b, 0, n));
        }
        in.close();
        process.destroy();
        result = out.toString();
        log.info("执行结果为：" + result);
        return result;
    }

    /**
     * 指定工作目录执行目录
     *
     * @param command   命令
     * @param directory 工作目录
     * @return 执行结果
     */
    @SneakyThrows
    public static String execute(String command, String directory) {

        String result;
        log.info("开始执行 linux 命令：" + command);
        Runtime run = Runtime.getRuntime();
        Process process = run.exec(command, null, new File(directory));
        InputStream in = new BufferedInputStream(process.getInputStream());
        StringBuilder out = new StringBuilder();
        byte[] b = new byte[8192];
        for (int n; (n = in.read(b)) != -1; ) {
            out.append(new String(b, 0, n));
        }
        in.close();
        process.destroy();
        result = out.toString();
        log.info("执行结果为：" + result);
        return result;
    }

    public static void execute2(String cmd1, String cmd2) throws IOException, InterruptedException {
        Runtime run = Runtime.getRuntime();
        log.info("开始执行 linux 命令：" + cmd1);
        Process process1 = run.exec(cmd1);
        process1.waitFor();
        log.info("开始执行 linux 命令：" + cmd2);
        Process process2 = run.exec(cmd2);
        process2.waitFor();
        process1.destroy();
        process2.destroy();
        log.info("linux 命令执行完成。");
    }


    public static String run(String command) throws IOException {
        Scanner input = null;
        StringBuilder result = new StringBuilder();
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            try {
                //等待命令执行完成
                process.waitFor(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            InputStream is = process.getInputStream();
            input = new Scanner(is);
            while (input.hasNextLine()) {
                result.append(input.nextLine()).append("\n");
            }
            result.insert(0, command + "\n"); //加上命令本身，打印出来
        } finally {
            if (input != null) {
                input.close();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return result.toString();
    }

    public static String run(String[] command) throws IOException {
        Scanner input = null;
        StringBuilder result = new StringBuilder();
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            try {
                //等待命令执行完成
                process.waitFor(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            InputStream is = process.getInputStream();
            input = new Scanner(is);
            while (input.hasNextLine()) {
                result.append(input.nextLine()).append("\n");
            }
            result.insert(0, Arrays.toString(command) + "\n"); //加上命令本身，打印出来
        } finally {
            if (input != null) {
                input.close();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return result.toString();
    }


}
