package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"gopkg.in/yaml.v2"
    {{- range $key, $value := .}}
    pkg{{$key}} "{{$value}}"
    {{- end}}
)

func main() {
	replacer := strings.NewReplacer(
		"github.com/owncloud/ocis/v2/services/", "",
		"/pkg/config/defaults", "",
	)
	cfg := map[string]string{
	    {{- range $key, $value := .}}
		replacer.Replace("{{$value}}"): func() string {
			fmt.Println("Generating example YAML config for {{ $value -}}")
			c := pkg{{$key}}.FullDefaultConfig()
			yml, err := yaml.Marshal(c)
			if err != nil {
				log.Fatalf("Marshalling yaml for pkg0 failed: %s\n", err)
			}
			return fmt.Sprintf("# Autogenerated\n# Filename: %s-config-example.yaml\n\n%s", replacer.Replace("{{ $value }}"),string(yml))
		}(),
		{{- end}}
	}
	for pkg, yml := range cfg {
		targetFolders := []string{
			// TODO: comment in when it is clear how to commit this to the structure of the master|main branch
			// filepath.Join("../../", pkg, "/config"),
			"../../docs/services/_includes/",
		}
		for _, targetFolder := range targetFolders {
			os.MkdirAll(targetFolder, 0700)
			targetYamlFile, err := os.Create(filepath.Join(targetFolder, replacer.Replace(pkg) + "-config-example.yaml"))
			if err != nil {
				log.Fatalf("Failed to create target file for : %s", err)
			}
			defer targetYamlFile.Close()
			targetYamlFile.WriteString(yml)
		}
	}
}
