]> git.ktnx.net Git - mpd-feeder.git/blob - bin/mpd-feeder
53c826578840f3900e08e14472539117a4ba6073
[mpd-feeder.git] / bin / mpd-feeder
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 use App::MPD::Feeder;
8 use App::MPD::Feeder::Options;
9 use Log::Any qw($log);
10 use Log::Any::Adapter Stderr => log_level => 'error';
11
12 my $feeder = Feeder->new();
13
14 if (@ARGV) {
15     my $cmd = shift @ARGV;
16
17     if ($cmd eq 'dump-config') {
18         die "dump-config command accepts no arguments\n" if @ARGV;
19
20         $feeder->opt->dump;
21         exit;
22     }
23
24     if ( $cmd eq 'add-unwanted-artist' ) {
25         die "Missing command arguments\n" unless @ARGV;
26         $feeder->set_db_needs_update(0);
27         for my $artist (@ARGV) {
28             if ( $feeder->db_add_unwanted_artist($artist) ) {
29                 $log->info("Artist '$artist' added to the unwanted list\n");
30             }
31             else {
32                 $log->warn("Artist '$artist' already in the unwanted list\n");
33             }
34         }
35         exit;
36     }
37
38     if ( $cmd eq 'del-unwanted-artist' ) {
39         die "Missing command arguments\n" unless @ARGV;
40         $feeder->set_db_needs_update(0);
41         for my $artist (@ARGV) {
42             if ( $feeder->db_del_unwanted_artist($artist) ) {
43                 $log->info("Artist '$artist' deleted from the unwanted list\n");
44             }
45             else {
46                 $log->warn("Artist '$artist' is not in the unwanted list\n");
47             }
48         }
49         exit;
50     }
51
52     if ( $cmd eq 'add-unwanted-album' ) {
53         die "NOT IMPLEMENTED\n";
54     }
55
56     if ( $cmd eq 'one-shot' ) {
57         die "one-shot command accepts no arguments\n" if @ARGV;
58
59         $feeder->queue_songs(undef, sub { exit });
60         $feeder->mpd->loop->run;
61     }
62     elsif ( $cmd eq 'single' ) {
63         die "single command accepts no arguments\n" if @ARGV;
64
65         $feeder->queue_songs(1, sub { exit });
66         $feeder->mpd->loop->run;
67     }
68     else {
69         die "Unknown command '$cmd'";
70     }
71 }
72
73 $feeder->connect_db;
74
75 for ( ;; ) {
76     $feeder->queue_songs( undef, sub { $feeder->run } );
77
78     $log->debug("Entering event loop. PID=$$");
79
80     my $result = $feeder->mpd->loop->run;
81     $log->trace( "Got loop result of " . ( $result // 'undef' ) );
82
83     if ('reload' eq $result) {
84         $log->notice("disconnecting");
85         $feeder->stop;
86
87         exec( "$0", '--config', $feeder->cfg_file, '--skip-db-update' );
88     }
89 }