#!/usr/local/bin/perl

require Net::FTP;
use Net::Ping;
use strict;

my $server;
my $port;
my @lsdir;
my $current;
our $ftp = undef;
our $cmd;
my $subcmd;
my $anon;
my $p;

system("clear");
print "----------------------------------------------------";
print "\n\n| >>>>>>>>>>>>>>!Welcome to PerlFTP!<<<<<<<<<<<<<< |\n\n";
print "----------------------------------------------\n";
print "  Server: n0ne		Port: n0ne \n";
print "----------------------------------------------------\n";
print "\nPlease enter the FTP Server you want to connect to: ";
$server = <STDIN>;
chomp $server;
system("clear");
print "----------------------------------------------------";
print "\n\n| >>>>>>>>>>>>>>!Welcome to PerlFTP!<<<<<<<<<<<<<< |\n\n";
print "----------------------------------------------\n";
print "  Server: $server	Port: n0ne  \n";
print "----------------------------------------------------\n";
print "\nPlease enter the Port of your FTP server: ";
$port = <STDIN>;
chomp $port;
$ftp = Net::FTP->new($server, 
					 Timeout => 30,
					 Port => $port);
if ($ftp == undef) {
	print "Server seems down... Lets try to ping it!\n\n";
	$p = Net::Ping->new();
	if ($p->ping($server)) {
		print "$server is alive!!";
		$p->close;
		sleep 3;
	} else {
		die "Yep, $server is down!  Quitting...\n\n";
	}
}

while (1) {
	layout();
	print "Would you like to login as 'Anonymous'? y/n: ";
	$anon = <STDIN>;
	if ($anon =~ /^y$/i) {
		$ftp->login()	or die "\nAnonymous login failed! Quitting... \n";
		layout();
		print "Anonymous login successful!\n";
		last;
	} elsif ($anon =~ /^n$/i) {
		layout();
		print "Please enter username: ";
		my $username = <STDIN>;
		chomp $username;
		layout();
		print "Please enter password: ";
		my $password = <STDIN>;
		chomp $password;
		print "\nLogin successful!\n\n";
		$ftp->login($username, $password)	or die "\n\nLogin failed! Quitting... \n";
		last;
	}
}

$current = $ftp->pwd();
print "\n\nYou are here: ", $current, "\n";
@lsdir = $ftp->dir();
print join ("\n",@lsdir), "\n\n";
#print "Command: ";
do {
	print "Command: ";
	$cmd = <STDIN>;
	chomp $cmd;
	command(split(/\s+/,$cmd));
	} while ($cmd !~ /^quit$/i);
$ftp->quit();
die "\n\n\nConnection terminated! Goodbye!\n\n";



sub command {
	my @args=@_;
	my $check;
	if ($args[0] =~ /^help$/i) {
		print "Help is not available yet!";
	} elsif ($args[0] =~ /^cw?d$/i ) {
		print "CWD command executed with argument: ", $args[1] ,"\n\n";
		$check = $ftp->cwd($args[1]);
		if ($check) {
		$current = $ftp->pwd();
		print "\n\nYou are here: ", $current ,"			on ", $server ,"\n\n";
		} else {
			print "Changing Directory failed!\n";
			}
	} elsif ($args[0] =~ /^dir$/i ) {
		@lsdir = $ftp->dir();
		print join ("\n",@lsdir), "\n";
	} elsif ($args[0] =~ /^pwd$/i ) {
		$current = $ftp->pwd();
		print "\nYou are here: ", $current ,"			on ", $server ,"\n";
	} elsif ($args[0] =~ /^ls$/i ) {
		@lsdir = $ftp->ls();
		print join ("\n",@lsdir), "\n";
	} elsif ($args[0] =~ /^get$/i ) {
		print "\nCopying file ", $server ,$current ,"/" ,$args[1] ,"   to   " ,$args[2] ,"\n\n";
		print "Copying... Please be patient while waiting! ;-)\n\n";
		$ftp->get($args[1], $args[2]);
		#system("clear");
		print "Copying finished!\n";
	} elsif ($args[0] =~ /^put$/i ) {
		print "\nCopying file " ,$args[1], "   to   ", $server ,$current ,"/" ,$args[2] ,"\n\n";
		print "Copying... Please be patient while waiting! ;-)\n\n";
		$ftp->put($args[1], $args[2]);
		print "Copying finished!\n";
	} elsif ($args[0] =~ /^mkdir$/i ) {
		$ftp->mkdir($args[1], $args[2]);
		print "Directory ", $args[1] ," created!\n";
	} elsif ($args[0] =~ /^rmdir$/i ) {
		$ftp->rmdir($args[1], $args[2]);
		print "Directory ", $args[1], " deleted!\n";
	} elsif ($args[0] =~ /^cdup$/i ) {
		$ftp->cdup();
		$current = $ftp->pwd();
		print "\nYou are here: ", $current ,"			on ", $server ,"\n";
	} elsif ($args[0] =~ /^type$/i ) {
		$ftp->type($args[1]);
		if ($args[1] =~ /^a$/i ) {
			print "Switching to file translation mode: ascii \n";
		} elsif ($args[1] =~ /^i$/i ) {
			print "Switching to file translation mode: binary \n";
		} elsif ($args[1] =~ /^e$/i ) {
			print "Switching to file translation mode: ebcdic \n";
		} elsif ($args[1] =~ /^l$/i ) {
			print "Switching to file translation mode: byte \n";
		}
	}
}





sub layout {
	system("clear");
	print "----------------------------------------------------";
	print "\n\n| >>>>>>>>>>>>>>!Welcome to PerlFTP!<<<<<<<<<<<<<< |\n\n";
	print "----------------------------------------------------\n";
	print "  Server: $server	Port: $port  \n";
	print "----------------------------------------------------\n\n";
	print "       You are connected and in command mode!\n\n\n";
}