<commit_before>// go build -ldflags "-X main.dest linux -X main.version local"  main.go

package main

import (
	"bufio"
	"encoding/xml"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"os/user"
	"path/filepath"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"syscall"
	"time"

	"sp"

	"github.com/speedata/configurator"
	"github.com/speedata/hotfolder"
	"github.com/speedata/optionparser"
)

const (
	cmdRun        = "run"
	cmdServer     = "server"
	cmdCompare    = "compare"
	cmdClean      = "clean"
	cmdClearcache = "clearcache"
	cmdDoc        = "doc"
	cmdListFonts  = "list-fonts"
	cmdNew        = "new"
	cmdWatch      = "watch"
	cmdHelp       = "help"
	cmdHTML       = "html"

	osWindows = "windows"
	osLinux   = "linux"
	osDarwin  = "darwin"

	stringFalse = "false"
	stringTrue  = "true"
)

var (
	options             map[string]string
	defaults            map[string]string
	layoutoptions       map[string]string
	variables           map[string]string
	installdir          string
	bindir              string
	libdir              string
	srcdir              string
	pathToDocumentation string // Where the documentation (index.html) is
	inifile             string
	dest                string // The platform which this script runs on.
	version             string
	homecfg             string
	systemcfg           string
	pwd                 string
	exeSuffix           string
	homedir             string
	addLocalPath        bool // Add pwd recursively to extra-dir
	useSystemFonts      bool
	configfilename      string
	mainlanguage        string
	extraDir            []string
	extraxml            []string
	prependxml          []string
	starttime           time.Time
	cfg                 *configurator.ConfigData
	runningProcess      []*os.Process

	verbose bool
)

// The LuaTeX process writes out a file called "publisher.status"
// which is a valid XML file. Currently the only field is "Errors"
// with the number of errors occurred during the publisher run.
type statuserror struct {
	XMLName xml.Name `xml:"Error"`
	Code    int      `xml:"code,attr"`
	Error   string   `xml:",chardata"`
}

type status struct {
	XMLName xml.Name `xml:"Status"`
	Error   []statuserror
	Errors  int
}

func init() {
	var err error
	log.SetFlags(0)
	starttime = time.Now()
	go sigIntCatcher()
	pwd, err = os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	variables = make(map[string]string)
	layoutoptions = make(map[string]string)
	options = make(map[string]string)
	defaults = map[string]string{
		"address":    "127.0.0.1",
		"data":       "data.xml",
		"fontpath":   "",
		"grid":       "",
		"imagecache": "",
		"jobname":    "publisher",
		"layout":     "layout.xml",
		"port":       "5266",
		"quiet":      stringFalse,
		"runs":       "1",
		"tempdir":    os.TempDir(),
		"cache":      "optimal",
		"inkscape":   "inkscape",
	}

	// Let's try to find out the installation dir
	if execlocation, err := os.Executable(); err != nil {
		log.Fatal(err)
	} else {
		bindir = filepath.Dir(execlocation)
	}
	installdir = filepath.Join(bindir, "..")

	if version == "" {
		version = "local"
	}

	// log.Print("Built for platform: ",dest)
	switch runtime.GOOS {
	case osDarwin:
		defaults["opencommand"] = "open"
		exeSuffix = ""
		homedir = os.Getenv("HOME")
	case osLinux:
		defaults["opencommand"] = "xdg-open"
		homedir = os.Getenv("HOME")
		exeSuffix = ""
	case osWindows:
		defaults["opencommand"] = "cmd /C start"
		exeSuffix = ".exe"

		me, err := user.Current()
		if err != nil {
			log.Fatal(err)
		}
		homedir = me.HomeDir
	}
	addLocalPath = true
	useSystemFonts = false
	configfilename = "publisher.cfg"
	mainlanguage = "en_GB"

	// LC_ALL is something like "de_DE.UTF-8"
	re := regexp.MustCompile("^(d|D)(e|E)")
	var indexpage string
	if re.MatchString(os.Getenv("LANG")) {<commit_msg>Different way to find the executable<commit_after>// go build -ldflags "-X main.dest linux -X main.version local"  main.go

package main

import (
	"bufio"
	"encoding/xml"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"os/user"
	"path/filepath"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"syscall"
	"time"

	"sp"

	"github.com/speedata/configurator"
	"github.com/speedata/hotfolder"
	"github.com/speedata/optionparser"
)

const (
	cmdRun        = "run"
	cmdServer     = "server"
	cmdCompare    = "compare"
	cmdClean      = "clean"
	cmdClearcache = "clearcache"
	cmdDoc        = "doc"
	cmdListFonts  = "list-fonts"
	cmdNew        = "new"
	cmdWatch      = "watch"
	cmdHelp       = "help"
	cmdHTML       = "html"

	osWindows = "windows"
	osLinux   = "linux"
	osDarwin  = "darwin"

	stringFalse = "false"
	stringTrue  = "true"
)

var (
	options             map[string]string
	defaults            map[string]string
	layoutoptions       map[string]string
	variables           map[string]string
	installdir          string
	bindir              string
	libdir              string
	srcdir              string
	pathToDocumentation string // Where the documentation (index.html) is
	inifile             string
	dest                string // The platform which this script runs on.
	version             string
	homecfg             string
	systemcfg           string
	pwd                 string
	exeSuffix           string
	homedir             string
	addLocalPath        bool // Add pwd recursively to extra-dir
	useSystemFonts      bool
	configfilename      string
	mainlanguage        string
	extraDir            []string
	extraxml            []string
	prependxml          []string
	starttime           time.Time
	cfg                 *configurator.ConfigData
	runningProcess      []*os.Process

	verbose bool
)

// The LuaTeX process writes out a file called "publisher.status"
// which is a valid XML file. Currently the only field is "Errors"
// with the number of errors occurred during the publisher run.
type statuserror struct {
	XMLName xml.Name `xml:"Error"`
	Code    int      `xml:"code,attr"`
	Error   string   `xml:",chardata"`
}

type status struct {
	XMLName xml.Name `xml:"Status"`
	Error   []statuserror
	Errors  int
}

func init() {
	var err error
	log.SetFlags(0)
	starttime = time.Now()
	go sigIntCatcher()
	pwd, err = os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	variables = make(map[string]string)
	layoutoptions = make(map[string]string)
	options = make(map[string]string)
	defaults = map[string]string{
		"address":    "127.0.0.1",
		"data":       "data.xml",
		"fontpath":   "",
		"grid":       "",
		"imagecache": "",
		"jobname":    "publisher",
		"layout":     "layout.xml",
		"port":       "5266",
		"quiet":      stringFalse,
		"runs":       "1",
		"tempdir":    os.TempDir(),
		"cache":      "optimal",
		"inkscape":   "inkscape",
	}

	switch runtime.GOOS {
	case osWindows:
		exeSuffix = ".exe"
	}
	// Let's try to find out the installation dir
	if execlocation, err := os.Executable(); err != nil {
		if strings.Contains(os.Args[0], "/") {
			if bindir, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
				log.Fatal(err)
			}
		} else {
			// if that fails (see for example #254), we try exec.LookPath
			if execlocation, err = exec.LookPath("sp" + exeSuffix); err != nil {
				log.Fatal(err)
			}
			bindir = filepath.Dir(execlocation)
		}
	} else {
		bindir = filepath.Dir(execlocation)
	}
	installdir = filepath.Join(bindir, "..")

	if version == "" {
		version = "local"
	}

	// log.Print("Built for platform: ",dest)
	switch runtime.GOOS {
	case osDarwin:
		defaults["opencommand"] = "open"
		homedir = os.Getenv("HOME")
	case osLinux:
		defaults["opencommand"] = "xdg-open"
		homedir = os.Getenv("HOME")
	case osWindows:
		defaults["opencommand"] = "cmd /C start"

		me, err := user.Current()
		if err != nil {
			log.Fatal(err)
		}
		homedir = me.HomeDir
	}
	addLocalPath = true
	useSystemFonts = false
	configfilename = "publisher.cfg"
	mainlanguage = "en_GB"

	// LC_ALL is something like "de_DE.UTF-8"
	re := regexp.MustCompile("^(d|D)(e|E)")
	var indexpage string
	if re.MatchString(os.Getenv("LANG")) {
{'fraction_non_alphanumeric': 0.1273525721455458, 'fraction_numerical': 0.0038895859473023838, 'mean_word_length': 2.5696372592924317, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1157610', 'n_tokens_mistral': 2935, 'n_tokens_neox': 2775, 'n_words': 684}