aboutsummaryrefslogtreecommitdiff
path: root/cmd/mpd-scrobbler/main.go
blob: 30c3e5c7e228a0b865e56b087e553788154b8767 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"go.fcuny.net/x/internal/scrobbler"
)

func main() {
	var (
		mpdHost = flag.String(
			"host",
			"localhost",
			"The MPD server to connect  to (default: localhost)",
		)
		mpdPort = flag.Int(
			"port",
			6600,
			"The TCP port of the MPD server to connect to (default: 6600)",
		)
	)
	flag.Parse()

	net := "tcp"
	addr := fmt.Sprintf("%s:%d", *mpdHost, *mpdPort)

	dbpath, err := getDbPath()
	if err != nil {
		log.Fatalf("failed to get the path to the database: %v", err)
	}

	s, err := scrobbler.NewScrobbler(net, addr, dbpath)
	if err != nil {
		log.Fatalf("failed to create a client: %v", err)
	}

	defer func() {
		if err := s.Close(); err != nil {
			log.Fatalf("failed to close the scrobbler: %v", err)
		}
	}()

	err = s.Run()
	if err != nil {
		log.Fatalf("failed to run the scrobbler: %v", err)
	}
}

func getDbPath() (string, error) {
	xch := os.Getenv("XDG_CONFIG_HOME")
	if xch == "" {
		home := os.Getenv("HOME")
		xch = filepath.Join(home, ".config")
	}

	scrobblerHome := filepath.Join(xch, "mpd-scrobbler")
	if _, err := os.Stat(scrobblerHome); os.IsNotExist(err) {
		if err := os.Mkdir(scrobblerHome, 0o755); err != nil {
			return "", err
		}
	}

	return filepath.Join(scrobblerHome, "scrobbler.sql"), nil
}