From 8e75a646d1f8bb64379a67b1160d681768f3ec5f Mon Sep 17 00:00:00 2001
From: Nick White <git@njw.name>
Date: Wed, 28 Aug 2019 19:30:53 +0100
Subject: Add standalone graph tool; confgraph

---
 bookpipeline/cmd/confgraph/main.go | 71 ++++++++++++++++++++++++++++++++++++++
 bookpipeline/graph.go              |  8 +++--
 2 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 bookpipeline/cmd/confgraph/main.go

diff --git a/bookpipeline/cmd/confgraph/main.go b/bookpipeline/cmd/confgraph/main.go
new file mode 100644
index 0000000..20a5838
--- /dev/null
+++ b/bookpipeline/cmd/confgraph/main.go
@@ -0,0 +1,71 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"rescribe.xyz/go.git/bookpipeline"
+	"rescribe.xyz/go.git/lib/hocr"
+)
+
+func walker(confs *[]*bookpipeline.Conf) filepath.WalkFunc {
+	return func(path string, info os.FileInfo, err error) error {
+		if info.IsDir() {
+			return nil
+		}
+		if ! strings.HasSuffix(path, ".hocr") {
+			return nil
+		}
+		avg, err := hocr.GetAvgConf(path)
+		if err != nil {
+			return err
+		}
+		c := bookpipeline.Conf{
+			Conf: avg,
+			Path: path,
+		}
+		*confs = append(*confs, &c)
+		return nil
+	}
+}
+
+func main() {
+	flag.Usage = func() {
+		fmt.Fprintln(flag.CommandLine.Output(), "Usage: bookpipeline hocrdir graph.png")
+		flag.PrintDefaults()
+	}
+	flag.Parse()
+
+	if flag.NArg() != 2 {
+		flag.Usage()
+		return
+	}
+
+	var confs []*bookpipeline.Conf
+	err := filepath.Walk(flag.Arg(0), walker(&confs))
+	if err != nil {
+		log.Fatalln("Failed to walk", flag.Arg(0), err)
+	}
+
+	// Structure to fit what bookpipeline.Graph needs
+	// TODO: probably reorganise bookpipeline to just need []*Conf
+	cconfs := make(map[string]*bookpipeline.Conf)
+	for _, c := range confs {
+		cconfs[c.Path] = c
+	}
+
+	fn := flag.Arg(1)
+	f, err := os.Create(fn)
+	if err != nil {
+		log.Fatalln("Error creating file", fn, err)
+	}
+	defer f.Close()
+	err = bookpipeline.Graph(cconfs, filepath.Base(flag.Arg(0)), f)
+	if err != nil {
+		log.Fatalln("Error creating graph", err)
+	}
+}
diff --git a/bookpipeline/graph.go b/bookpipeline/graph.go
index a4fdee0..4386218 100644
--- a/bookpipeline/graph.go
+++ b/bookpipeline/graph.go
@@ -23,12 +23,16 @@ type GraphConf struct {
 	Pgnum, Conf float64
 }
 
-func Graph(confs map[string]*Conf, bookname string, w io.Writer) (error) {
+func Graph(confs map[string]*Conf, bookname string, w io.Writer) error {
 	// Organise confs to sort them by page
 	var graphconf []GraphConf
 	for _, conf := range confs {
 		name := filepath.Base(conf.Path)
-		numend := strings.Index(name, "_")
+		var numend int
+		numend = strings.Index(name, "_")
+		if numend == -1 {
+			numend = strings.Index(name, ".")
+		}
 		pgnum, err := strconv.ParseFloat(name[0:numend], 64)
 		if err != nil {
 			continue
-- 
cgit v1.2.1-24-ge1ad