# Copyright (C) 2005  Joshua Hoblitt
#
# $Id: Server.pm,v 1.2 2005-10-12 02:11:10 jhoblitt Exp $

package PS::IPP::PSFTP::Server;

use strict;
use warnings FATAL => qw( all );

use vars qw( $VERSION );
$VERSION = '0.01';

use CGI;

sub run
{
    my ($class, $cgi) = @_;

    $cgi ||= CGI->new;

    my $mode = $cgi->url(-path_info=> 1, -relative=> 1);

    if ($mode =~ /files_available/) {
        $class->files_available($cgi);
    } elsif ($mode =~ /file_received/) {
        $class->file_received($cgi);
    } else {
        $class->error($cgi, "invalid mode");
    }
}

sub files_available
{
    my ($class, $cgi) = @_;

    my $recipient = $cgi->param('recipient')
        or $class->error($cgi, "recipient not specified");

    print $cgi->header('text/plain'),
          $class->list_files($recipient);
}

sub file_received
{
    my ($class, $cgi) = @_;

    my $recipient = $cgi->param('recipient')
        or $class->error($cgi, "recipient not specified");
    my $id = $cgi->param('id')
        or $class->error($cgi, "id not specified");
    my $discarded = $cgi->param('discarded')
        or $class->error($cgi, "discarded not specified");

    $class->set_received($recipient, $id, $discarded);

    print $cgi->header(-status=> 200),
}

sub error
{
    my ($class, $cgi, $message) = @_;

    print $cgi->header(-status=> 400),
          $message, "\n",
          $cgi->end_html();
}

sub get_available
{
    my ($class, $recipient) = @_;

    return <<END;
2005-08-15|12:00:01|telescope_2005-08-15T12:00:01|metadata||8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-15T12:00:01
2005-08-15|12:10:01|telescope_2005-08-15T12:10:01|metadata||8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-15T12:10:01
2005-08-15|12:20:01|telescope_2005-08-15T12:20:01|metadata||8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-15T12:20:01
END
}

sub set_received
{
}

1;

__END__
