//go:build none

// Create the help text for the rc
//
// Run with go generate ./cmd/bisync (defined in rc.go)
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/muesli/reflow/wordwrap"
	"github.com/rclone/rclone/cmd"
	"github.com/rclone/rclone/cmd/bisync"
	"github.com/rclone/rclone/fs"
	"github.com/spf13/pflag"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

// Output the help to stdout
func main() {
	out := os.Stdout
	if len(os.Args) > 1 {
		var err error
		out, err = os.Create(os.Args[1])
		if err != nil {
			fs.Fatalf(nil, "Open output failed: %v", err)
		}
		defer out.Close()
	}
	fmt.Fprintf(out, "<!--- Docs generated by help.go - use go generate to rebuild - DO NOT EDIT --->\n\n")
	fmt.Fprint(out, RcHelp())
}

// RcHelp returns the rc help
func RcHelp() string {
	return wordwrap.String(bisync.MakeHelp(`This takes the following parameters:

- path1 (required) - (string) a remote directory string e.g. ||drive:path1||
- path2 (required) - (string) a remote directory string e.g. ||drive:path2||
- dryRun - (bool) dry-run mode
`+GenerateParams()+`
See [bisync command help](https://rclone.org/commands/rclone_bisync/)
and [full bisync description](https://rclone.org/bisync/)
for more information.
`), 76)
}

// example: "create-empty-src-dirs" -> "createEmptySrcDirs"
func toCamel(s string) string {
	split := strings.Split(s, "-")
	builder := strings.Builder{}
	for i, word := range split {
		if i == 0 { // first word always all lowercase
			builder.WriteString(strings.ToLower(word))
			continue
		}
		builder.WriteString(cases.Title(language.AmericanEnglish).String(word))
	}
	return builder.String()
}

// GenerateParams automatically generates the param list from commandDefinition.Flags
func GenerateParams() string {
	builder := strings.Builder{}
	fn := func(flag *pflag.Flag) {
		if flag.Hidden {
			return
		}
		builder.WriteString(fmt.Sprintf("- %s - (%s) %s  \n", toCamel(flag.Name), flag.Value.Type(), flag.Usage))
	}
	commandDefinition, _, _ := cmd.Root.Find([]string{"bisync"})
	commandDefinition.Flags().VisitAll(fn)
	return builder.String()
}
