//
//  AppLovinBuildPostProcessor.cs
//  AppLovin MAX Unity Plugin
//
//  Created by Santosh Bagadi on 10/30/19.
//  Copyright © 2019 AppLovin. All rights reserved.
//
#if UNITY_IOS || UNITY_IPHONE
using System.Diagnostics;
using System.IO;
namespace AppLovinMax.Scripts.IntegrationManager.Editor
{
    /// 
    /// A helper class to run command line tools.
    ///
    /// TODO: Currently only supports shell (Linux). Add support for Windows machines.
    /// 
    public static class AppLovinCommandLine
    {
        /// 
        /// Result obtained by running a command line command.
        /// 
        public class Result
        {
            /// 
            /// Standard output stream from command line.
            /// 
            public string StandardOutput;
            /// 
            /// Standard error stream from command line. 
            /// 
            public string StandardError;
            /// 
            /// Exit code returned from command line.
            /// 
            public int ExitCode;
            /// 
            /// The description of the result that can be used for error logging.
            /// 
            public string Message;
        }
        /// 
        /// Runs a command line tool using the provided  and .
        /// 
        /// The tool path to run
        /// The arguments to be passed to the command line tool
        /// The directory from which to run this command.
        /// 
        public static Result Run(string toolPath, string arguments, string workingDirectory)
        {
            var stdoutFileName = Path.GetTempFileName();
            var stderrFileName = Path.GetTempFileName();
            var process = new Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.RedirectStandardInput = false;
            process.StartInfo.RedirectStandardOutput = false;
            process.StartInfo.RedirectStandardError = false;
            process.StartInfo.WorkingDirectory = workingDirectory;
            process.StartInfo.FileName = "bash";
            process.StartInfo.Arguments = string.Format("-l -c '\"{0}\" {1} 1> {2} 2> {3}'", toolPath, arguments, stdoutFileName, stderrFileName);
            process.Start();
            process.WaitForExit();
            var stdout = File.ReadAllText(stdoutFileName);
            var stderr = File.ReadAllText(stderrFileName);
            File.Delete(stdoutFileName);
            File.Delete(stderrFileName);
            var result = new Result();
            result.StandardOutput = stdout;
            result.StandardError = stderr;
            result.ExitCode = process.ExitCode;
            var messagePrefix = result.ExitCode == 0 ? "Command executed successfully" : "Failed to run command";
            result.Message = string.Format("{0}: '{1} {2}'\nstdout: {3}\nstderr: {4}\nExit code: {5}", messagePrefix, toolPath, arguments, stdout, stderr, process.ExitCode);
            return result;
        }
    }
}
#endif