From bu.edu!leland.Stanford.EDU!stanford.edu!stergios Wed Aug  7 21:10:22 1991
Return-Path: <bu.edu!leland.Stanford.EDU!stanford.edu!stergios>
Received: by minya.uucp (/\=-/\ Smail3.1.18.1 #18.16)
	id <m0k7ytJ-0005mEC@minya.uucp>; Wed, 7 Aug 91 21:10 EDT
Received: from leland.Stanford.EDU by BU.EDU (1.99) Wed, 7 Aug 91 20:47:44 EDT
Received:  by leland.Stanford.EDU (5.57/25-eef) id AA16095; Wed, 7 Aug 91 17:47:41 -0700
Received: by kt22.Stanford.EDU (4.1/SMI-4.1)
	id AA05272; Wed, 7 Aug 91 17:47:40 PDT
Date: Wed, 7 Aug 91 17:47:40 PDT
From: bu.edu!stanford.edu!stergios (Stergios)
Message-Id: <9108080047.AA05272@kt22.Stanford.EDU>
To: jc@minya.UUCP, jc@sppip7.lkg.dec.com
In-Reply-To: jc@minya.UUCP's message of 7 Aug 91 02:35:08 GMT
Subject: Tool to split up and mail big files?
Reply-To: bu.edu!kt22.stanford.edu!stergios



here you go.

sm



#!/bin/sh

##############################################################################
# These commands and paths should be configured for your machine/site

BASENAME=/usr/bin/basename
CAT=/bin/cat
RM=/bin/rm
SPLIT=/usr/bin/split
TA=/bin/tar          # Archive command (assumed here to be in search path!)
CO=/usr/ucb/compress # Default compress command  (assumed to be in search path!)
MA=/usr/ucb/Mail     # Default Mail program
UU=/usr/bin/uuencode # uuencode command (assumed here to be in search path!)
CB=16                # Default number of bits to use in compress
CF="-b 16"           # Default compress command line flag(s)
MS=Subj              # Mailer Subject line flag
DE=10                # Default delay time between sending pieces
TD=/tmp              # Default temporary directory for storing temp files
DB=5                 # Default debugging level
##############################################################################
#
# mailsplit:  package, transmit, re-assemble unix directories via e-mail
#
# @(#)mailsplit.SH    2.6    88/08/20       M F Wyle


##############################################################################
# You might want to change these, but it's not likely.

ME=`$BASENAME $0`   # Program (my) name
LI=700              # Default number of lines for split-up pieces
CM="$0 $*"          # Command line
VE="2.6  88/08/20"       # Version of this program
MF=""               # Default Mailer flags
IJ="true"           # Flag which signals if joinr script should be prepended
TM=false            # Default test Mode flag (If set, don't do anything)
T1=$TD/1sp$$        # Default temp file for output of tar, compress
T2=$TD/2sp$$        # Default temp file for uuencoded output
T3=$TD/3sp$$        # Default temp file prefix for split pieces
T4=$TD/4sp$$        # a copy of joinr
##############################################################################

# You do NOT want to change these variables or their initial values!

RA=all              # Default range of pieces to send
FC=0                # File Count
ne="FL=$FL' '"      # Next command in parser
SU="_default_"      # Default Subject is first file or dir sent
FL=""               # Initialize file list
TO=""               # Initialize recipient
umask 0             # Important for writing reading files created!

# Human Factors Law Number 4:  
# "Upon startup, thou shalt tell the user where he is and how to quit."
#           -Ben Schneiderman-

echo " " 1>&2
echo "$ME version $VE    (C) 1988    M F Wyle" 1>&2
echo " " 1>&2

##############################################################################
# Read beyond here at your own risk; you've entered... The hacker zone...
# Parse the command line for recipient, options, flags, files, dirs:

for AR in $* ; do
  case $AR in
    -C  ) ne="CO=" ;;
    -D  ) ne="TD=" ;;
    -M  ) ne="MF=" ;;
    -N  ) IJ="false" ;;
    -T  ) ne="TA=" ;;
    -V  ) DB=9     ;;
    -b  ) ne="CB=" ;;
    -d  ) ne="DE=" ;;
    -l  ) ne="LI=" ;;
    -m  ) ne="MA=" ;;
    -n  ) TM=true  ;;
    -r  ) ne="RA=" ;;
    -s  ) ne="SU=" ;;
    -t  ) TM=true ;;
    -v  ) echo "$ME version $VE by Mitch Wyle" ;  exit 0 ;;
    -x* ) DB=`echo $AR | cut -c3` ; DB=${DB:-0} ;;
    -u  ) ne="UU=" ;;
    -*  ) echo " " ; echo "Bad flag for $ME": $AR
          echo "Usage:    $ME  [<options>] <mailpath> <file(s)>"
          echo "Example:  $ME  werner@utastro.uucp ./casetool"
          echo " "
          exit 0  ;;
    *   ) eval $ne"$AR" ; ne='FL=$FL" "' ;;
  esac
done
case $# in
  0 | 1 )
    echo " "
    echo "Usage:    $ME  [<options>] <mailpath> <file(s)>"
    echo "Example:  $ME  werner@utastro.uucp CaseTool"
    echo " "
    exit 0  ;;
esac
set $FL
TO=$1
case $SU in
  _default_ ) SU=$2 ;;
esac

case $DB in
  8|9) set -x ;;
esac

# create a copy of joinr in /tmp:
cat >$T4 <<'JOINR!END'
#!/bin/sh
#
# joinr - Extract and glue together mailsplit transmissions 
# @(#)joinr.SH    2.6    88/08/20       M F Wyle

############################################################################
# These commands and paths should be configured for your machine/site

BA=/usr/bin/basename     # chop off leading path *
CAT=/bin/cat         # Concatenate And prinT *
TA=/bin/tar          # Archive command *
TAO="xvf"        # options to send to archive command to extract data
RM="/bin/rm -f"      # Remove command
SED=/bin/sed         # Stream editor *
MV=/bin/mv           # Rename command *
GREP=/bin/grep       # get regular expression *
HEAD=/usr/ucb/head       # Get initial part of file (for a non-UCB sys use cat) *
PWD=/bin/pwd         # Print working directory *
touch=/usr/bin/touch     # the touch command
uncompress=/usr/local/bin/uncompress
uudecode=/usr/bin/uudecode
CE=Z           # compress'd file name extension

TD=/tmp            # temp directory
T1=/tmp/x_$$_    # temp file for pieces
T2=/tmp/g_$$     # temp file for garbage
T3=/tmp/f_$$     # temp file for archive
T4=/tmp/p_$$     # file containing the extract awk script

##############################################################################
# You might want to change these, but it's not likely.

ME=`$BA $0`        # This program's name
VE="2.6 88/08/20"  # Version of this program

##############################################################################
# Hacker city

echo " "
echo "$ME version $VE    (C) 1988     M F Wyle" 1>&2
echo " "

echo "...initializing, preparing to extract..."
if test ! -t 0; then
  echo "...found data on standard in; ignoring command line..."
else
  echo "...standard input empty, looking for files on command line..."
  case $# in
    0) echo " "
       echo "Usage:   $ME [ < ] file(s)"
       echo "Example: $ME < p1of2 p2of2"
       echo " "
       exit 1 ;;
  esac
  echo "...found $# file(s) on command line, feeding them to $ME"
  $CAT $* | $ME
  exit 0
fi

$RM $T1* $T2 $T3* $T4*  # get rid of old files
$CAT  <<'EOA' > $T4         # create awk extract script
awk '
BEGIN { inhex = 0 ; part = 0 ; file = "" }
/^---/ {
  if ($2 == "start") {
    part++
    if (part < 10) {
      file = "T1" $6
      if (proc_files[file] == 1) {
        file = "T2"
      } else {
        proc_files[file] = 1
      }
    }
  }
  if (inhex && (NF > 1))
    inhex = 0
  else {
    inhex = 1
    getline
  }
}
{ if (inhex) print $0 > file }'
EOA

# Make awk script use process-specific filenames
$SED -e "s;T1;$T1;" -e "s;T2;$T2;" < $T4 > $T4"p"
$RM $T4
$MV $T4"p" $T4
# Invoke the awk script on std in:
echo ...extracting data...
. $T4
$CAT $T1* > $T3                # gather pieces
echo ...finished extraction, starting uudecode...
fname=`$HEAD -100 $T3 | $GREP begin | $SED 's/.* .* //'`
OurDir=`$PWD`
cd $TD
$touch $T3
chmod -f 777 $T3
$uudecode $T3
$RM  $T3
chmod -f 777 $fname
$MV $fname "$fname.$CE"
echo ...finished uudecode, starting uncompress...
$uncompress $fname.$CE
$MV $fname $OurDir
cd $OurDir
echo ...finished uncompress, starting tar extraction...
fname=`basename $fname`
$RM $T1*                      # tar returns no exit status!! Grrr.
$TA $TAO $fname 2>$T1         # check exit status of tar
if test -s $T1 ; then
  echo "... >>> Sigh <<<  $TA seems to be having problems:"
  $CAT $T1
  echo "...saving $TA archive in $fname."
else
  echo "...That's all folks!"
  $RM $fname
fi
$RM  $T1* $T2* $T3* $T4*
exit 0
JOINR!END

case $RA in               # hacked from usenet commentary; will
  all) ;;                 # be replaced by an awk script RSN
  *  )
    SIFS="$IFS"
    IFS=","
    for i in $RA ; do
      R=$R" "$i
    done
    RA=$R
    IFS=$SIFS
    R=""
    for tok in $RA ; do
      case $tok in 
      *-*)
        SIFS="$IFS"
        IFS='-'
        set $tok
        IFS="$SIFS"
        i=$1
        max=${2:-99}
        while test $i -le $max ; do
          R=$R" "$i
          i=`expr $i + 1`    # only exec in the entire parse
        done ;;
      *) R=$R" "$tok ;;
      esac
      RA=$R
    done ;;
esac

case $DB in
  5|6|7|8|9) echo "...beginning tar and compress..." ;;
esac
set $FL ; shift
for i in $* ; do
  if test -s $i ; then        # Test existence of each file to send
    FC=`expr $FC + 1`         # Increment file counter
  fi
done
if test $FC -gt 0 ; then      # Is there something to send?
  case $DB in                 # Yup. continue
    1|2|3|4) 
      case $TM in
        true )
          echo "would have issued command:"
          echo "$TA cvf - $* 2> /dev/null | $CO $CF >$T1" ;;
        false)  $TA cvf - $* 2> /dev/null | $CO $CF  >$T1 ;;
        *    ) echo "Sigh... Programmer error in $ME at line 93." ;;
      esac ;;
    *) 
      case $TM in
       true )
         echo "would have issued command:"
         echo "$TA cvf - $* | $CO $CF >$T1" ;;
       false) $TA cvf - $* | $CO $CF >$T1 ;;
       *    ) echo "Sigh... Programmer error in $ME at line 153." ;;
      esac ;;
  esac
else                          # Nope, nothing to send
case $DB in
  [5-9] ) echo "...whoops!  Nothing to send!  Punting..." 1>&2 ;;
esac
  exit 1
fi
case $DB in
  5|6|7|8|9) echo "...finished tar, compress, starting uuencode..."
esac
case $TM in
  true )
    echo "would have issued command:"
    echo "$UU $T1 $T1 > $T2 " ;;
  false) $UU $T1 $T1 > $T2 ; ;;
  *    ) echo "Sigh... Programmer error in $ME at line 170." ;;
esac
case $DB in
  5|6|7|8|9) echo "...finished uuencode, starting split..." ;;
esac
case $TM in 
  true )
    echo "would have issued command:" 
    echo "$SPLIT -$LI $T2 $T3" ;; 
  false)  $SPLIT -$LI $T2 $T3 ; ;; 
  *    ) echo "Sigh... Programmer error in $ME at line 128." ;;
esac 
case $TM in
  true )
    echo "would have issued commands:"
    echo "$MA -s "$SU - part n of m" $TO" 
    echo "with n ranging from 1 to m; m cannot be determined."
    echo "without running for real."
    exit 0 ;;
esac 

n=1
set $T3*
for f  do
  SE=false
  case $RA in
    all)  SE=true ;;
    *  )
      for p in $RA ; do
        case $p in
          $n ) SE=true ;;
        esac
      done ;;
  esac
  case $SE in
    true)
    {
    echo " "
    case $n in
    1)
      if test ! -s $f ; then
        case $DB in 
          [5-9]) echo "...whoops!  $SPLIT failed!   Punting..." 1>&2 ;;
        esac 
        exit 1 ;
      fi
      case $IJ in
        true) cat $T4 ;;
      esac
      echo " " ;;
    esac
    echo " "
    echo "This file was packed by mailsplit version:"
    echo $VE
    echo with command:
    echo "$CM"
    echo "on: `date`"
    echo " "
    echo "--- start of $SU part $n of $#"
    $CAT $f
    echo "--- end of $SU part $n of $#"
    echo " "
    echo " "
    } | 
    case $MS in
      Subj)  $MA $MF -s "$SU - part $n of $#" $TO ;;
      *)     $MA $MF $TO ;;
    esac
    case $DB in
      [5-9])   echo "...sent part $n of $# to $MA..." ;;
    esac
    case $DB in
      5|6|7|8|9) echo "...sleeping $DE seconds (waiting for mailer) ..." ;;
    esac
    sleep $DE ;;
  esac
  n=`expr $n + 1`
done
$RM -f $T1 $T2 $T3* $T4

