#! /bin/sh
# Mail a multi-part shar from a list of files.
# Copyright (C) 1990, 1994, 1995 Free Software Foundation, Inc.
# François Pinard <pinard@iro.umontreal.ca>, 1991.

package="sharutils"
version="4.2.1"

prefix='${DESTDIR}/usr/local'
bindir="${prefix}/bin"
transform='s,^,g,; '

shar=$bindir/`echo shar | sed "$transform"`
mail_files=$bindir/`echo mail-files | sed "$transform"`

progname=`echo $0 | sed -e 's,.*/,,'`

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

with OPTION in:
      --help      display this help and exit
      --version   output version information and exit

  -s SIZE         decide size of each part in Kb, default 60
  -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.

size=60

while test $# -gt 0; do
  case $1 in
    -s) if test $# -gt 1; then size=$2; shift 2;
        else echo "$usage"; exit 1; fi ;;
    -[MTBzZ]) mode=$1; shift ;;
    -x) trace=-x; set -x; shift ;;
    --v* ) echo "$progname - $package $version"; exit 0 ;;
    --h* ) echo "$usage"; exit 0 ;;
    -) break ;;
    -*) echo "Try \`$progname --help' for more information."; exit 1 ;;
    *) break
  esac
done

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

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

### Check if we should gzip.

if test -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.

$shar $mode -P -L$size -o$temp -c -F $* \
  && $mail_files $trace $dest shar "$subject" $temp* \
  && rm ${temp}* \
  && exit 0

rm -f ${temp}*
exit 1
