package dotor

// |@@| C

import (
	"context"
	"gardening/src/lib"
	"gardening/src/lib/config"
	"gardening/src/lib/filesystem"
	"gardening/src/lib/operating_system"
	"os"
	"strings"
)

type SupportDotor struct {
	Config                   *config.Config
	OperatingSystemInterface operating_system.OperatingSystemInterface
	Filesystem               *filesystem.Filesystem
}

func (this *SupportDotor) Act(ctx context.Context) {
	envDir := this.Config.GetRootDir() + "/config/envs/env.dot"
	s, err := os.ReadFile(envDir)
	lib.Poe(err)
	lines := strings.Split(string(s), "\n")
	for _, line := range lines {
		if line == "" {
			continue
		}
		key, value, found := strings.Cut(line, "=")
		if !found {
			lib.Panic("Invalid dot file for line " + line)
		}
		err := this.OperatingSystemInterface.Setenv(ctx, key, value)
		lib.Poe(err)
	}
	println("Env configuration loaded from " + envDir)
}
