// Copyright 2021 Chaos Mesh Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
	"fmt"
	"os"
	"path/filepath"
)

func SetRuntimeEnv() error {
	ex, err := os.Executable()
	if err != nil {
		return err
	}
	wd := filepath.Dir(ex)

	_, err = os.Stat(fmt.Sprintf("%s/tools", wd))
	if os.IsNotExist(err) {
		return err
	}

	bytemanHome := os.Getenv("BYTEMAN_HOME")
	if len(bytemanHome) == 0 {
		err = os.Setenv("BYTEMAN_HOME", fmt.Sprintf("%s/tools/byteman", wd))
		if err != nil {
			return err
		}
	}

	path := os.Getenv("PATH")
	err = os.Setenv("PATH", fmt.Sprintf("%s/tools:%s/bin:%s", wd, bytemanHome, path))
	if err != nil {
		return err
	}

	return nil
}
