#!/usr/bin/perl 
# -*-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="";
$::passive=1;
$::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-installation-architecture`;
$arch='i386' if $?;
chomp $arch;
$::dldir="debian";

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

chdir "$vardir/methods/ftp";
if (! -d "debian") {
    mkdir "debian", 0755;
}
# get info from user 
print <<EOM;

You must supply an ftp site, use of passive mode, username, password,
path to the debian directory,list of distributions you are interested
in and place to download the binary package files to (relative to
/var/lib/dpkg/methods/ftp).

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


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

if($::passive) {
    print "\nUse passive mode [y]: ";
} else {
    print "\nUse passive mode [n]: ";
}
$ans=<STDIN>;
chomp $ans;
if($ans ne "") {
    if($ans =~ /^[Yy]/) {
	$::passive=1;
    } else {
	$::passive=0;
    }
}

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

print <<EOM;

If you are using anonymous ftp to retrieve files, enter your email
address for use as a password.  Otherwise enter "?" and dpkg-ftp will
prompt you each time.

EOM

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 <<EOM;

Note: order here is important.  Package files are scanned in order so
later distributions will override earlier ones.

So put stable before unstable.

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

print "\nEnter directory to download binary package files to\n(relative to /var/lib/dpkg/methods/ftp/)\n";
DLLOOP: while (1) {
    print "[$::dldir]: ";
    $ans=<STDIN>;
    chomp $ans;
    if( $ans =~ m#/$# ) { $ans = substr($ans, 0, -1); }
    $::dldir = $ans if $ans ne "";
    if( ! -d $::dldir ) {
	print "$::dldir is not a directory.\n";
    } else {
	last DLLOOP;
    }
} 
my $problem=0;
my $exit=0;


sub download() {
    print "Connecting to $::ftpsite...\n";
    my $ftp = Net::FTP->new($::ftpsite, Passive => $::passive);
    if(!$ftp->ok) { print "Failed to connect\n"; die "error"; }
#    $ftp->debug(1);
    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";
#	if(!$ftp->pasv()) { print $ftp->message . "\n"; die "error"; }
	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: Could not 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 "Could not open file $vardir/methods/ftp/vars: $!, stopped";
print VARS "\$ftpsite='$::ftpsite';\n";
print VARS "\$passive='$::passive';\n";
print VARS "\$username='$::username';\n";
print VARS "\$password='$::password';\n";
print VARS "\$ftpdir='$::ftpdir';\n";
print VARS "\$distribs='$::distribs';\n";
print VARS "\$dldir='$::dldir';\n";
close VARS;
if($problem) {
    print "Press return to continue\n";
    <STDIN>;
}
exit $exit;
