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

#use strict;
#use diagnostics;

use Net::FTP;
use Debian::DpkgFtp;

# 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="/pub/debian";
$::distribs="dists/stable/main dists/stable/non-free dists/stable/contrib";
my $arch=`dpkg --print-installation-architecture`;
$arch='i386' if $?;
chomp $arch;
$::dldir="debian";

# authenticated FTP proxy
$::use_auth_proxy = 0;
$::proxyhost = "";
$::proxylogname = $logname;
$::proxypassword = "";

# get info from control file if available
-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).

Supply "?" as a password to be asked each time you connect.

Eg:      ftp site: ftp.debian.org
          passive: y
         username: anonymous
         password: $email
          ftp dir: /debian
    distributions: dists/stable/main dists/stable/contrib
     download dir: debian

If you are using a non-us mirror, you may want to try something like
"../debian-non-US/hamm" or "dists/stable/non-us" in distributions.

You may have to use an authenticated FTP proxy in order to reach the
FTP site:

Eg:  use auth proxy: y
              proxy: proxy.isp.com
      proxy account: $::proxylogname
     proxy password: ?
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 your password,
or "?" if you want dpkg-ftp to 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 "";

$::use_auth_proxy = yesno($::use_auth_proxy ? "y":"n", "Go through an authenticated FTP proxy");

if ($::use_auth_proxy) {
    print "\nEnter proxy hostname [$::proxyhost]: ";
    my $ans=<STDIN>;
    chomp $ans;
    $::proxyhost = $ans if $ans ne "";

    print "\nEnter proxy logname [$::proxylogname]: ";
    my $ans=<STDIN>;
    chomp $ans;
    $::proxylogname = $ans if $ans ne "";

    print "\nEnter proxy password [$::proxypassword]: ";
#    system("stty", "-echo");
    my $ans=<STDIN>;
    chomp $ans;
#    print "\n";
#    system("stty", "echo");
    $::proxypassword = $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() {
    $ftp = do_connect ($::ftpsite,$::username,$::password,$::ftpdir,$::passive,
		       $::useproxy,$::proxyhost,$::proxylogname,$::proxypassword);

    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 - ";
    if ($@ eq "connect") {
	print "config was untested\n";
    } else {
	print "$@\n";
    }
    $exit = 1;
};

# output new vars file
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";
print VARS "\$use_auth_proxy=$::use_auth_proxy;\n";
print VARS "\$proxyhost='$::proxyhost';\n";
print VARS "\$proxylogname='$::proxylogname';\n";
print VARS "\$proxypassword='$::proxypassword';\n";
print VARS "1;\n";		      # necessary for "do vars" to succeed at any time
close VARS;
system ("chmod", "600", "$vardir/methods/ftp/vars");
if($problem) {
    print "Press return to continue\n";
    <STDIN>;
}
exit $exit;
