
	use DBI;

	sub addslashes($)
	{
		my($s)=@_;
		$s =~ s/'/''/g;
		return $s;
	}

	# -- This are required functions, do NOT change the function names.

	sub ices_perl_initialize {
		$db_name = 'phpgroupware_dev';
		$db_host = 'localhost';
		$db_user = 'root';
		$db_pass = '';

		$db = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",$db_user,$db_pass);
		return 1;
	}

	# Function called to shutdown your python enviroment.
	# Return 1 if ok, 0 if something went wrong.
	sub ices_perl_shutdown {
		$db->disconnect();
		return 1;
	}

	# Function called to get the next filename to stream.
	# Should return a string.
	sub ices_perl_get_next {
		my($sh,@r,@r2,$file,@files);

		# First, check the status of commercials
		$sh = $db->prepare("select config_value from phpgw_config where config_name='run_commercials' and config_app='dj'");
		$sh->execute();
		@r = $sh->fetchrow_array();
		$commercial_freq = $r[0];

		if ($commercial_freq)
		{
			$sh = $db->prepare("select config_value from phpgw_config where config_name='run_commercials' and config_app='dj'");
			$sh->execute();
			@r = $sh->fetchrow_array();
			$run_commercials = $r[0];

			$sh = $db->prepare("select config_value from phpgw_config where config_name='songs_until_commercials' and config_app='dj'");
			$sh->execute();
			@r = $sh->fetchrow_array();
			$songs_until_commercials = $r[0];

			$sh = $db->prepare("select config_value from phpgw_config where config_name='total_commercials_run' and config_app='dj'");
			$sh->execute();
			@r = $sh->fetchrow_array();
			$total_commercials_run = $r[0];

			if ($songs_until_commercials != 0 && $total_commercials != 1)
			{
				$db->do("update phpgw_config set config_value='" . ($songs_until_commercials - 1) . "' where config_name='songs_until_commercials' and config_app='dj'");
			}

			# Are we supposed to run a commercial now ?
			if ($songs_until_commercials == 0)
			{
				if ($total_commercials_run == 0 && $total_commercials != 1)
				{
					$db->do("update phpgw_config set config_value=config_value+1 where config_name='total_commercials_run' and config_app='dj'");
				}
				elsif ($total_commercials_run > $total_commercials)
				{
					$db->do("update phpgw_config set config_value='0' where config_name='total_commercials_run' and config_app='dj'");
					$db->do("update phpgw_config set config_value='$run_commercials' where config_name='songs_until_commercials' and config_app='dj'");
				}
				else
				{
					$db->do("update phpgw_config set config_value='$run_commercials' where config_name='songs_until_commercials' and config_app='dj'");
     			}

				$sh = $db->prepare("SELECT COUNT(*) FROM phpgw_dj_songlist WHERE song_cycle_status='A' AND song_status='C'");
				$sh->execute();

				@r = $sh->fetchrow_array();
				$sh->execute();

				if (! $r[0])
				{
					$db->do("UPDATE phpgw_dj_songlist SET song_cycle_status='C'");
				}

				$sh = $db->prepare("SELECT song_name FROM phpgw_dj_songlist WHERE song_cycle_status='A' AND song_status='C'");
				$sh->execute();
				while (@r = $sh->fetchrow_array())
				{
					push(@files,$r[0]);
				}

				srand();
				$file = $files[int rand @files];
				$db->do("update phpgw_dj_songlist set song_cycle_status='P' where song_name='" . addslashes($file) . "'");
			}

		}

		# Next, check the que
		$sh = $db->prepare("select min(que_id) from phpgw_dj_que");
		$sh->execute();

		@r = $sh->fetchrow_array();
		$sh->execute();
	   if ($r[0])
	   {
			$sh2 = $db->prepare("select * from phpgw_dj_que where que_id='" . $r[0] . "'");
			$sh2->execute();

			@r2 = $sh2->fetchrow_array();
			$sh2->execute();

	   	$file = $r2[1];

			$sh2 = $db->do("delete from phpgw_dj_que where que_id='" . $r2[0] . "'");

	   }

		if ($file)
		{
			return($file);
		}
		else
		{
			# Next make sure there is something that hasn't already been played
			$sh = $db->prepare("SELECT COUNT(*) FROM phpgw_dj_songlist WHERE song_cycle_status='A' AND song_status='A'");
			$sh->execute();

			@r = $sh->fetchrow_array();
			$sh->execute();

			if (! $r[0])
			{
				$db->do("UPDATE phpgw_dj_songlist SET song_cycle_status='A'");
			}

			$sh = $db->prepare("SELECT song_name FROM phpgw_dj_songlist WHERE song_cycle_status='A' AND song_status='A'");
			$sh->execute();
			while (@r = $sh->fetchrow_array())
			{
				push(@files,$r[0]);
			}

			srand();
			$file = $files[int rand @files];
			$db->do("update phpgw_dj_songlist set song_cycle_status='P' where song_name='" . addslashes($file) . "'");

			return($file);
		}
	}

	# Function used to put the current line number of
	# the playlist in the cue file. If you don't care
	# about cue files, just return any integer.
	# It should, however, return 0 the very first time
	# and then never 0 again. This is because the metadata
	# updating function should be delayed a little for
	# the very first song, because the icecast server may
	# not have accepted the stream yet.
	sub ices_perl_get_current_lineno
	{
		if ($first_run)
		{
			return 0;
			$first_run = True;
		}
		else
		{
			return 1;
		}
	}
