package opencore import ( "fmt" "os" "path/filepath" "regexp" "strings" "github.com/Nwflower/reims-go/internal/i18n" ) const ( // GuestLangSidecarFile stores the user's chosen OpenCore prev-lang:kbd value // inside the VM directory so `update-opencore` rebuilds can replay it. GuestLangSidecarFile = ".reims-go-guest-lang" ) // Preset describes a selectable macOS boot/recovery language (`prev-lang:kbd`). type Preset struct { Value string `json:"value"` LabelZH string `json:"label_zh"` LabelEN string `json:"label_en"` } // Presets returns the built-in macOS prev-lang:kbd choices. // `:0` pairs the locale with the standard US keyboard layout so terminal // commands in macOS Recovery / Setup Assistant stay predictable; `:252` uses // OpenCore's Pinyin layout ID. func Presets() []Preset { return []Preset{ {Value: "zh-Hans:0", LabelZH: "简体中文(美式键盘,推荐)", LabelEN: "Simplified Chinese (US Keyboard)"}, {Value: "zh-Hans:252", LabelZH: "简体中文(拼音键盘)", LabelEN: "Simplified Chinese (Pinyin)"}, {Value: "zh-Hant:0", LabelZH: "繁體中文(美式键盘)", LabelEN: "Traditional Chinese (US Keyboard)"}, {Value: "en-US:0", LabelZH: "English (US)", LabelEN: "English (US)"}, {Value: "ja-JP:0", LabelZH: "日本語(美式键盘)", LabelEN: "Japanese (US Keyboard)"}, {Value: "ko-KR:0", LabelZH: "한국어(美式键盘)", LabelEN: "Korean (US Keyboard)"}, } } // DefaultGuestLang chooses the default macOS prev-lang:kbd matching the app UI locale. func DefaultGuestLang(locale i18n.Locale) string { resolved := strings.ToLower(string(i18n.ResolveLocale(string(locale)))) switch { case resolved == "zh-tw" || resolved == "zh-hk" || resolved == "zh-mo" || strings.HasPrefix(resolved, "zh-hant"): return "zh-Hant:0" case strings.HasPrefix(resolved, "zh"): return "zh-Hans:0" case strings.HasPrefix(resolved, "ja"): return "ja-JP:0" case strings.HasPrefix(resolved, "ko"): return "ko-KR:0" default: return "en-US:0" } } var reGuestLang = regexp.MustCompile(`^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,2}:[0-9]{1,6}$`) // NormalizeGuestLang validates and normalizes a prev-lang:kbd string. // An empty input returns ("", nil), meaning "unspecified / keep existing". func NormalizeGuestLang(raw string) (string, error) { s := strings.TrimSpace(raw) if s == "" { return "", nil } if !reGuestLang.MatchString(s) { return "", fmt.Errorf("invalid macOS language code %q (expected format like zh-Hans:0 or en-US:0)", raw) } return s, nil } // ReadSavedGuestLang reads the persisted prev-lang:kbd preference from vmDir. func ReadSavedGuestLang(vmDir string) string { if vmDir == "" { return "" } b, err := os.ReadFile(filepath.Join(vmDir, GuestLangSidecarFile)) if err != nil { return "" } norm, err := NormalizeGuestLang(string(b)) if err != nil { return "" } return norm } // WriteSavedGuestLang saves (or clears when empty) the VM's prev-lang:kbd preference. func WriteSavedGuestLang(vmDir, lang string) error { if vmDir == "" { return fmt.Errorf("vm directory is empty") } norm, err := NormalizeGuestLang(lang) if err != nil { return err } p := filepath.Join(vmDir, GuestLangSidecarFile) if norm == "" { if err := os.Remove(p); err != nil && !os.IsNotExist(err) { return err } return nil } return os.WriteFile(p, []byte(norm+"\n"), 0o644) } // ResolveOpenCorePath returns the OpenCore image path inside vmDir (preferring // opencore.qcow2, falling back to opencore.img), or "" if neither exists. func ResolveOpenCorePath(vmDir string) string { if vmDir == "" { return "" } for _, name := range []string{"opencore.qcow2", "opencore.img"} { p := filepath.Join(vmDir, name) if info, err := os.Stat(p); err == nil && !info.IsDir() { return p } } return "" }