/*
/*
 * Copyright (c) 2024 Augur AI, Inc.
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
 * If a copy of the MPL was not distributed with this file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 
 * Copyright (c) 2024 Augur AI, Inc.
 *
 * This file is licensed under the Augur AI Proprietary License.
 *
 * Attribution:
 * This work is based on code from https://github.com/hofstadter-io/hof, licensed under the Apache License 2.0.
 */

package yagu

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
)

func Shell(script, workdir string) (string, error) {
	cmd := exec.Command("sh", "-c", script)
	if workdir != "" {
		cmd.Dir = workdir
	}
	out, err := cmd.CombinedOutput()
	return string(out), err
}

func Bash(script, workdir string) (string, error) {
	cmd := exec.Command("bash", "-p", "-c", script)
	if workdir != "" {
		cmd.Dir = workdir
	}
	out, err := cmd.CombinedOutput()
	return string(out), err
}

func BashTmpScriptWithArgs(script string, args []string) (string, error) {

	// make sure we have our temp dir
	err := Mkdir(".hof")
	if err != nil {
		return "", err
	}

	// create temp file
	f, err := ioutil.TempFile(".hof", "tmp-jumps-*")
	if err != nil {
		return "", err
	}
	// and cleanup for sure
	defer os.Remove(f.Name())

	// write script
	_, err = fmt.Fprintln(f, script)
	if err != nil {
		return "", err
	}

	// close
	err = f.Close()
	if err != nil {
		return "", err
	}

	// make exec'd
	err = os.Chmod(f.Name(), 0755)
	if err != nil {
		return "", err
	}

	// prepare args to bash invocation as a "/path/to/scripts.sh"
	doargs := append([]string{f.Name()}, args...)
	dorun := strings.Join(doargs, " ")

	// run script and return output / status
	cmd := exec.Command("bash", "-p", "-c", dorun)
	out, err := cmd.CombinedOutput()
	return string(out), err
}

func Exec(args []string) (string, error) {
	command, rest := args[0], args[1:]
	cmd := exec.Command(command, rest...)
	out, err := cmd.CombinedOutput()
	return string(out), err
}
