/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

package main

import (
	"io/ioutil"
	"os"

	"github.com/jawher/mow.cli"

	"code.vanwa.ch/sebastian/ecload/pkg/ecload"
)

var Version string
var Build string

func main() {
	logger := ecload.InitLogger(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)

	app := cli.App("ecload", "Download books from https://www.e-codices.unifr.ch")
	app.Version("v version", Version)

	app.Spec = "[-o=<PATH>] [-s=<SIZE>] ID"

	var (
		outDir = app.StringOpt("o out-dir", ".", "save pdf in PATH")
		size   = app.StringOpt("s size", "medium", "Size of the downloaded images. One of small, medium, large, max.")
		id     = app.StringArg("ID", "", "ID of the book to download. Copy the last two url parts on the overview page to get it (for example bbb/0003).")
	)

	app.Action = func() {
		if *size != "small" && *size != "medium" && *size != "large" && *size != "max" {
			logger.Error.Println("SIZE must be one of: small, medium, large, max")
			cli.Exit(1)
		}

		err := ecload.DownloadBook(*outDir, *size, *id, logger)
		if err != nil {
			logger.Error.Println(err)
			os.Exit(1)
		}
	}

	err := app.Run(os.Args)
	if err != nil {
		os.Exit(1)
	}
}
