#!/bin/bash
#
# Listing 5.
# Send_pass.sh
# 
echo ------------------------------
echo $0
echo ------------------------------

      QUEUE=$HOME/queue.txt
 QUEUE_PASS=$HOME/queue_pass.txt
QUEUE_CHECK=$HOME/queue_check.txt
DELIM1="+"
RECP="$(cat recp.conf)"

#######################################################################
[[ -e $QUEUE ]]  || { echo $0:$QUEUE  does not exist ; exit 1; }

echo Reading Q $QUEUE ...
while read LINE; do

     ID=$(echo $LINE  | cut -d "$DELIM1" -f1)
   PASS=$(echo $LINE  | cut -d "$DELIM1" -f2)
   HOST=$(echo $LINE  | cut -d "$DELIM1" -f4)
COMMAND=$(echo $LINE  | cut -d "$DELIM1" -f3)

echo  Sending subject of $ID-$PASS to $RECP  with body of "$COMMAND $HOST"
echo "$COMMAND $HOST" | mail -s "$ID$DELIM1$PASS"  "$RECP"


done < $QUEUE

echo Moving contents of Q to  Qcheck $QUEUE \-\> $QUEUE_CHECK
cat  $QUEUE >> $QUEUE_CHECK

echo
echo QC:Contents of Qcheck $QUEUE_CHECK:
[[ -e $QUEUE_CHECK ]] && cat $QUEUE_CHECK

echo
echo QP:Contents of Qpass $QUEUE_PASS:
[[ -e $QUEUE_PASS ]] && cat $QUEUE_PASS


rm -v $QUEUE
echo 
