#!/usr/bin/perl

$DIR = $ENV{'ROOT'} || '/home/tom/station break';
$MIXERDEV = $ENV{'MIXERDEV'} || '/dev/mixer';
$AUDIODEV = $ENV{'AUDIODEV'} || '/dev/dsp';

$| = 1;
@RECENT = ();

print "[pid $$ start]\n";

chdir $DIR;
srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
sponsor_shuffle();

while (1) {
	@localtime = localtime;
	while ((localtime)[2] >= 1 && (localtime)[2] < 8)
	{
		play (choose ('overnight'));
		sponsor_shuffle ();
	}

	while ((localtime)[2] == 13 || (localtime)[2] == 14 ||
	       (localtime)[2] == 18 || (localtime)[2] == 19) {
		# 1-3pm, 6-8pm

		# words
		play (choose ('words'));
		check_restart();

		# at least one music track
		play (choose ('music-day'));

		if (time - $last_id_time > 600) {
			play (choose ('stationid'));
			$last_id_time = time;
		}

		if (time - $last_sponsor_time > 1800 || $ENV{'TESTONLY'}) {
			play (shift (@sponsor)) if @sponsor;
			play (shift (@sponsor)) if @sponsor;
			$last_sponsor_time = time;
		}
	}

	# play music until the top of the hour

	$this_hour = (localtime)[2];
	while ($this_hour == (localtime)[2]) {
		if ($this_hour >= 21 || $this_hour <= 2) {
			play (choose ('music-night music-day'));
		} else {
			play (choose ('music-day'));
		}
	}

	# play a station id, unless I played one less than 10 minutes ago
	if (time - $last_id_time > 600) {
		play (choose ('stationid'));
		$last_id_time = time;
		if (time - $last_sponsor_time > 1800) {
			play (shift (@sponsor)) if @sponsor;
			play (shift (@sponsor)) if @sponsor;
			$last_sponsor_time = time;
		}
	}

	check_restart();
}

sub check_restart
{
	if (-e "$ENV{'HOME'}/stop" && unlink ("$ENV{'HOME'}/stop")) {
		print "[pid $$ stop]\n";
		exit 0;
	}
}

sub is_it_stationid_time
{
	(localtime)[1] < 10 || (localtime)[1] >= 50;
}

sub choose
{
	my ($sections, $glob) = @_;
	if (!defined ($glob)) { $glob = '*.mp3'; }
	chomp (@mp3 = `find $sections -type f -name '$glob'`);
	my ($tries) = 0;
	while (++$tries < 7)
	{
		$n = int(rand($#mp3 + 1));
		if ($n > $#mp3)
		{
			$n = $#mp3;
		}
		if (!grep ($_ eq $mp3[$n], @RECENT))
		{
			last;
		}
	}
	unshift @RECENT, $mp3[$n];
	splice @RECENT, 5;
	return $mp3[$n];
}

sub play
{
	my ($mp3) = @_;
	print ($mp3, "\n");
	if (!defined ($ENV{'TESTONLY'})) {
		system ('mpg123-auto', '-q', '-a', $AUDIODEV, $mp3);
		# select (undef, undef, undef, 1.5);
	}
}

sub sponsor_shuffle
{
	if ($lastshuffleday == (localtime)[3]) {
		return;
	}
	print STDERR "... shuffling sponsorships ...\n";
	$lastshuffleday = (localtime)[3];
	chomp (@sponsor = `find -s sponsor -type f -name \*.mp3`);
	for (0..$#sponsor) {
		splice @sponsor, 1 + rand $#sponsor, 0, (shift @sponsor);
	}
	my $bad = 1;
	my $tries = 0;
	while ($bad && ++$tries < 100) {
		$bad = 0;
		for (0..$#sponsor) {
			my ($id) = $sponsor[$_] =~ m:/(\d+):;
			my $prev;
			for ($prev = ($_-4<0 ? 0 : $_-4); $prev<$_; $prev++) {
				my ($previd) = $sponsor[$prev] =~ m:/(\d+):;
				if ($previd == $id) {
					push (@sponsor,
					      splice (@sponsor, $_, 1));
					$bad = 1;
				}
			}
		}
		if ($bad) {
			unshift (@sponsor, pop @sponsor);
		}
	}
	print STDERR "... took $tries tries\n";
	print STDERR (map ("... $_\n", @sponsor));
}
