#!/usr/bin/perl
use strict;
use warnings;
#use List::MoreUtils qw(uniq);
use File::Copy;

my $favoritestream = 'http://currentstream1.publicradio.org:80/';
my $nowplaying;
my $nowplayingspaces;
my $nowplayingfilepath;
my $directorytocopysongto = '/home/superkuh/www/mp3';
my @mp3files;
my $npfile = '/home/superkuh/.nowplaying';
my $htmlfile = '/home/superkuh/www/np.html';
my $singlelinehtmlfile = '/home/superkuh/www/snp.html';
#unlink $htmlfile;
my @played;
my @unique;
my $totalnumtracks;
my $timesthistrackplayed;
my $previoussong = "";

my ($second, $minute, $hour, $dayofmonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = gmtime();
my $year = 1900 + $yearOffset;
$month++;
$month = sprintf("%02d",$month);
$hour = sprintf("%02d",$hour);
$minute = sprintf("%02d",$minute);
$dayofmonth = sprintf("%02d",$dayofmonth);
# print "\nYear: $year, Month: $month, Day: $dayofmonth, Time: $hour:$minute\n";


chdir("/home/superkuh/tests/");

open (my $np, "<", "$npfile") or die "Can't open $npfile to read.\n$!\n";
while((defined (my $line = <$np>))){
	push(@played, $line);
}
close $np or die "can't close $npfile for some fucking reason.\n$!";

$nowplaying = `cd /home/superkuh/tests/; /usr/bin/audtool --current-song`;
chomp $nowplaying;
$nowplayingfilepath = `cd /home/superkuh/tests/; /usr/bin/audtool --current-song-filename`;
chomp $nowplayingfilepath;
#my $OK_CHARS='-a-zA-Z0-9_.@\s,';
#$nowplaying =~ s/[^$OK_CHARS]//go;
$nowplayingspaces = $nowplaying;
$nowplayingspaces =~ s/\s/%20/g;
$nowplayingspaces .= '.mp3';

# print "$nowplayingfilepath eq $nowplayingspaces eq $favoritestream";

if ($nowplayingfilepath =~ /http:\/\//i) {
#if ($nowplayingfilepath eq $favoritestream) {
#	$nowplayingspaces = $favoritestream;
	$nowplayingspaces = $nowplayingfilepath;
} else {
	$nowplayingspaces = 'http://superkuh.com/mp3/' . $nowplayingspaces;
}


# @unique = uniq(@played);
@unique = removesubsequentdupes(@played);
sub removesubsequentdupes {
	my @played = @_;
	my @unique;
	
	my $previoustrack = "";
	#my $index = 0;
	foreach my $track (@played) {
		if ($track eq $previoustrack) {
			next;
		} else {
			push(@unique, $track);
		}
		$previoustrack = $track;
	}
	return @unique;
}

$totalnumtracks = scalar(@unique);

open ($np, ">", "$npfile") or die "Can't open $npfile to write.\n$!\n";
foreach my $song (@unique) {
	chomp $song;
	print $np "$song\n";
	if ($song eq $nowplaying) {
		$timesthistrackplayed++;
	}
}
close $np or die "Can't close $npfile, so, uh...\n$!";


@unique = reverse @unique;
open (my $html, ">", "$htmlfile") or die "Can't open $htmlfile to write.\n$!\n";
open (my $shtml, ">", "$singlelinehtmlfile") or die "Can't open $singlelinehtmlfile to write.\n$!\n";
#print $shtml "<a href=\"$nowplayingspaces\">$nowplaying</a>";

print $shtml "<html><head><title>superkuh is listening to $nowplaying, $nowplayingspaces</title></head><body>\n";
print $shtml "$nowplaying";
close $shtml or die "can't close $shtml for some fucking reason.\n$!";

print $html "<html><head><title>superkuh is listening to $nowplaying, $nowplayingspaces</title></head><body>\n";
print $html "<h3>$month-$dayofmonth-$year @ $hour:$minute UTC I listened to: <a href=\"$nowplayingspaces\">$nowplaying</a></h3>\n";
print $html "<h5>$totalnumtracks entries listed, this track played $timesthistrackplayed times.</h5>";
print $html "<ul>\n";
foreach my $song (@unique) {
	chomp $song;

	if ($song eq $nowplaying) {
		if ($previoussong eq $song) {
				
		} else {
			print $html "<li><a href=\"$nowplayingspaces\">$song</a></li>\n";
		}
	} else {
		print $html "<li>$song</li>\n";
	}
	$previoussong = $song;
}
print $html "</ul>\n</body></html>";
close $html or die "can't close $html for some fucking reason.\n$!";

## This copies the mp3 file
# print "$nowplaying\n"; # :: $nowplayingfilepath\n";
if ($nowplaying =~ /No song playing/i) {
	die "no song playing: $nowplaying\n";
} else {
	push(@played, $nowplaying);
	@mp3files = <$directorytocopysongto/*>;
	foreach my $mp3 (@mp3files) {
		chomp $mp3;
		# print "Deleting mp3: $mp3\n";
		#unlink $mp3;
		print "$nowplaying =~ /$previoussong";
		unlink $mp3 if !($nowplaying =~ /$previoussong/i);
	} 

	copy("$nowplayingfilepath", "$directorytocopysongto/$nowplaying.mp3")
  	 or warn "Copy of \"$nowplayingfilepath\" to \"$nowplaying\" failed: $!";


}

splice( @mp3files );
splice( @played );
splice( @unique );

# http://www.superkuh.com/mp3/Jefferson%20Airplane%20-%20Best%20of%20-%2019%20-%20Wooden%20Ships.mp3
# http://www.superkuh.com/mp3/Jefferson%20Airplane%20-%20Best%20of%20-%2019%20-%20Wooden%20Ships.mp3



