#!/usr/bin/perl
# file: pbsqsc		G. Moody	22 February 2012
#
# _____________________________________________________________________________
# PhysioBank simple query search client
#
# This program submits its command line arguments as a query to the PhysioBank
# Simple Query Server (pbsqsd) and writes the server's reply to stdout.

use IO::Socket;

# collect the command line arguments into a space-separated string
$query = join " ", @ARGV;

# open a connection to pbsqs (port 9967)
$socket = IO::Socket::INET->new(PeerAddr => 'localhost',
				PeerPort => 9967,
				Proto    => 'tcp',
				Type     => SOCK_STREAM)
    or die "Couldn't connect to localhost:9967 : $@\n";

# wait for the server's prompt
do {
    $answer = <$socket>;
} until ($answer =~ m/^pbs>/);

# send the query
print $socket $query . "\n";

# copy the server's reply to stdout until it writes another prompt
$answer = <$socket>;
until ($answer =~ m/^pbs>/) {
    print $answer;
    $answer = <$socket>;
}

# finished, close the socket and exit
close $socket;
