#!/bin/csh -fb
###############################################################################
##  uufiles  (tar-compress and uuencode files for email (PG, 11/92, 11/95))  ##
###############################################################################
# call this file uufiles (remember to chmod +x uufiles) and put it somewhere on
# your path. uuencoded file is executable with self-unpacking csh script at top
###############################################################################
# Usage: cd to directory where you want .uu file to be created, then say e.g.
# uufiles [-gz] [-9]
# directory with files to be archived:  ~/mypaper
# files (e.g. *.ps, file1 file2 file3, or * for all): *.ps
# output file (will create name.tar.Z [.gz] uuencoded as name.uu) name: figures
# specifying -9  uses the slowest gzip compression method (optimal compression)
# single files are not tarred, just compressed and uuencoded
###############################################################################
#
gzip -h >& /dev/null
if ($status && "$1" != "-gz") then
  set gz=Z unc=uncompress gunzip=zcat gzip=(compress -f -c)
  echo Sadly, will use Z-compression. For better results, get gzip
  echo "   from ftp://prep.ai.mit.edu/pub/gnu/ installed on your path."
else
  set level="$1"
  if ("$1" == "-gz") then
     echo note gzip is now default, -gz flag harmless but no longer needed
     set level="$2"
  endif
  set gz=gz unc=gunzip gunzip="gunzip -c" gzip=(gzip $level -f -c)
  echo will use gzip $level to compress
endif
echo -n "directory with files to archive (default is $cwd): "
set dir=$<
if ("$dir" == "") set dir="."
if (! -d "$dir") then
 echo no such directory $dir
 exit
endif
echo -n "files in $dir (e.g. *.ps, file1 file2 file3, or * for all): "
set files="$<"
set files=`cd $dir; glob $files`
set z=$0; set z=$z:t
set files=`echo $files | sed -e "s/ $z / /g" -e "s/^$z //" -e "s/ $z"'$//'`
if (`echo $dir/$files | wc -w` == 1 && ! -d `echo $dir/$files`) then
  set dtar="" tar=cat name=$files:t
  set pname=$name:r
  if (! -e "$dir/$files") then
     echo no such file $dir/$files, nothing done. try again.
     exit
  endif
  echo single file. will create $name.$gz uuencoded as $pname.uu
else
  echo "(here are the files that will be packed:"
  echo "   "$files
  echo " carriage return or ctrl-C at next query to exit in case of problem)"
  echo -n "output file (will create name.tar.$gz uuencoded as name.uu) name: "
  set pname=$<
  if ("$pname" == "") then
     echo no name specified, nothing done. try again.
     exit
  endif     
  if ("$pname:e" == uu) set pname="$pname:r"
  set name=$pname:t dtar=".tar" tar="tar cf -"
endif
cat > $pname.uu << EOF
#!/bin/csh -f
# Uuencoded $gz-compressed $dtar file created by csh script  uufiles
# For more info (11/95), see e.g. http://xxx.lanl.gov/faq/uufaq.html
# If you are on a unix machine this file will unpack itself: strip
# any mail header and call resulting file, e.g., $pname:t.uu
# (uudecode ignores these header lines and starts at begin line below)
# Then say        csh $pname:t.uu
# or explicitly execute the commands (generally more secure):
#    uudecode $pname:t.uu ;   $unc $name$dtar.$gz ;
EOF
if ("$dtar" != "")  echo "#    tar -xvf $name.tar" >> $pname.uu
cat << EOF >> $pname.uu
# On some non-unix (e.g. VAX/VMS), first use editor to change filename
# in "begin" line below to $name${dtar}-$gz , then execute
#    uudecode $pname:t.uu
#    $gzip[1] -d $name${dtar}-$gz
EOF
if ("$dtar" != "")  echo "#    tar -xvf $name.tar" >> $pname.uu
echo \# >> $pname.uu
echo uudecode \$0 >> $pname.uu
echo chmod 644 $name$dtar.$gz >> $pname.uu
if ("$dtar" != "") then
   echo "$gunzip $name$dtar.$gz | tar -xvf -" >> $pname.uu
   echo rm \$0 $name$dtar.$gz >> $pname.uu
else
   echo $unc $name$dtar.$gz >> $pname.uu
   echo rm \$0 >> $pname.uu
endif
echo exit >> $pname.uu
echo "" >> $pname.uu
echo ""
echo " Switching to directory  $dir  and creating file  $pname.uu  from"
(cd $dir; ls $files)
(cd $dir; $tar $files) | $gzip | uuencode $name$dtar.$gz \
| sed -e '1s/^begin [0-7][0-7]* /begin 644 /' -e '2,$s/ /`/g' >> $pname.uu
# chmod 755 $pname.uu
echo -n " result: "
ls -l $pname.uu
echo ""
echo "finished. to invert:  uudecode $pname.uu"
if ( "$dtar" != "" ) then
 echo "  $gunzip $name$dtar.$gz | tar -xvf -  (or  gnutar zxvf $name$dtar.$gz)"
else
 echo "                      $unc $name$dtar.$gz "
endif
echo "or execute  csh $pname.uu  (which removes $pname.uu and $name$dtar.$gz)"
exit
