#! /bin/sh
#   faxmail script by Robert Hummel
#   Usage:
#	faxmail [-at person][-fn faxnum][-tn voicenum][-ou org]
#		[-o more_org -o yet_more_org ...][-t][-c cc][-s subj][-p][file]"
#       faxmail [-t]
#
#  First form mails file (default: stdin) using fields defined by command
#  line args;  -c used to specify copies to, -s to give a subject to the
#  mail message.  -t says to provide only the address -- don't mail.
#  Parameters with embedded blanks must be surrounded by quotes.
#  -p says that file is postscript, if not specified, heuristics determine.
#
#  Second form is interactive mode, and user designates fields through
#  a menu, giving the file name in addition to other fields.  Interactive
#  mode cannot read the file from the stdin.
#
ou="" ; out=0 ; interactive=0 ; file="-" ; postscript=0
if test $# -gt 1
then
while test "$1"
do
case $1 in
	-fn|-FN)
		fn="$2"
		shift; shift;;
	-tn|-TN)
		tn="$2"
		shift; shift;;
	-at|AT)
		at="$2"
		shift; shift;;
	-ou|-OU)
		ou="${ou}/OU=$2"
		shift; shift;;
	-o|-O)
		o="$2"
		shift; shift;;
	-fsi|-FSI)
		echo "Option FSI not implemented at NYU"
		shift; shift;;
	-tti|-TTI)
		echo "Option TTI not implemented at NYU";
		shift; shift;;
	-auth|-AUTH)
		# echo "Option AUTH not yet implemented at NYU";
		auth="$2"
		shift; shift;;
	-)
		file="-"
		break;;
	-c)
		cc="$2"
		shift; shift;;
	-s)
		subject="$2"
		shift; shift;;
	-t)
		out=1 ; shift;;
	-p)
		postscript=1;;
	-*)
		echo "faxmail: Option $1 not recognized"
		echo "Usage: faxmail [-at person][-fn faxnum][-tn voicenum]"
		echo "[-ou org][-o more_org -o yet_more_org ...][-t][-c cc][-s sub][file]"
		shift;;
	*)
		case $# in
			1) file=$1; break;;
			*) echo "faxmail: Option $1 not recognized"
		echo "Usage: faxmail [-at person][-fn faxnum][-tn voicenum]"
		echo "[-ou org][-o more_org -o yet_more_org ...][-t][-c cc][-s sub][file]"
			shift;;
		esac
esac
done
else 
  field="help"
  interactive=1 
  case $1 in
    "") ;;  # file = "-"
    -t) out=1 ;;  # file = "-"
     *) file=$1 ;;
  esac
  while true
    do
    case $field in
	  fn) 
		  echo -n "${fn+(Overwrite }${fn}${fn+) }FN: "
		  read fn ;;
	  tn)
		  echo -n "${tn+(Overwrite }${tn}${tn+) }TN: "
		  read tn ;;
	  at)
		  echo -n "${at+(Overwrite }${at}${at+) }AT: "
		  read at ;;
	  o)
		  echo -n "${o+(Overwrite }${o}${o+) }O: "
		  read o ;;
	  ou)
		  test ! "$ou" = "" && echo "(Append to ${ou}) "
		  echo -n "OU : "
		  read ou1
		  ou="${ou}/OU=${ou1}" ;;
	  fsi)
		  echo "FSI not used at NYU" ;;
	  tti)
		  echo "FSI not used at NYU" ;;
	  auth)
		  echo -n "${auth+(Overwrite }${auth}${auth+) }AUTH: "
		  read auth ;;
		  # echo "AUTH not yet implemented at NYU" ;;
	  file)
		  echo "Overwrite current input file \"$file\" "
		  echo -n "FILE: "
		  read file ;;
	  print)
		  echo FN=$fn
		  echo TN=$tn
		  echo AT=$at
		  echo O=$o
		  echo OU=$ou
		  echo AUTH=$auth 
		  ;;
	  help)
  # -- HELP MENU
  echo
  echo "fn	Telephone number of the destination FAX"
  echo "tn	Telephone number of the recipient's voice line"
  echo "at	To the Attention Of, usually a the recipient's name"
  echo "o	Organization"
  echo "ou	Organizational unit or department (may be repeated)"
  echo "fsi	Sender's FAX Station Identification string (disabled at NYU)"
  echo "tti	Transmitting Terminal Identification (disabled at NYU)"
  echo "auth	TARA code or calling card number "
  echo "file	Change input file"
  echo "print	Print summary of current fields"
  echo "help	Print this message"
  echo "quit	(Or ^C) Abort"
  echo "send	(Or ^D)	Exit input mode and send fax"
  # -- END HELP MENU
		  ;;
	  send|"")
		  break ;;
	  quit)
		  echo "Nothing sent"
		  exit 1;;
	  *)
		  echo "Keyword not recognized"
		  echo 'Enter "help" to print menu, "send" to exit' ;;
    esac
    echo
    echo "Enter field name"
    read field
  done  # infinite while loop
fi
if test ! "$at"
then 
	if test "$interactive" -eq 0
		then echo "faxmail: You must give both -at and -fn fields"
		exit 1
	fi
	echo "(AT	To the Attention Of, usually the recipient's name)"
	echo -n "AT: "
	read at
fi
if test ! "$fn" 
then
	if test "$interactive" -eq 0
		then echo "faxmail: You must give both -at and -fn fields"
		exit 1
	fi
	echo "(FN	Telephone number of the destination FAX)"
	echo -n "FN: "
	read fn
fi
if test "$file" = "-" -a "$out" -eq 0 -a "$interactive" -eq 1
	then echo 'You must specify a file for transmission'
	echo -n "File= "
	read file
fi
#  Put together user name.  Quotes go on in any case:
avpl=\"${fn+/FN=}${fn}${at+/AT=}${at}${auth+/AUTH=}${auth}${tn+/TN=}${tn}${o+/O=}$o${ou}/\"
# Next line was needed on INRIA machine, due to quote stripping:
# avpl=`echo ${avpl} | sed "s/ /_/g"`  
tmpfile=""
case $out in
0)
	if test "$file" = "-"
	then
		tmpfile=/tmp/fax.$$
		trap 'rm $tmpfile; exit 1' 2
		cat > $tmpfile
		file=$tmpfile
	fi
	if test ! -s "$file"
		then echo "faxmail: File $file is does not exist or is empty"
		test "$tmpfile" && rm $tmpfile
		exit 1
	fi
	if test ! -r "$file"
		then echo "faxmail: File $file does not exist or is unreadable"
		test "$tmpfile" && rm $tmpfile
		exit 1
	fi
	if test "$postscript" -eq 1 || head -5 $file | grep -s "%! *PS" \
			|| file $file | grep -s -i "postscript"
		then add="${avpl}@PS-FAX.NYU.EDU"
		else add="${avpl}@TEXT-FAX.NYU.EDU"
	fi
	( echo "To: $add" ; test "$cc" && echo "Cc: $cc" ; \
	  test "$subject" && echo "Subject: $subject" ; echo ; cat $file ) | \
		/usr/lib/sendmail -t
	test "$tmpfile" && rm $tmpfile
	;;
1)
	if test "$file" != "-"
		then if test "$postscript" -eq 1 \
		  || head -5 $file | grep -s "%! *PS" \
		  || file $file | grep -s -i "postscript"
			then add="${avpl}@PS-FAX.NYU.EDU"
			else add="${avpl}@TEXT-FAX.NYU.EDU"
		fi
	else add="$avpl"
	fi
	echo "$add" ;;
esac
#  End of script


