#!/usr/bin/perl

# Requires playerctl binary in $PATH
# If $instance is specified in i3blocks, playerctl will attempt to get data from this player

use Env qw(BLOCK_INSTANCE);

my @metadata = ();
my $player_arg = "";

if ($ARGV[0] =~ "--player=") {
    $player_arg = $ARGV[0];
    $player_arg =~ s/\s+//g;
}

# Check playback status and set appropriate colour
$_ = qx(playerctl status);
if (not m/Playing/) {
  print("<span color='#586e75'>");
}
else {
 print("<span>");
}

# Obtain & format metadata from playerctl
my $artist = qx(playerctl $player_arg metadata artist);
push(@metadata, $artist) if $artist;

my $title = qx(playerctl $player_arg metadata title);
$title =~ s/(\s|\s\()[Ff]([et]at[. ].*|t.*)//;
$title =~ s/&/&amp;/;
push(@metadata, $title) if $title;

# Print stuff
print(join(" - ", @metadata)) if @metadata;
print("</span>");
