#! /bin/sh
# Send - part of the email package of
# sbbs
appath=$dir/admin/profile

if [ "$1" = "" ] ; then
echo "Enter user name or # to send mail to:"
read name
to=`echo "$name"| tr "[a-z]" "[A-Z]"`
to=`echo "$to" | tr -s " "`
else
to=$1
echo "Include message in reply?(n)\c"
read yn
yn=`echo $yn | tr -d "!"`
if [ "$yn" = Y -o "$yn" = y ] ; then
  xflag=1
fi
fi
q=`expr $to + 0 2> /dev/null`
if [ $? -ne 0 ] ; then
  t=`awk -F: '{print $3}' $dir/admin/profile/members | grep "^$to\$"`
  flagh=1
else
t=`grep "^$to:" $appath/members`
fi
if [ "$t" = "" ] ; then
  echo "There is no user $to"
  exit
fi
if [ "$flagh" =  1 ] ; then
  flagh=0
  to=`awk -F: '{print $1 ":" $3}' $dir/admin/profile/members | grep ":$to\$"`
  to=`echo "$to"| awk -F: '{print $1}'`
fi
k=`grep "^$to:" $dir/admin/profile/members | awk -F: '{print $3}'`
if [ -z "$k" ] ; then
  echo "User was deleted."
  exit
fi

no=`ls $dir/mail/"$to" | wc -l`
no=`echo $no`
no=`expr $no + 1`

file=sbbs.sml.$$
echo "From: #$bbs_name ($fullname)" >> /tmp/$file
echo "Date: `date`" >> /tmp/$file
echo "Message to: #$to(you)" >> /tmp/$file
if [ "$1" != "" ] ; then
  echo "Reply from bulletin: \"$2\"\n" >> /tmp/$file
fi
if [ "$xflag" = 1 ] ; then
   sed -n '6,$s/^/> /
           6,$p' $dir/bbs/$basen/$3 >> /tmp/$file
fi
if [ "$1" = "" ] ; then
echo "\n" >> /tmp/$file
fi
echo "Enter your message now."
echo "Enter CTRL-D for editor."
while : ; do
   cat >> /tmp/$file
   $dir/programs/edit /tmp/$file
   p=$?
   if [ $p = 2 ] ; then
     exit
   fi
   if [ $p = 1 ] ; then
     continue
   fi
   if [ $p = 3 ] ; then
     cat /tmp/$file > $dir/mail/"$to"/$no
     rm /tmp/$file
     echo "Message Saved..."
     exit
   fi
done
