#!/usr/local/bin/perl
#
# Copyright (C) 1996 Mark Boyns <boyns@sdsu.edu>
#
# Mailsound
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

use RPlay;
use Getopt::Long;

$opt_file = "$ENV{'HOME'}/.Mailsounds";
$opt_host = "localhost";
$opt_matches = 1;
GetOptions ('file=s', 'host=s');

@patterns = ();
@attributes = ();
$matches = 0;

open (CONF, $opt_file) || die "$opt_file: $!";
while (<CONF>)
{
    next if (/^\#/ || /^[ \t\n]/);
    chop;
    ($pattern, $args) = split (/\t+/);
    push (@patterns, $pattern);
    local (%attrs);
    foreach (split (/[\t ]+/, $args))
    {
	($name, $value) = split ('=');
	$attrs{$name} = $value;
    }
    push (@attributes, \%attrs);
}
close (CONF);

@lines = <STDIN>;
foreach $i (0 .. $#patterns)
{
    foreach (@lines)
    {
	last if /^\n/;
	if (/$patterns[$i]/)
	{
	    $hash_ref = $attributes[$i];
	    %attrs = %$hash_ref;

	    $host = $attrs{'host'};
	    $host = $opt_host unless $host;
	
	    foreach $h (split (':', $host))
	    {
		$rp = new RPlay;
		$rp->connect ($h);
		$rp->play ($hash_ref);
		$rp->disconnect ();
	    }

	    $matches++;
	    last;
	}
    }

    last if $matches == $opt_matches;
}

exit 0;
