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

package="sharutils"
version="4.5.2"

prefix='/usr'
bindir="${prefix}/bin"
transform='s,x,x,'

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 SUBJECT      use \"shar: SUBJECT\" as subject line
  -s SIZE         decide size of each part in Kb, default 60
  -M              decide how to send each file separately
  -T              avoid calling compress, gzip, bzip2 nor uuencode
  -B              force calling uuencode
  -z              force calling gzip and uuencode
  -j              force calling bzip2 and uuencode
  -Z              force calling compress and uuencode
  -x              trace script

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

if type -p mktemp >/dev/null 2>/dev/null; then
  temp=`mktemp /var/tmp/$$.shar.XXXXXX` || exit 1
else
  temp=/var/tmp/$$.shar
  rm -f $temp*
fi
trap "rm -f $temp*; exit 1" 1 2 3 7 13 15
trap "rm -f $temp*" 0

### Decode the options.

size=60

while test $# -gt 0; do
  case $1 in
    -S) if test $# -gt 1; then mysubject=$2; shift 2;
        else echo "$usage"; exit 1; fi ;;
    -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
if test -z "$mysubject" ; then
  subject="$*"
else
  subject="$mysubject"
fi

### 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|bz2|tgz|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

# mailshar.in ends here
