#!/usr/bin/perl

my $VERSION = "0.95 prerelease";
print("Penguin Code Sending Client version $VERSION\n");

use Getopt::Std;
getopts('h:p:vZ:f:');

if (! ($opt_h && $opt_f)) { # require that they enter a hostname and filename
    print("Usage: penguind -h HOSTNAME -f FILENAME [-p PORTNUM]" .
          "[-Z PASSWORD] [-v]\n");
    print("-h HOSTNAME      sets the penguin server to send to\n");
    print("-p PORTNUM       sets the port number to send to\n");
    print("-f FILENAME      sets the filename to read and send code from\n");
    print("-v               sets the verbosity level to high\n");
    print("-Z PASSWORD      set password noninteractively; INSECURE!  DO NOT USE!\n");
    exit(0);
} else {
    $targethost = $opt_h;
    $codefile   = $opt_f;
}

if ($opt_v) {
    $verbose = 1;
}

if ($opt_Z) {
    print("(received your password insecurely!)\n") if $verbose;
    $PGP_Password = $opt_Z;
} else {
    print("Type your PGP Password (echo disabled with 'stty -echo'):\n");
    system("stty -echo");
    chop($PGP_Password = <STDIN>);
    system("stty echo");
    print("thanks (echo back on)\n");
}

use Penguin;
use Penguin::Rights;
use Penguin::Frame::Code;
use Penguin::Frame::Data;
use Penguin::Wrapper::PGP;
use Penguin::Compartment;
use Penguin::Channel::TCP::Client;

open(CODEFILE, "< $codefile")
         or die "can't open $codefile to send it";
{local $/ = undef; $codetosend = <CODEFILE>}
close(CODEFILE);


$mychannel = new Penguin::Channel::TCP::Client Peer => $targethost,
                                               Port => $portnumber;
                                             
$mychannel->open();
print("connected to server.\n") if $verbose;

$frame = new Penguin::Frame::Code Wrapper => 'Penguin::Wrapper::PGP';

assemble $frame Password => $PGP_Password,
                Text     => $codetosend,
                Title    => "untitled program",
                Name     => "Joe Random User";

# print $frame->contents();     # uncomment to see what the frame looks like
putframe $mychannel Frame => $frame;

$frame1 = getframe $mychannel; # expecting a data frame

$results = $frame1->disassemble(Password => $PGP_Password);
print("results from the code executed on $targethost:\n") if $verbose;
print("----------------------------------------------\n") if $verbose;
print($results);
