package main

import (
	"fmt"
	"os"
	"regexp"
	"strings"
)

var reS3KeyPDFUpload = regexp.MustCompile(`^.*\/(?P<year>\d{4})\/(?P<month>\d{2})\/(?P<day>\d{2})\/.*\.pdf$`)

func main() {
	key := "xyz/2019/12/06/2019_12_06_SOMETHING.pdf"
	if !reS3KeyPDFUpload.MatchString(key) {
		fmt.Fprintf(os.Stderr, "key [%s] does not match pattern\n", key)
		os.Exit(1)
	}

	res := reS3KeyPDFUpload.FindStringSubmatch(key)
	fmt.Printf("%+v\n", res)

	pre := getS3PrefixesToIndex(key)
	fmt.Printf("s3 Prefixes: %+v\n", pre)
}

var iter = 0

func basicRecursion(sections []string, prefix string) []string {
	iter++
	fmt.Printf("iter = %v\n", iter)

	fmt.Printf("len(sections) = %d\n", len(sections))
	var list []string
	head := sections[0]
	tail := sections[1:]
	if len(prefix) > 0 {
		head = fmt.Sprintf("%v/%v", prefix, head)
	}

	list = append(list, head)
	if len(tail) > 0 {
		l2 := basicRecursion(tail, head)
		return append(list, l2...)
	}
	return list
}

func getS3PrefixesToIndex(s3Key string) []string {
	sections := strings.Split(s3Key, "/")
	return basicRecursion(sections[:len(sections)-1], "")
}