package main

import (
	"io"
	"log"
	"os"

	"fyne.io/systray"
	"github.com/gen2brain/beeep"
)

const version = "0.23.0"

func main() {
	os.Exit(runCLI(os.Args[1:]))
}

func runTray(verbose bool) {
	if err := os.MkdirAll(configDir(), 0700); err != nil {
		log.Printf("restray: create config directory: %v", err)
		return
	}
	if err := os.MkdirAll(dataDir(), 0700); err != nil {
		log.Printf("restray: create state directory: %v", err)
		return
	}

	lock, err := os.OpenFile(lockPath(), os.O_CREATE|os.O_RDWR, 0600)
	if err != nil || lockFile(lock) != nil {
		return
	}
	defer lock.Close()

	lp := logPath()
	if info, err := os.Stat(lp); err == nil && info.Size() > 1<<20 {
		os.Truncate(lp, 0)
	}
	f, err := os.OpenFile(lp, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
	if err == nil {
		if verbose {
			log.SetOutput(io.MultiWriter(f, os.Stderr))
		} else {
			log.SetOutput(f)
		}
		log.SetFlags(log.Ldate | log.Ltime)
	}

	beeep.AppName = "Restray"
	log.Printf("Restray v%s", version)
	defer log.Print("Restray exiting")

	systray.Run(onReady, func() {
		log.Print("systray exited")
	})
}
