// Copyright (C) 2022 Marius Schellenberger

package main

import (
	"encoding/hex"
	"flag"
	"fmt"
	"os"

	"cachefs/pkg/fs"
	"cachefs/pkg/provider/parse"
)

var (
	version string

	dst      string
	metadata string
	fstat    bool
)

func fatal(err error) {
	fmt.Println(err)
	os.Exit(1)
}

func main() {
	flag.StringVar(&dst, "dst", "", "url path to cache files (example: file:///mnt/cache, uses DST_KEY)")
	flag.StringVar(&metadata, "data", "", "path to metadata file")
	flag.BoolVar(&fstat, "fstat", false, "use fstat to determine real file size for sparse files")
	flag.Parse()

	var (
		dstk      []byte
		err       error
		encrypted bool
	)
	dstkey := os.Getenv("DST_KEY")
	if dstkey != "" {
		dstk, err = hex.DecodeString(dstkey)
		if err != nil {
			fatal(err)
		}
		encrypted = true
	}
	dstfs, err := parse.FS(dst, dstk, true)
	if err != nil {
		fatal(err)
	}
	err = fs.MetadataGenerator(metadata, dstfs, fstat, encrypted)
	if err != nil {
		fatal(err)
	}
}
