#!/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: uu-sendfile,v 1.2 1997/03/30 01:46:47 ejb Exp $
# $Source: /home/ejb/scripts/RCS/uu-sendfile,v $
# $Author: ejb $
#

require 5.002;
use strict;

package OnExit;
my $package = "OnExit";

# Field names
my $f_function = "function";
my $f_args = "args";

# Routines

sub new
{
    my $class = shift;
    my $rep = +{$package => {} };
    $rep->{$package}{$f_function} = shift;
    $rep->{$package}{$f_args} = shift;
    bless $rep, $class;
}

sub DESTROY
{
    my $rep = shift;
    my $function = ($rep->{$package}{$f_function});
    my $args = ($rep->{$package}{$f_args});
    &{$function}(@{$args});
}

package main;

my $whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;
#my $dirname = ($0 =~ m,(.*)/[^/]+$,) ? $1 : ".";

my $tmpdir = "/tmp/$whoami.$$.tmp";
&usage() if (@ARGV < 2);
my $file = shift;
my $addrs = join(' ', @ARGV);

if ($file !~ m/^[\w\-]{1,8}$/)
{
    exit 0 unless
	&yn("Enclosure names will not conform to 8.3 names.  Continue? ");
}

$SIG{'INT'} = $SIG{'HUP'} = $SIG{'TERM'} = sub { exit 2 };
mkdir $tmpdir, 0777 or die "$whoami: mkdir $tmpdir failed: $!\n";
my $on_exit = new OnExit(sub { system("rm -rf @_") }, [$tmpdir]);

umask 002;
my $lastcomp = ($file =~ m,([^/]*)$,) ? $1 : $file;
system("split --bytes=35000 $file $tmpdir/$lastcomp.");
chdir $tmpdir || die "$whoami: can't chdir $tmpdir: $!\n";
my @files = (<$lastcomp.*>);
my $i = 0;
for (@files)
{
    $i++;
    my $t = sprintf("%03d", $i);
    my $new = $_;
    $new =~ s/\.[^\.]+$/\.$t/;
    my $r = system("uuencode $_ $new > $new.uu");
    if ($r != 0)
    {
	die "$whoami: uuencode failed\n";
    }
}
@files = (<$lastcomp.*.uu>);
my $parts = scalar(@files);
my $i = 0;
for (@files)
{
    $i++;
    my $subject = "$file part $i/$parts";
    open(M, "|mail -s \"$subject\" $addrs") or
	die "$whoami: can't run mail\n";
    open(F, "<$_") || die "$whoami: can't open $_: $!\n";
    while (<F>)
    {
	print M;
    }
    close(F);
    close(M);
    print "sent part $i of $parts\n";
    if ($i != $parts)
    {
	print "waiting 3 seconds\n";
	sleep 3;
    }
}


sub usage
{
    die "Usage: $whoami file addrs"
}

sub yn
{
    my $prompt = shift;
    my $answer;
    print $prompt;
    do
    {
	chop($answer = <STDIN>);
	if ($answer eq "y")
	{
	    return 1;
	}
	elsif ($answer eq "n")
	{
	    return 0;
	}
	else
	{
	    print "Please enter y or n: ";
	}
    }
    while (($answer ne "y") && ($answer ne "n"));
}
