#ident "@(#)/usr/local/sysadmin/MAILMGR 1.0 03/13/91" Author: Mr Reinhard Pfaffinger
# Office: 1924 CSS/SDA, Randolph AFB, Texas 78150
# Phone: AV 487-8649  Comm: (512) 652-8649
#
# This script allows the system administrator to manage the MAILER environment.
#

chkfortty() {
             if [ "$TERM" = "tty" ]
               then
                   echo  
             else
                 tput clear 
             fi
            }

while :
 do
   chkfortty
   echo "                   <<< AT&T 3B2/600G Mail Manager Menu >>>\n"
   echo "                       1.  Read Personal MailBox (ELM)" 
   echo "                       2.  Read System/Administrative Users' MailBoxes"
   echo "                       3.  SENDMAIL Daemon Shutdown/Startup"
   echo "                       4.  Add ALIAS(ES) to SENDMAIL"
   echo "                       5.  Delete ALIAS(ES) from SENDMAIL"
   echo "                       6.  List ALIASES Defined in SENDMAIL"
   echo "                       7.  Send Mail to All Users on '`uname -n`' (MAILX)"
   echo "                       8.  System Mail Queue\n" 
   echo "                       q or Q to quit\n"
   echo "                       Enter response <RETURN> =>> \c"
   read RESP
   case "$RESP" in
      1)
        /usr/local/elm
        continue
        ;;
      2)
        chkfortty
        echo "Looking for system/administrative users with mailboxes on '`uname -n`' ... \c"
        for i in `cut -d: -f1,3 /etc/passwd`
          do
            if [ "`echo $i | cut -d: -f2`" -le "102" ]
              then
                  echo $i | cut -d: -f1 | egrep -v "root" >> /tmp/$$
            fi
          done
        for j in `cat /tmp/$$`
          do
            if [ -f /usr/mail/$j ]
              then
                  echo $j >> /tmp/$$.$$
            fi
          done
        if [ -s /tmp/$$.$$ ]
          then
              :
        else
            echo "\n\n\tNo system/administrative logins have mail to read!\c"
            sleep 3
            continue
        fi
        for y in `cat /tmp/$$.$$` 
          do
            chkfortty
            echo "Reading user: $y's  mail ... \c" 
            sleep 2
            echo "\n"
            su - $y -c mail
           done 
        sleep 3
        /bin/rm -f /tmp/$$ /tmp/$$.$$
        continue
        ;;
      3)
        chkfortty
        /usr/local/sendml.mgr
        echo "\nPress <RETURN> ... \c"
        read RTN
        continue
        ;;
      4)
        chkfortty
        /usr/local/distlst.mgr add
        continue
        ;;
      5)
        chkfortty
        /usr/local/distlst.mgr del 
        continue
        ;;
      6)
        chkfortty
        /usr/local/distlst.mgr list all
        echo "Which alias would you like to view? =>> \c"
        read A
        distlst.mgr list $A 
        continue
        ;;
      7)
        /usr/local/localmail
        continue
        ;;
      8)
        while :
         do
        chkfortty
        echo "\t\tSelect one of the following:\n"
        echo "\t\t\t1. Query Mail Queue"
        echo "\t\t\t2. Remove Queued Mail Message(s)\n"
        echo "\t\t\tq or Q to quit\n"
        echo "\t\tEnter response <RETURN> =>> \c"
        read MAILQUERY
        case $MAILQUERY in
           1)
             chkfortty
             echo "Enter number of times to query the mail queue <RETURN> (default 1) =>> \c"
             read QQ
             echo "\n"
             if [ "$QQ" -gt "1" ]
               then
                   /usr/local/mq -t $QQ | pg -n20 -p"Press <RETURN> to continue; q or Q to quit ... "
             else
                 /usr/bin/mailq -v | pg -n20 -p"Press <RETURN> to continue; q or Q to quit ... "
             fi
             ;;
           2)
             chkfortty
             if [ "`/usr/bin/mailq`" = "Mail queue is empty" ]
               then
                   echo "\n\n\t\t\tMail queue is empty!\c"
                   sleep 3
                   continue
             fi
             echo "The following mail message ID(s) are removable:\n"
             /usr/bin/mailq | cut -c1-7 | egrep -v "QID|Mail|^$|	" | pr -t -3
             echo "\nEnter the mail ID(s) you wish to remove from the queue, separated by a"
             echo "SPACE, and press <RETURN> =>> \c"
             read MAILIDS
             echo
             /usr/local/dm $MAILIDS  
             echo "\nPress <RETURN> ... \c"
             read RTN
             ;;
      q|Q|"")
             break;;
        esac
         done
        continue
        ;;
    q|Q)
        exit 0
        ;;
      *)
        echo "\n\t\t\t\tInvalid response!\c"
        sleep 2
        continue
        ;;
  esac
 done
