package main

import (
	"flag"
	"fmt"
	"os"

	"gitbase.de/apairon/mark2web/config"
	"gitbase.de/apairon/mark2web/helper"
)

var (
	// Version is the app's version string
	Version = "UNKNOWN"
	// GitHash is the current git hash for this version
	GitHash = "UNKNOWN"
	// BuildTime is the time of build of this app
	BuildTime = "UNKNOWN"
)

var log = helper.Log

var contentConfig = new(config.PathConfigTree)

func main() {
	inDir := flag.String("in", "./", "input directory")
	outDir := flag.String("out", "html", "output directory")
	createOutDir := flag.Bool("create", false, "create output directory if not existing")
	//clearOutDir := flag.Bool("clear", false, "clear output directory before generating website")
	logLevel := flag.String("logLevel", "info", "log level: debug, info, warning, error")
	version := flag.Bool("version", false, "print version of this executable")

	flag.Parse()
	if version != nil && *version {
		fmt.Printf(`%11s: %s
%11s: %s
%11s: %s
`, "version", Version, "git hash", GitHash, "build time", BuildTime)
		os.Exit(0)
	}

	level := "info"
	if logLevel != nil {
		level = *logLevel
	}
	helper.ConfigureLogger(level)

	if inDir == nil || *inDir == "" {
		log.Panic("input directory not specified")
	}
	log.Infof("input directory: %s", *inDir)

	if outDir == nil || *outDir == "" {
		log.Panic("output directory not specified")
	}
	log.Infof("output directory: %s", *outDir)

	if createOutDir != nil && *createOutDir {
		if _, err := os.Stat(*outDir); os.IsNotExist(err) {
			log.Debugf("output directory '%s' does not exist", *outDir)
			log.Debugf("trying to create output directory: %s", *outDir)
			err := os.MkdirAll(*outDir, 0755)
			if err != nil {
				log.Panic(err)
			}
			log.Noticef("created output directory: %s", *outDir)
		} else {
			log.Noticef("output directory '%s' already exists", *outDir)
		}
	}

	if fD, err := os.Stat(*outDir); os.IsNotExist(err) {
		log.Panicf("output directory '%s' does not exist, try -create parameter or create manually", *outDir)
	} else {
		if fD == nil {
			log.Panicf("something went wrong, could not get file handle for output dir %s", *outDir)
		} else if !fD.IsDir() {
			log.Panicf("output directory '%s' is not a directory", *outDir)
		}
	}

	log.Debug("reading global config...")
	configFilename := *inDir + "/config.yml"
	err := config.ReadGlobalConfig(configFilename)
	if err != nil {
		log.Panicf("could not read file '%s': %s", configFilename, err)
	}
	config.Config.Directories.Input = *inDir
	config.Config.Directories.Output = *outDir

	log.Debugf("reading input directory %s", *inDir)

	defaultTemplate := "base.html"
	defaultInputFile := "README.md"
	defaultOutputFile := "index.html"
	defaultPathStrip := "^[0-9]*_(.*)"
	defaultPathIgnoreForNav := "^_"
	defaultFilenameStrip := "(.*).md$"
	defaultFilenameIgnore := "^_"
	defaultFilenameOutputExtension := "html"

	defaultPathConfig := new(config.PathConfig)
	defaultPathConfig.Template = &defaultTemplate
	defaultPathConfig.Index = &config.IndexConfig{
		InputFile:  &defaultInputFile,
		OutputFile: &defaultOutputFile,
	}
	defaultPathConfig.Path = &config.DirnameConfig{
		Strip:        &defaultPathStrip,
		IgnoreForNav: &defaultPathIgnoreForNav,
	}
	defaultPathConfig.Filename = &config.FilenameConfig{
		Strip:           &defaultFilenameStrip,
		Ignore:          &defaultFilenameIgnore,
		OutputExtension: &defaultFilenameOutputExtension,
	}
	defaultPathConfig.Imaging = &config.ImagingConfig{
		Width:   1920,
		Height:  1920,
		Process: "fit",
		Quality: 75,
	}

	helper.ReadContentDir(*inDir+"/content", *outDir, "", defaultPathConfig, contentConfig)
	//spew.Dump(contentConfig)

	//spew.Dump(navMap)

	templatesDir := *inDir + "/templates"
	helper.SetTemplateDir(templatesDir)
	filtersDir := templatesDir + "/filters"
	if _, err := os.Stat(filtersDir); !os.IsNotExist(err) {
		helper.RegisterFilters(filtersDir)
	}
	helper.ProcessContent(contentConfig, contentConfig)

	helper.ProcessAssets()
}
