#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: ftp_at,v 1.3 1998/05/10 14:29:51 ejb Exp $
# $Source: /home/ejb/scripts/RCS/ftp_at,v $
# $Author: ejb $
#
# Author: E. Jay Berkenbilt
#
#   Note to ERA USERS:
#   The authoritative location for this script is /home/ejb/scripts.
#   Please notify ejb if you edit it so that the original can be
#   suitably updated.
#


require 5.001;
use strict;

my $whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

my $default_config = "/usr/local/share/ftp_at.conf";
my $proxy;
my $user;
my $system;
&load_config();
my ($ftp_proxy) = ($ENV{'FTP_PROXY'} || $proxy);
$ftp_proxy = "" if $ftp_proxy eq "none";
my ($ftp_anon_password) = ($ENV{'FTP_ANON_PASSWORD'} || "$user\@$system");
my @commands = ();
my %requests = ();

&usage if (@ARGV <= 1);
my $cmd = "$whoami " . join(' ', @ARGV);

my $time = shift(@ARGV);
my @files = @ARGV;

my $error = 0;

my $f;
foreach $f (@files)
{
    if ($f !~ m/^([^:]+):(.+)$/)
    {
	warn "$whoami: invalid file specification $f\n";
	$error = 1;
    }
    else
    {
	&register($1, $2);
    }
}
&usage if $error;

my $h;
foreach $h (keys %requests)
{
    my $host = $h;
    my $user = "ftp";
    my $pass = $ftp_anon_password;
    
    if ($h =~ m/^([^\@]+)\@(.+)$/)
    {
	$user = $1;
	$host = $2;
	if ($user =~ m,^([^/]+)/(.+)$,)
	{
	    $user = $1;
	    $pass = $2;
	}
    }
    if ($ftp_proxy)
    {
	push(@commands, "open $ftp_proxy");
	push(@commands, "user $user\@$host $pass");
    }
    else
    {
	push(@commands, "open $host");
	push(@commands, "user $user $pass");
    }
    push(@commands, "bin");
    my $f;
    foreach $f (@{$requests{$h}})
    {
	push(@commands, "cd /");
	if ($f =~ m,^(.+)/([^/]+)$,)
	{
	    my $dir = $1;
	    $f = $2;
	    $dir =~ s,^/,,;
	    push(@commands, "cd $dir");
	}
	push(@commands, "get $f");
    }
    push(@commands, "close");
}
push(@commands, "quit");

open(AT, "|at $time") || die "$whoami: can't run at $time: $!\n";
select AT;
print "ftp -n <<__EOF_FTP__\n";
for (@commands)
{
    print $_, "\n";
}
print "__EOF_FTP__\n";
print 'echo ' . $whoami . ': done at `date`', "\n";
print 'echo ' . $cmd, "\n";;
select(STDOUT);
close(AT);


sub register
{
    my($host, $file) = @_;
    if (! exists $requests{$host})
    {
	$requests{$host} = [];
    }
    push(@{$requests{$host}}, $file);
}


sub load_config
{
    my $error = 0;
    ($user) = ($ENV{'USER'} || $ENV{'LOGNAME'} || (getpwuid($<))[0] ||
	       "unknown");
    chop($system = `hostname`);
    $proxy = "";
    
    if (-f $default_config)
    {
	if (open(CONF, "<$default_config"))
	{
	    while (<CONF>)
	    {
		chomp;
		s/#.*//;
		next if m/^\s*$/;
		if (m/^([^:]+)\s*:\s*(.+)$/)
		{
		    my ($key, $value) = ($1, $2);
		    if ($key eq "system")
		    {
			$system = $value;
		    }
		    elsif ($key eq "proxy")
		    {
			$proxy = $value;
		    }
		    else
		    {
			$error = 1;
			warn "$whoami: $default_config, line $.: " .
			    "unrecognized key $key\n";
		    }
		}
		else
		{
		    $error = 1;
		    warn "$whoami: $default_config, line $. is invalid.\n";
		}
	    }
	    close(CONF);
	}
    }

    exit 1 if $error;
}

sub usage
{
    print STDERR <<EOF;
Usage: $whoami at_time [user[/pass]@]host:file [ ... ]
   at_time is a four-digit number specifying a time for at
     (for example 0200 for 2:00 a.m.)
   If pass is unspecified, your email address will be used.  If user is
     unspecified, the username "ftp" will be used.  You can override the
     default password with the FTP_ANON_PASSWORD environment variable.
   This script is proxy-aware.  If the environment variable FTP_PROXY
     is set, it should contain the hostname of a proxy.
   For this system, the default ftp password is \`user\@$system\', and
EOF
    ;
    if ($ftp_proxy)
    {
	print STDERR "     the default ftp proxy is \`$ftp_proxy\'.\n";
    }
    else
    {
	print STDERR "     no ftp proxy is used.\n";
    }
    print STDERR <<EOF;
   Files will not necessarily be requested in the order they appear on
     the commandline.
   This script will generate an at job to grab the specified files via
     ftp at the given time.
EOF
    ;
    exit 1;
}
