#!/usr/local/bin/perl5

# start multiple mpds on localhost

use Sys::Hostname;

$num_to_start = shift ARGV  or  die "usage: $0 num_mpds_to_start";

my $myhostname = hostname();
$myhostname =~ s/(.*)\..*\..*\..*/$1/;

# print "$myhostname \n";

%port = ();
$rc = system("mpd &");
for $i (0..$num_to_start-2)
{
    # print "$i\n";
    open(LS,"ls -1 /tmp/mpd.client_* | ");
    @x = <LS>;

    for $x (@x)
    {
	chomp $x;
	$x =~ m/\/tmp\/mpd.client_.*_(.*)/;
	$port = $1;
	# print "port=:$port:\n";
	if (!exists($port{$x}))
	{
	    $port{$x} = 1;
	    $rc = system("mpd -h $myhostname -p $port &");
	    # print "mpd -h $myhostname -p $port\n";
	}
    }
}
