# -*-perl-*-

$ftp_site = "";
$ftp_dir = "";
$ftp_configured = 0;
$ftp_series_file = "/tmp/series";
@ftp_spec = ();

sub ftp_init {
    require "/usr/lib/rhs/rpm/rpmftp.pl";

    if (! $net_up) {
	&make_net_up;
	if (! $net_up) {
	    &rhs_msgbox("Error",
<<EOM
>
The network must be properly configured for an
FTP install to work.
>
EOM
			, 60);
	    return 0;
	}
    }

    $ftp_configured = 0;

    if (! &configure_ftp) {
	return 0;
    }
    while (! &test_ftp) {
	if (! &rhs_menu("FTP Configuration",
<<EOM
>
FTP initialization failed.
>
The following error was returned:
>
$ftp_error
>
EOM
			, 60, 2,
			"Configure", "Reconfigure FTP",
			"Give Up", "Just forget it")) {
	    return 0;
	}
	if ($dialog_result eq "Give Up") {
	    return 0;
	}
	&configure_ftp;
    }

    $ftp_configured = 1;
    return 1;
}

sub test_ftp {

    &rhs_infobox("FTP Configuration",
<<EOM
>
Testing ftp://$ftp_site$ftp_dir
>
EOM
		 , 60);

    if (! &get_ftp_file("RedHat/base/series", $ftp_series_file)) {
	return 0;
    }

    return 1;
}

sub get_ftp_file {
    local ( $remote, $local ) = @_;

    return &ftp_file("ftp://$ftp_site$ftp_dir/$remote", $local);
}

sub get_ftp_dir {
    local ( $dir ) = @_;
    local ( @result );

    @result = &ftp_dir("ftp://$ftp_site$ftp_dir/$dir");
    return @result;
}

sub configure_ftp {

    if (! &rhs_menu("FTP Configuration",
<<EOM
Choose one of the following Official Red Hat Mirror sites.
EOM
		    , 75, 6,
		    "ftp://ftp.pht.com/pub/linux/redhat/ftp-install", "",
		    "ftp://ftp.caldera.com/pub/mirrors/redhat/ftp-install", "",
		    "ftp://ftp.cms.uncwil.edu/linux/redhat/ftp-install", "",
		    "ftp://ftp.wilmington.net/linux/redhat/ftp-install", "",
		    "ftp://ftp.lasermoon.co.uk/pub/distributions/RedHat/ftp-install", "",
		    "Other", ""
		    )) {
	return 0;
    }

    if ($dialog_result ne "Other") {
	$dialog_result =~ m|ftp://([^/]*)(.*)|;
	
	$ftp_site = $1;
	$ftp_dir = $2;
    } else {
	if (! &rhs_inputbox ( "FTP Configuration",
<<EOM
>
Enter the FTP host name.
>
EOM
			     , 60, $ftp_site)) {
	    return 0;
	}
	$ftp_site = $dialog_result;
	if (! &rhs_inputbox ( "FTP Configuration",
<<EOM
>
Enter the path to the Red Hat files on $ftp_site.
>
EOM
			     , 60, $ftp_dir)) {
	    return 0;
	}
	$ftp_dir = $dialog_result;
	if ($ftp_dir =~ m|^[^/]|) {
	    $ftp_dir = "/" . $ftp_dir;
	}

#	if (&rhs_yesno("FTP Configuration",
#<<EOM
#>
#If you are behind a firewall, you may need to log in as
#something other than anonymous, and give a real password.
#>
#Do you need to do this?
#>
#EOM
#		       , 60)) {
#	    if (! &rhs_inputbox ( "FTP Configuration",
#<<EOM
#>
#Enter the login name for your FTP server
#>
#EOM
#				 , 60, $rpm_ftp_login)) {
#		return 0;
#	    }
#	    $rpm_ftp_login = $dialog_result;
#	    if (! &rhs_inputbox ( "FTP Configuration",
#<<EOM
#>
#Enter the password for your FTP server
#>
#THIS PASSWORD WILL SHOW IN THE SCREEN IN CLEAR TEXT!
#>
#EOM
#				 , 60, $rpm_ftp_password)) {
#		return 0;
#	    }
#	    $rpm_ftp_password = $dialog_result;
#	}

    }

    return 1;
}

sub ftp_get_file_list {
    local (*files) = @_;

    if (! $ftp_configured) {
	&rhs_msgbox("FTP Install", "\nFTP is not configured!\nYou will" .
	    "have to configure it and return to this stage.\n\n", , 70);
	return 0;
    }

    &rhs_infobox("FTP Install",
<<EOM
>
Getting list of packages from FTP site.
>
EOM
		 , 60);

    @files = &get_ftp_dir("RedHat/RPMS");
    if ($ftp_error) {
	&rhs_msgbox("FTP Install",
<<EOM
>
FTP failed!
>
Error returned: $ftp_error
>
EOM
		    , 70);
	return 0;
    }

    return 1;
}

###########
1;
