package main

import (
	"flag"
	"fmt"
	"os"

	"git.schreifuchs.ch/schreifuchs/pat/pkg/cat"
	"git.schreifuchs.ch/schreifuchs/pat/pkg/clip"
	"git.schreifuchs.ch/schreifuchs/pat/pkg/ignore"
)

const DELEMITTER = "-(%s)--------------------------------------------------------------------------\n"

func main() {
	ignorePath := flag.String("i", "", "set path to gitignore, if no gitignore parent dirs will be searched")
	hiddenFiles := flag.Bool("h", false, "show hidden files")
	delemitter := flag.String("d", DELEMITTER, "delemitter to use to split files when not in markdown mode must contain %s for filename")
	markdown := flag.Bool("m", false, "markdown mode, outputs files in markdown")
	typst := flag.Bool("t", false, "typst mode, outputs files in typst")
	flag.Parse()

	cats, err := cat.Path(flag.Args()...)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	if *ignorePath != "" {
		i, err := ignore.FindGitignore(*ignorePath)
		if err != nil {
			fmt.Printf("can't get gitignore: %v", err)
			os.Exit(1)
		}

		cats = cats.Ignored(i)
	}

	if !*hiddenFiles {
		cats = cats.Ignored(ignore.Filesystem{})
	}

	var out string
	if *markdown {
		out = cats.ToMarkdown()
	} else if *typst {
		out = cats.ToTypst()
	} else {
		out = cats.ToString(*delemitter)
	}
	fmt.Print(out)

	if err = clip.Copy(out); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
}
