#!/usr/bin/perl -w
######################################################################
# (c) Michael Schilli 1999
######################################################################

###
### ptkftp.pl - Graphisches ftp Tool
###

BEGIN { push(@INC, "../tk", "../oo"); };

print @INC;
use Net::FTP;
use Tk;
use Path;
use Process;


# Variablen
my $host     = "localhost";
my $email    = "me\@mysite";
my $login    = "anonymous";
my $dir      = "/";
my $file     = "..";

# Ftp initialisieren
my $ftp = Net::FTP->new($host);

$ftp->login($login, $email);


# Tk Widgets
my $top = MainWindow->new();
my $topframe = $top->Frame();

my $dirframe = $topframe->Frame();
$dirframe->Label(-text => "Directory:")->pack(-side => "left");
$dirframe->Entry(-textvariable => \$dir)->pack(-side => "left", 
					       -expand => "yes",
					       -fill => "x");

my $hostframe = $topframe->Frame();
$hostframe->Label(-text => "Host:")->pack(-side => "left");
$hostframe->Entry(-textvariable => \$host)->pack(-side => "left");

my $fileframe = $top->Frame();
$fileframe->Label(-text => "File:")->pack(-side => "left");
$fileframe->Entry(-textvariable => \$file)->pack(-side => "left",
                                                 -expand => "yes",
						 -fill => "x");

my $listbox = $top->ScrlListbox(-label => "", -width  => 40);

$listbox->bind("<Double-Button-1>" => 
	       sub {  my $sel = $listbox->Getselected;
		      if($sel =~ /\/$/ || $sel eq "..") {
		          scandir($ftp, $sel, \$dir, \$file, $listbox);
                      } else {
			  getfile($ftp, $sel, \$file);
			  $listbox->selection("clear", 0, "end");
                      } 
                   });

$listbox->bind("<Button-1>" => 
	       sub { $file = getextended($listbox);
		   });

my $exitbutton   = $top->Button("-text" => "Exit", 
				"-command" => sub { $ftp->quit(); exit; });

# Alles packen
$topframe->pack(-fill => "x");
$dirframe->pack(-side => "left", -expand => "yes", -fill => "x");
$hostframe->pack(-side => "left");
$fileframe->pack(-fill => "x");
$listbox->pack("-fill"   => "both", "-expand" => "yes");
$exitbutton->pack();

scandir($ftp, $dir, \$dir, \$file, $listbox);
$file = getextended($listbox);

MainLoop;

###
### In neues Verzeichnis wechseln und Listbox aktualisieren
###
sub scandir {
    my $ftp     = shift;
    my $dir     = shift;
    my $dirref  = shift;
    my $fileref = shift;
    my $listbox = shift;
    my $i;
    my $shortfile;

    $listbox->delete(0, "end");            # Listbox leeren

    $listbox->insert("end", "..");         # 'cd up' Emulation

    $ftp->cwd("$dir") && ($$dirref = Path::cd($$dirref, $dir));

    foreach $i (@files = $ftp->dir()) {
       $i =~ /^[^d\-]/ && next;                # nur Files oder Directories
       $i =~ /^d/ && ( $i .= "/" );            # "/" an directories anhängen
       ($i =~ /(\S+)$/) && ($shortfile = $1);   # Filename
       $listbox->insert("end", "$shortfile");
    }
    $listbox->selection("set", 1);
    $$fileref = getextended($listbox);
}


sub getextended {
    my $listbox = shift;
    my $file;

    my $sel = $listbox->Getselected;
    ($file) = grep(/\s+$sel$/, @files); 
    $file = "" unless defined $file;
    $file =~ s/\s+/ /g;
    $file;
}

sub looper {
    after(1000, \& looper);      
}

sub getfile {
    my $ftp     = shift;
    my $file    = shift;
    my $textref = shift;

    my $oldtext = $$textref;

    my $pr = Process->new();

    $pr->start(sub { $ftp->get("$file"); });

    my $bytes = 0;
    while($pr->poll()) {
	$$textref = "$file: $bytes";
        $fileframe->update();
	sleep(1);
	$bytes = (stat("$file"))[7];
    }
    $$textref .= " DONE"
    #$$textref = $oldtext;
}
