package com.secspace.lib.common.utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class CmdUtil {
    public static final String TAG = "CmdUtil";

    public final static String CMD_SU = "su";
    public final static String CMD_SH = "sh";
    public final static String CMD_EXIT = "exit\n";
    public final static String CMD_LINE_END = "\n";

    public static class CmdResult {
        public int result = -1;
        public String errorMsg;
        public String successMsg;

        @Override
        public String toString() {
            return "CmdResult{" +
                    "result=" + result +
                    ", errorMsg='" + errorMsg + '\'' +
                    ", successMsg='" + successMsg + '\'' +
                    '}';
        }
    }


    public static CmdResult exec(String cmd, boolean isRoot) {
        String[] CMDs = {cmd};
        return exec(CMDs, isRoot);
    }


    public static CmdResult exec(String[] cmds, boolean isRoot) {
        CmdResult cmdResult = new CmdResult();
        if (cmds == null || cmds.length == 0) return null;
        Process process = null;
        DataOutputStream os = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;
        try {
            process = Runtime.getRuntime().exec(isRoot ? CMD_SU : CMD_SH);
            os = new DataOutputStream(process.getOutputStream());
            for (String cmd : cmds) {
                if (cmd != null) {
                    os.write(cmd.getBytes());
                    os.writeBytes(CMD_LINE_END);
                    os.flush();
                }
            }
            os.writeBytes(CMD_EXIT);
            os.flush();
            cmdResult.result = process.waitFor();

            successMsg = new StringBuilder();
            errorMsg = new StringBuilder();
            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String buffer;
            while ((buffer = successResult.readLine()) != null) successMsg.append(buffer);
            while ((buffer = errorResult.readLine()) != null) errorMsg.append(buffer);
            cmdResult.successMsg = successMsg.toString();
            cmdResult.errorMsg = errorMsg.toString();
            Log.i(TAG, "result: " + cmdResult.result + "  successMsg: " + cmdResult.successMsg + "  errorMsg: " + cmdResult.errorMsg);
        } catch (IOException e) {
            String errmsg = e.getMessage();
            if (errmsg != null) {
                Log.e(TAG, errmsg);
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            String errmsg = e.getMessage();
            if (errmsg != null) {
                Log.e(TAG, errmsg);
            } else {
                e.printStackTrace();
            }
        } finally {
            try {
                if (os != null) os.close();
                if (successResult != null) successResult.close();
                if (errorResult != null) errorResult.close();
            } catch (IOException e) {
                String errmsg = e.getMessage();
                if (errmsg != null) {
                    Log.e(TAG, errmsg);
                } else {
                    e.printStackTrace();
                }
            }
            if (process != null) process.destroy();
        }
        return cmdResult;
    }
}
