#!/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: mme_sendfile,v 1.3 1996/10/14 23:46:25 ejb Exp $
# $Source: /home/ejb/scripts/RCS/mme_sendfile,v $
# $Author: ejb $
#

require 5.002;
use strict;

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

mkdir $tmpdir, 0777 or die "$whoami: mkdir $tmpdir failed: $!\n";

umask 002;
my $lastcomp = ($file =~ m,([^/]*)$,) ? $1 : $file;
system("split --bytes=25000 $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 | sed -e 's/^begin /begin 0/' |" .
		   " sed -e 's/\`/ /g' > $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";
    print M "//BEGIN BINARY MAIL SEGMENT:\n";
    open(F, "<$_") || die "$whoami: can't open $_: $!\n";
    while (<F>)
    {
	print M;
    }
    close(F);
    print M "//END BINARY MAIL SEGMENT\n";
    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"
}
