package main

import (
	"fmt"
	"github.com/urfave/cli"
	"formulaic/commands"
	"log"
	"os"
	"os/user"
	"path/filepath"
	"time"
)

func formRoot() (*string, error) {
	usr, err := user.Current()
	if err != nil {
		return nil, cli.NewExitError("Could not determine current user.", 1)
	}

	rootpath, err := filepath.Abs(usr.HomeDir + "/.formulaic/")
	if err != nil {
		return nil, cli.NewExitError(err, 1)
	}
	
	rootstat, err := os.Stat(rootpath)
	if os.IsNotExist(err) {
		os.Mkdir(rootpath, 0755)
		return &rootpath, nil
	}
	
	if !rootstat.IsDir() {
		return nil, cli.NewExitError(fmt.Sprintf("%s: not a directory", rootpath), 1)
	}

	return &rootpath, nil
}	


func main() {
	app := cli.NewApp()
	root, err := formRoot()

	if err != nil {
		log.Fatal(err)
	}

	branchFlag := cli.StringFlag{
		Name:  "checkout, b",
		Usage: "branch, tag or commit to checkout",
	}

	updateFlag := cli.BoolFlag{
		Name:  "update, u",
		Usage: "update the template local template if it is already present",
	}

	app.Name = "formulaic"
	app.Version = "0.2"
	app.Compiled = time.Now()
	app.Copyright = "Copyright © 2018 Elf M. Sternberg"
	app.Usage = "generate a new project from a project template"

	app.Authors = []cli.Author{
		{
			Name:  "Elf M. Sternberg",
			Email: "elf.sternberg@gmail.com",
		},
	}

	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "verbose, V",
			Usage: "print debugging information",
		},
	}

	app.Commands = []cli.Command{
		{
			Name:      "build",
			Aliases:   []string{"b"},
			Usage:     "build a new project from a formulaic template",
			UsageText: "formulaic build [options] [template name or url] [key=value ...]",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "config, c",
					Usage: "specify alternative formulaic config file",
					Value: "formulaic.[toml|json|yaml]",
				},
				cli.StringFlag{
					Name:  "path, o",
					Usage: "specify path to directory into which project will be written",
					Value: ".",
				},
				cli.BoolFlag{
					Name:  "force, f",
					Usage: "overwrite if output directory exists",
				},
				updateFlag,
				branchFlag,
				cli.BoolFlag{
					Name:  "noprompt, x",
					Usage: "do not prompt for input, use the formulaic config file only",
				},
				cli.BoolFlag{
					Name:  "replay, r",
					Usage: "use replay file, if any",
				},
			},
			Action: func(c *cli.Context) error {
				fmt.Printf("Build.")
				return nil
			},
		},
		{
			Name:      "list",
			Aliases:   []string{"l"},
			Usage:     "list all formulaic templates in your cache, with optional filter",
			UsageText: "formulaic list [options] [search expression]",
			Flags: []cli.Flag{
				/* This is commented out because I know that
				   maintaining a template repository will never be a
				   priority for me.
				*/
				/*
					cli.BoolFlag{
						Name: "remote, r",
						Usage: "show remote templates from template repository",
					},
				*/
				cli.BoolFlag{
					Name:  "long, l",
					Usage: "show the long description, if available",
				},
				cli.BoolFlag{
					Name:  "prompts, p",
					Usage: "show the template prompts, if available",
				},
			},
			Action: func(c *cli.Context) error {
				return commands.List(root)
			},
		},
		{
			Name:      "fetch",
			Aliases:   []string{"f"},
			Usage:     "fetch and store, but do not run, a template",
			UsageText: "formulaic fetch [options] [template url]",
			Flags: []cli.Flag{
				updateFlag,
			},
			Action: func(c *cli.Context) error {
				fmt.Printf("Fetch.")
				return nil
			},
		},
	}

	app.Run(os.Args)
}
