#!/usr/local/bin/bash
# Mail a list of files, as they are.
# Copyright (C) 1990 Free Software Foundation, Inc.
# Francois Pinard <pinard@iro.umontreal.ca>, 1991.

usage="\
Usage: $0 [OPTION] DESTIN TYPE SUBJECT FILE ...

with OPTION in:

  -x   trace script"

SLEEP=2

### Decode the options.

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

if [ z$1 = z-x ]; then
  trace=-x; set -x; shift
fi

destin="$1"; shift
type="$1"; shift
subject="$1"; shift

maxcount=$#
files="$*"

### Mail all files, making a proper subject for each message.

( if [ -f $destin ]; then cat $destin
  else echo $destin
  fi
) |
( total=0
  while read destin; do
    count=0
    for file in $files; do
      count=`expr $count + 1`
      if [ $maxcount = 1 ]; then
	string="$type"
      else
	string="$type ($count/$maxcount)"
      fi
      echo "Mailing $string to $destin"
      [ $total -ne 0 ] && sleep $SLEEP
      /usr/bin/Mail -s "$string: $subject" $destin < $file
      total=`expr $total + 1`
      [ $count -lt $maxcount ] && sleep $SLEEP
    done
  done
  if [ $total -eq 1 ]; then echo Message queued
  else echo $total messages queued
  fi
)

exit 0
