#!/usr/bin/perl -w

use DBI;
use LWP::Simple;

chdir "/";

die unless (scalar @ARGV == 1);

$channel = $ARGV[0];

#if ($channel < 67) {
#    exit;
#}

my $dbh = DBI->connect( 'dbi:mysql:dbname=lamp;host=localhost',
                        'lamp', '_password_' ) or die "$DBI::errstr";
my $meq = $dbh->prepare( "select channel_number from channels where (username = '' and channel_number = $channel);" );
$meq->execute();

$free = 0;
while ( $meq->fetchrow_array ) {
    $free = 1;
}

exit unless $free;

do {
    $page = get("http://127.0.0.1/random-song.php");
    die unless (defined $page);
} while ($page =~ /nigger|fuck|\bshit\b|\bcunt\b|\bass\b/i);

# don't play songs with offensive titles on own initative

# if people request them, fine

($filename, $id, $name) = split /\n/, $page;

print STDERR "FILENAME:: $filename\n";

$name =~ s/^ *//g;
$name =~ s/ *$//g;

$meq = $dbh->prepare( "update channels set now_playing = $id where channel_number = $channel;" );
$meq->execute();

exec '/usr/local/bin/lamp-play', $filename, $channel, '', $name, '0', '0';

