//
//  main.swift
//  Swift Command Line
//
//  Created by User on 10/4/22.
//  Thanks to ...
//  https://stackoverflow.com/questions/27046728/how-do-you-use-posix-spawn-to-replace-the-deprecated-system-to-launch-opendiff

import Foundation

func system(_ command: String) {
    var args = command.components(separatedBy: " ")
    let path = args.first
    args.remove(at: 0)

    do {
        let task = try Process()
        task.launchPath = path
        task.arguments = args
        task.launch()
        task.waitUntilExit()
    }
    catch {
        // Couldn't create audio player object, log the error
    }
}

func systemtwo(_ commandtwo: String) {
    var argstwo = commandtwo.components(separatedBy: " ")
    let pathtwo = argstwo.first
    argstwo.remove(at: 0)

    do {
        let tasktwo = try Process()
        tasktwo.launchPath = pathtwo
        tasktwo.arguments = argstwo
        tasktwo.launch()
        // tasktwo.waitUntilExit()
    }
    catch {
        // Couldn't create audio player object, log the error
    }
}

var msg = ""
var phpidea = "/usr/bin/php /Applications/MAMP/htdocs/find_executable_and_go.php WhYfOrArTtHoU"
var i = 2

if CommandLine.arguments.count <= 1
{
    print("Hello, World!")
}
else
{
    msg = CommandLine.arguments[1]
    while i < CommandLine.arguments.count
    {
        msg += " " + CommandLine.arguments[i]
        i += 1
    }
    // phpidea = "/usr/bin/curl " + "\"HTTP://localhost:8888/find_executable_and_go.php?inproject=&argvs=" + msg.replacingOccurrences(of: " ", with: "+") + "\""
    print(msg)
}

systemtwo(phpidea)
system("/usr/bin/python /Applications/MAMP/htdocs/tkinter_filebrowser_test.py")


