package installer

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"text/template"

	"git.rootprojects.org/root/watchdog.go/cmd/watchdog/installer/static"
)

func install(c *Config) error {
	// Linux-specific config options
	if c.System {
		if "" == c.User {
			c.User = "root"
		}
	}
	if "" == c.Group {
		c.Group = c.User
	}
	serviceDir := "/etc/systemd/system/"

	// Check paths first
	serviceName := c.Name + ".service"
	if !c.System {
		// Not sure which of these it's supposed to be...
		// * ~/.local/share/systemd/user/watchdog.service
		// * ~/.config/systemd/user/watchdog.service
		// https://wiki.archlinux.org/index.php/Systemd/User
		serviceDir = filepath.Join(c.home, ".local/share/systemd/user")
	}
	err = os.MkdirAll(filepath.Dir(serviceDir), 0750)
	if nil != err {
		return err
	}

	// Create service file from template
	b, err := static.ReadFile("dist/etc/systemd/system/_name_.service.tmpl")
	if err != nil {
		return err
	}
	s := string(b)
	rw := &bytes.Buffer{}
	// not sure what the template name does, but whatever
	tmpl, err := template.New("service").Parse(s)
	if err != nil {
		return err
	}
	err = tmpl.Execute(rw, c)
	if nil != err {
		return err
	}

	// Write the file out
	servicePath := filepath.Join(serviceDir, serviceName)
	if err := ioutil.WriteFile(servicePath, rw.Bytes(), 0644); err != nil {
		return fmt.Errorf("ioutil.WriteFile error: %v", err)
	}
	fmt.Printf("Wrote %q\n", servicePath)

	return nil
}
