#!/usr/bin/perl -I/usr/depot/lib/perl5
# See below for documentation.

require 5.000;
use Net::PSYC;
use Pod::Usage qw( pod2usage );

my $noargs = $#ARGV == -1;
use Getopt::Std;
getopt('tbm');

if ($opt_h) {
	pod2usage;
	exit;
}

$binder = $opt_b || 'psyc://:0d/';	# get yourself any udp port on local ip
					# talk to local psycamp (default)
$target = $opt_t || 'psyc://localhost:1144d/';
$method = $opt_m || '_request_execute';	# commands are kind requests
					# for execution in PSYC

bind_uniform( $binder );
print "sendmsg '$target', '$method',\n" if $opt_v;

if ($opt_p || $0 =~ /play/i) {
        use Cwd qw(chdir);  # maintains PWD in ENV
        print STDERR $ENV{'PWD'}, " = PWD\n" if DEBUG & 1;
	$opt_p = 1;
}

if ($#ARGV >= 0) {
	$_ = join( ' ', @ARGV );
	&go( $_ );
} else {
	print STDERR <<X if $noargs and -t STDIN;
*** Insert commands now. Each line will be sent as a command
*** to -t $target using the -m $method.
*** Try -h for help or 'perldoc psyccmd' for a manual.

X
	while(<STDIN>) {
		chomp;
		&go($_);
	}
}
sleep 4;
exit;


sub go {
	$_ = shift;
	if ($opt_p) {
		$_ = "$ENV{'PWD'}/$_" unless m§^/§ || m§^\w:/§;
		$_ = $opt_j ? "scan $_" : "open $_";
	}
	print "\t'$_'\n" if $opt_v;
	return if $opt_n;
	$rc = sendmsg ($target, $method, $_); # , { _kilroy => 'woz ere' } );
	die $rc if $rc;
}

__END__

=pod

=head1 NAME

psyccmd - a tool to remote control a PSYC entity or application

psycplay - a tool to remote control a PSYC music player

=head1 SYNOPSIS

 psyccmd [-t <uniform>] [-b <uniform>] [-m <method>] [<command>]
   or
 psycplay [-t <uniform>] [-b <uniform>] <file>

 Other options:
	-v	Be verbose.
	-q	Be quieter.
	-n	No action.

=head1 DESCRIPTION

This little thing sends _request_execute containing
what you provide on the command line (or with every
line of stdin) to a PSYC entity given by option,
or to the local port 1144 which is the default for
the jukebox server psycamp.

=head1 CAVEATS

As long as Net::PSYC does not provide any encryption, you
should use this within a trustworthy local network or
as a localhost service.

=head1 AUTHORS

carlo von lynX.

=head1 COPYRIGHT

This program is free software, published under the Affero GNU Public
License. A disclaimer isn't necessary in my country, nor do I need
to mention the current year to assert a copyright.

