2021-08-26 14:32:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-08-26 15:31:20 +00:00
|
|
|
"os"
|
2021-08-26 14:32:51 +00:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2021-08-26 15:31:20 +00:00
|
|
|
const (
|
|
|
|
ErrNoCompiler = "none of the following latex compilers available: [latexmk, pdflatex]"
|
|
|
|
)
|
|
|
|
|
2021-08-26 14:32:51 +00:00
|
|
|
func main() {
|
|
|
|
fmt.Printf("Pdflatex command exists: %t\n", commandExists("pdflatex"))
|
2021-08-26 15:31:20 +00:00
|
|
|
fmt.Printf("LatexMk command exists: %t\n", commandExists("latexmk"))
|
|
|
|
fmt.Printf("Operation System: %s/%s\n", runtime.GOOS, runtime.GOARCH)
|
2021-08-26 14:32:51 +00:00
|
|
|
|
|
|
|
compileAndInstall()
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileAndInstall() {
|
|
|
|
out, err := compileLatex("main.tex")
|
2021-08-26 15:31:20 +00:00
|
|
|
|
2021-08-26 14:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("An error occured while compiling the document!")
|
|
|
|
|
2021-08-26 15:31:20 +00:00
|
|
|
if err.Error() == ErrNoCompiler {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if filename := parseMissingFile(out); filename != "" {
|
2021-08-26 14:32:51 +00:00
|
|
|
fmt.Printf("We need to download: %s\n", filename)
|
|
|
|
|
2021-08-26 15:31:20 +00:00
|
|
|
// now we need to perform a root check
|
2021-08-26 14:32:51 +00:00
|
|
|
if rootCheck() {
|
|
|
|
log.Println("Awesome! You are now running this program with root permissions!")
|
|
|
|
|
|
|
|
if installFile(filename) {
|
2021-08-26 15:31:20 +00:00
|
|
|
// we remove the main aux file to really trigger a rebuild!
|
|
|
|
os.Remove("main.aux")
|
2021-08-26 14:32:51 +00:00
|
|
|
// if successfully installed we try to compile again
|
|
|
|
compileAndInstall()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Fatal("This program must be run as root! (sudo)")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println(*out)
|
|
|
|
|
|
|
|
fmt.Println("another build error occured!")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("document built successfully!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMissingFile(output *string) string {
|
|
|
|
matchfile := regexp.MustCompile("! LaTeX Error: File `([^`']*)' not found|! I can't find file `([^`']*)'.")
|
|
|
|
matches := matchfile.FindStringSubmatch(*output)
|
|
|
|
fmt.Printf("%#v\n", matches)
|
|
|
|
if matches != nil {
|
|
|
|
if matches[1] != "" {
|
|
|
|
return matches[1]
|
|
|
|
} else {
|
|
|
|
return matches[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok now we try to find a font error
|
|
|
|
fontregex := regexp.MustCompile(`! Font \\[^=]*=([^\s]*)\s`)
|
|
|
|
fontmatch := fontregex.FindStringSubmatch(*output)
|
|
|
|
fmt.Printf("%#v\n", fontmatch)
|
|
|
|
if fontmatch != nil {
|
|
|
|
if fontmatch[1] != "" {
|
|
|
|
return fontmatch[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now try babel errors
|
|
|
|
babelregex := regexp.MustCompile("Unknown option `([^`']*)'. Either you misspelled")
|
|
|
|
babelmatch := babelregex.FindStringSubmatch(*output)
|
|
|
|
if babelmatch != nil {
|
|
|
|
if babelmatch[1] != "" {
|
|
|
|
return babelmatch[1] + ".ldf"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if a specific system command is available
|
|
|
|
func commandExists(cmd string) bool {
|
|
|
|
_, err := exec.LookPath(cmd)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the thumbail picture from video file
|
|
|
|
func compileLatex(filename string) (*string, error) {
|
2021-08-26 15:31:20 +00:00
|
|
|
app := ""
|
|
|
|
if commandExists("latexmk") {
|
|
|
|
app = "latexmk"
|
|
|
|
} else if commandExists("pdflatex") {
|
|
|
|
app = "pdflatex"
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf(ErrNoCompiler)
|
|
|
|
}
|
2021-08-26 14:32:51 +00:00
|
|
|
|
|
|
|
cmd := exec.Command(app,
|
|
|
|
"-file-line-error",
|
|
|
|
"-interaction=nonstopmode",
|
|
|
|
"-synctex=1",
|
|
|
|
"-output-format=pdf",
|
|
|
|
filename)
|
|
|
|
|
|
|
|
stdout, err := cmd.Output()
|
|
|
|
|
|
|
|
output := string(stdout)
|
|
|
|
|
|
|
|
return &output, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootCheck() bool {
|
|
|
|
cmd := exec.Command("id", "-u")
|
|
|
|
output, err := cmd.Output()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// output has trailing \n
|
|
|
|
// need to remove the \n
|
|
|
|
// otherwise it will cause error for strconv.Atoi
|
|
|
|
// log.Println(output[:len(output)-1])
|
|
|
|
|
|
|
|
// 0 = root, 501 = non-root user
|
|
|
|
i, err := strconv.Atoi(string(output[:len(output)-1]))
|
|
|
|
|
|
|
|
if err != nil {
|
2021-08-26 15:31:20 +00:00
|
|
|
// maybe no unix system?
|
2021-08-26 14:32:51 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:31:20 +00:00
|
|
|
return i == 0
|
2021-08-26 14:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func installFile(filename string) bool {
|
|
|
|
if !commandExists("dnf") {
|
|
|
|
fmt.Println("dnf not existing!")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("dnf", "-y", "install", fmt.Sprintf("tex(%s)", filename))
|
|
|
|
fmt.Println(cmd.String())
|
|
|
|
|
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
|
|
|
|
fmt.Println("running dnf install now!")
|
2021-08-26 15:31:20 +00:00
|
|
|
cmd.Start()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
for scanner.Scan() {
|
|
|
|
m := scanner.Text()
|
|
|
|
fmt.Println(m)
|
|
|
|
}
|
|
|
|
}()
|
2021-08-26 14:32:51 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-08-26 15:31:20 +00:00
|
|
|
scanner := bufio.NewScanner(stderr)
|
2021-08-26 14:32:51 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
m := scanner.Text()
|
|
|
|
fmt.Println(m)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-08-26 15:31:20 +00:00
|
|
|
err := cmd.Wait()
|
2021-08-26 14:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|