#!/bin/sh
# A shell script to extract binary files from a uudecoded file.
#From: umnoor@ccu.umanitoba.ca          Article 11297 of alt.binaries.pictures.erotica
#
#A while ago, I posted this script which ran only under C-Shell. I didn't
#think of people who dont have C-shell. I still get mails from people asking
#for fix etc. Here is version which is (almost) guarenteed to run on any Unix
#which has standard Bourne Shell (at least thats what I am told that Bourne
#shell is standard on all Unix platforms).

#Just save this post to a file, remove every thing above (and including) the
#"Cut Here" line below. Rename resultant file to your liking and give execute
#permission to the file and you are all set. Read comments in script for more
#details.
#Following command is used to give execute permission to a file.....
#         chmod u+x filename
#
#--nasir
#noor@muug.mb.ca
#umnoor@ccu.umanitoba.ca
#
#
# Written By Nasir Ahmed Noor (noor@muug.mb.ca)
#      June 19/ 1992   (executed under C shell) 
#      revision August 22/ 1992  (made changes to execute under Bourne shell)
#
# This script will extract binary files (Gifs etc) from uudecoded posts
# on the UseNet or email. Comes particularly handy if a large uuencoded file
# is divided into many small pieces and remvoing mail headers from all of
# those parts can take forever.
#
#          - You can store multiple uuencoded files in one single file and
#            this script will extract all binary files from that file and
#            save under their respective names.
#          - Unlimited number of files can be given on command line for
#            processing.
#          - Wild cards are allowed as part of file names on command line.
#
# Usage:
# Store all uudecoded parts of a file (in right order) in a file and give 
# that file as argument to this script. AN INPUT FILE CAN CONTAIN UUDECODED
# PARTS OF MORE THAN ONE FILE AND THEY ALL WILL BE PROCESSED. (e.g., you can
# store a whole newsgroup in one file and it will process all of it). I did
# not experience any limit on the size of input file. The size of largest
# file, which I tried as input, was over 8MB.
#
# Send comments to noor@muug.mb.ca (Nasir).
#

infunc () {
  while ( test $myrec )
    do
       mawk  '$0 ~ /^begin / && NR > rec {print $0}; $0 ~ /^M[^a-z]/ && NR > rec {print $0}; NR > rec {sl=lr; lr=pr; pr=$0}; $1 ~ /^end/ && NR > rec {print sl; print lr; print pr; exit}' rec=$myrec $binfile | uudecode
       mawk 'NR > 1 {print $0}' $$temp > $$temp2
       \rm -f $$temp
       mv $$temp2 $$temp
       myrec=`awk 'NR == 1 {print $0}' $$temp`
    done
}

for binfile in $*
 do
   mawk '$0 ~ /^begin / {myrec = (NR - 1); print myrec}' $binfile > $$temp
   myrec=`awk 'NR == 1 {print $0}' $$temp`
   infunc
   \rm -f $$temp
 done

exit


