#!/usr/bin/perl 
# -*-perl-*-

BEGIN { unshift(@INC, '/usr/lib/dpkg/methods/ftp/perl') }
#use strict;
#use diagnostics;

use Net::FTP;

# deal with arguments
my $vardir = $ARGV[0];
my $method = $ARGV[1];
my $option = $ARGV[2];

if ($option eq "manual") {
    print "Manual package installation.\n";
    exit 0;
}
#print "vardir: $vardir, method: $method, option: $option\n";

$::ftpsite="";
$::username="anonymous";
my $logname=`logname`;
chomp $logname;
my $hostname=`hostname -f`;
chomp $hostname;
my $email=$logname . '@' . $hostname;
$::password=$email;
$::ftpdir="/debian";
$::distribs="stable non-free contrib";
my $arch=`dpkg --print-architecture`;
$arch='i386' if $?;
chomp $arch;

# get info from control file
-f "$vardir/methods/ftp/vars" and do "$vardir/methods/ftp/vars";

# get info from user 
print <<EOM;

You must supply an ftp site, username, password, path to the debian
directory and list of distributions you are interested in.

Eg:      ftp site: ftp.debian.org
         username: anonymous
         password: $email
          ftp dir: /debian
    distributions: stable non-free contrib

EOM


print "\nEnter ftp site [$::ftpsite]: ";
my $ans=<STDIN>;
chomp $ans;
$::ftpsite = $ans if $ans ne "";

print "\nEnter username [$::username]: ";
$ans=<STDIN>;
chomp $ans;
$::username = $ans if $ans ne "";

print "\nNOTE: if you do not want the password stored between sessions enter \"?\"\n";
print "Enter password [$::password]: ";
$ans=<STDIN>;
chomp $ans;
$::password = $ans if $ans ne "";

print "\nEnter debian directory [$::ftpdir]: ";
$ans=<STDIN>;
chomp $ans;
$::ftpdir = $ans if $ans ne "";

print "\nEnter space seperated list of distributions to get\n";
print "[$::distribs]: ";
$ans=<STDIN>;
chomp $ans;
$::distribs = $ans if $ans ne "";

my $problem=0;
my $exit=0;

sub download() {
    print "Connecting to $::ftpsite...\n";
    my $ftp = Net::FTP->new($::ftpsite);
    if(!$ftp->ok) { print "Failed to connect\n"; die "error"; }
    print "Login as $::username...\n";
    my $pass = $::password;
    if($pass eq "?") {
	print "Enter password for ftp: ";
	system("stty", "-echo");
	$pass = <STDIN>;
	chomp $pass;
	print "\n";
	system("stty", "echo");
    }
    if(!$ftp->login($::username, $pass)) { print $ftp->message() . "\n"; die "error"; }
    print "Setting transfer mode to binary...\n";
    if(!$ftp->binary()) { print $ftp->message . "\n"; die "error"; }
    print "Cd to $::ftpdir...\n";
    if(!$ftp->cwd($::ftpdir)) { print $ftp->message . "\n"; die "error"; }

    my @dists = split(/ +/, $::distribs);
    
    my $dist;
    foreach $dist (@dists) {
	my $dir = "$dist/binary-$arch";
	print "Checking $dir...\n";
	my @dirlst = $ftp->ls("$dir/");
	my $got_pkgfile = 0;
	my $line = "";
	foreach $line (@dirlst) {
	    if($line =~ /Packages/) {
		$got_pkgfile=1;
	    }
	}
	if( !$got_pkgfile) {
	    print "Warning: Couldn't find a Packages file in $dir\n",
	    "This may not be a problem if the directory is a symbolic link\n";
	    $problem=1;
	}
    }
    print "Closing ftp connection...\n";
    $ftp->quit();
}

# download stuff (protect from ^C)
print "\nUsing FTP to check directories...(stop with ^C)\n\n";
eval {
    local $SIG{INT} = sub {
	die "Interrupted!\n";
    };
    download();
};
if($@) {
    print "FTP ERROR\n";
    $exit = 1;
};

# output new vars file
unlink <$vardir/methods/ftp/Packages*>;

open(VARS, ">$vardir/methods/ftp/vars") or 
die "Couldn't open file $vardir/methods/ftp/vars: $!, stopped";
print VARS "\$ftpsite='$::ftpsite';\n";
print VARS "\$username='$::username';\n";
print VARS "\$password='$::password';\n";
print VARS "\$ftpdir='$::ftpdir';\n";
print VARS "\$distribs='$::distribs';\n";
close VARS;
if($problem) {
    print "press return to continue\n";
    <STDIN>;
}
exit $exit;
