about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-05-22 12:35:44 -0400
committerDrew DeVault <sir@cmpwn.com>2019-05-22 12:35:44 -0400
commitf42724caf3b374df0fa4eb170b3527cd20814eed (patch)
tree6efdb42ce0423e25c172382565a3c317b836737b
parent80704f242206e56d4c07669a5ca42827c1d59b59 (diff)
downloadaerc-f42724caf3b374df0fa4eb170b3527cd20814eed.tar.gz
Install default configs to XDG config if not found
-rw-r--r--Makefile22
-rw-r--r--aerc.go7
-rw-r--r--config/config.go38
3 files changed, 53 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index 64ba7d1..5708845 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,15 @@
+PREFIX?=/usr/local
+_INSTDIR=$(DESTDIR)$(PREFIX)
+BINDIR?=$(_INSTDIR)/bin
+SHAREDIR?=$(_INSTDIR)/share/aerc
+MANDIR?=$(_INSTDIR)/share/man
 GOFLAGS?=
 
 aerc:
-	go build $(GOFLAGS) -o aerc
+	go build $(GOFLAGS) \
+		-ldflags "-X main.Prefix=$(PREFIX)" \
+		-ldflags "-X main.ShareDir=$(SHAREDIR)" \
+		-o aerc
 
 %.1: doc/%.1.scd
 	scdoc < $< > $@
@@ -19,17 +27,13 @@ DOCS := \
 	aerc-smtp.5 \
 	aerc-tutorial.7
 
-all: aerc $(DOCS)
+doc: $(DOCS)
+
+all: aerc doc
 
 clean:
 	rm -f *.1 *.5 aerc
 
-PREFIX?=/usr/local
-_INSTDIR=$(DESTDIR)$(PREFIX)
-BINDIR?=$(_INSTDIR)/bin
-SHAREDIR?=$(_INSTDIR)/share/aerc
-MANDIR?=$(_INSTDIR)/share/man
-
 install: all
 	mkdir -p $(BINDIR) $(MANDIR)/man1 $(MANDIR)/man5 \
 		$(SHAREDIR) $(SHAREDIR)/filters
@@ -47,4 +51,4 @@ install: all
 
 .DEFAULT_GOAL := all
 
-.PHONY: aerc clean install
+.PHONY: aerc all doc clean install
diff --git a/aerc.go b/aerc.go
index 6f794c6..22df069 100644
--- a/aerc.go
+++ b/aerc.go
@@ -47,6 +47,11 @@ func getCommands(selected libui.Drawable) []*commands.Commands {
 	}
 }
 
+var (
+	Prefix   string
+	ShareDir string
+)
+
 func main() {
 	var (
 		logOut io.Writer
@@ -60,7 +65,7 @@ func main() {
 	logger = log.New(logOut, "", log.LstdFlags)
 	logger.Println("Starting up aerc")
 
-	conf, err := config.LoadConfig(nil)
+	conf, err := config.LoadConfig(nil, ShareDir)
 	if err != nil {
 		fmt.Printf("Failed to load config: %v\n", err)
 		os.Exit(1)
diff --git a/config/config.go b/config/config.go
index 1019297..1fb764b 100644
--- a/config/config.go
+++ b/config/config.go
@@ -3,6 +3,7 @@ package config
 import (
 	"errors"
 	"fmt"
+	"io/ioutil"
 	"net/url"
 	"os"
 	"os/exec"
@@ -189,7 +190,25 @@ func parseCredential(cred, command string) (string, error) {
 	return u.String(), nil
 }
 
-func LoadConfig(root *string) (*AercConfig, error) {
+func installTemplate(root, sharedir, name string) error {
+	if _, err := os.Stat(root); os.IsNotExist(err) {
+		err := os.MkdirAll(root, 0755)
+		if err != nil {
+			return err
+		}
+	}
+	data, err := ioutil.ReadFile(path.Join(sharedir, name))
+	if err != nil {
+		return err
+	}
+	err = ioutil.WriteFile(path.Join(root, name), data, 0644)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func LoadConfig(root *string, sharedir string) (*AercConfig, error) {
 	if root == nil {
 		_root := path.Join(xdg.ConfigHome(), "aerc")
 		root = &_root
@@ -201,7 +220,12 @@ func LoadConfig(root *string) (*AercConfig, error) {
 	filename = path.Join(*root, "aerc.conf")
 	file, err := ini.Load(filename)
 	if err != nil {
-		return nil, err
+		if err := installTemplate(*root, sharedir, "aerc.conf"); err != nil {
+			return nil, err
+		}
+		if file, err = ini.Load(filename); err != nil {
+			return nil, err
+		}
 	}
 	file.NameMapper = mapName
 	config := &AercConfig{
@@ -291,9 +315,15 @@ func LoadConfig(root *string) (*AercConfig, error) {
 	} else {
 		config.Accounts = accounts
 	}
-	binds, err := ini.Load(path.Join(*root, "binds.conf"))
+	filename = path.Join(*root, "binds.conf")
+	binds, err := ini.Load(filename)
 	if err != nil {
-		return nil, err
+		if err := installTemplate(*root, sharedir, "binds.conf"); err != nil {
+			return nil, err
+		}
+		if binds, err = ini.Load(filename); err != nil {
+			return nil, err
+		}
 	}
 	groups := map[string]**KeyBindings{
 		"default":  &config.Bindings.Global,