#!/bin/bash
# uuesplit
# split a long file containing several uuencoded files
# usage: uuesplit [file.to.split]

INFILE="uue.in"
TMPFILE="uue.out"
COUNTER=1

# first clean up things removing news-stuff
cat "$1" | sed -e "/^begin.*$/,/^end.*$/!d" > $INFILE

# then - create a filename for the output file and increase counter
#      - copy the first "begin ...  end" block to a separate file
#      - delete the first "begin ...  end" block
#      - make the resulting  shorter file the new input file
#      - echo what has been done to monitor progress
while [ -s $INFILE ]; do
   OUTFILE=${COUNTER}.uue
   COUNTER=$(($COUNTER+1))
   sed -n \
      -e "1,/^end.*$/w $OUTFILE" \
      -e "/^end.*$/q" $INFILE
   sed -e "1,/^end.*$/d" $INFILE >$TMPFILE
   rm -f $INFILE
   mv $TMPFILE $INFILE
   echo $OUTFILE
done
rm -f $INFILE
exit 0
