#!/usr/local/bin/bash
# Mail a multi-part shar from a list of files.
# Copyright (C) 1990, 1994 Free Software Foundation, Inc.
# Francois Pinard <pinard@iro.umontreal.ca>, 1991.

usage="\
Usage: $0 [OPTION...] DEST FILE ...

with OPTION in:

  -M   decide how to send each file separately
  -T   avoid calling compress, gzip nor uuencode
  -B   force calling uuencode
  -z   force calling gzip and uuencode
  -Z   force calling compress and uuencode
  -x   trace script

If none of -MTBzZ are given, -z is automatically selected if *none*
of the FILEs have an .arc, .exz, .gif, .z, .gz, .Z, .zip or .zoo suffix."

temp=/usr/tmp/$$.shar

### Decode the options.

while [ $# -gt 0 ]; do
  case $1 in
    -[MTBzZ]) mode=$1; shift ;;
    -x) trace=-x; set -x; shift ;;
    -*) echo "$usage"; exit 1 ;;
    *) break
  esac
done

if [ $# -lt 2 ]; then
  echo "$usage"
  exit 1
fi

dest="$1"
shift
subject="$*"

### Check if we should gzip.

if [ -z "$mode" ]; then
  mode=-z
  find $* -type f -print 2> /dev/null > $temp
  while read file; do
    case "`echo $file | sed -n 's|.*/||;/\./s|.*\.||p' \
	| tr '[A-Z]' '[a-z]'`" in
      arc|exz|gif|gz|z|zip|zoo) mode=; break ;;
    esac
  done < $temp
  rm $temp
fi

### Construct the multi-part shar files and mail them.

/usr/bin/shar $mode -P -L60 -o$temp -c -F $*
mail-files $trace $dest shar "$subject" $temp*
rm ${temp}*
exit 0
