#!/bin/bash
#
# Process_Queue.sh
#
# At this point we have a 2 Queues: 
# Qpass  == QUEUE_PASS - Password Queue has passwords and ids
# Qcheck == QUEUE_CHECK- Command Check Queue - has passwords, ids and commands

KEY="571"
PATH=$PATH:/bin:/usr/bin
export PATH

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)"
OUTPUT_FILE=$HOME/command_output.txt
#############################################################################

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


view_queue () {
echo There are $(cat $QUEUE_PASS  | wc -l ) items in the Qpass.  $QUEUE_PASS
echo There are $(cat $QUEUE_CHECK | wc -l ) items in the Qcheck. $QUEUE_CHECK
echo 

}

winner () {

echo 
echo "We have a winner"
echo Password in Qpass \($PASSWORD_IN_PASSWORD_QUEUE\) equals password in Qcheck \($PASSWORD_IN_CHECK_QUEUE\)
echo ID  in      Qpass \($ID_IN_PASSWORD_QUEUE\) equals\ ID in Qcheck \($ID_IN_CHECK_QUEUE\)
echo Command is  $COMMAND_IN_CHECK_QUEUE
echo 

}


remove_from_qcheck () {

sed -i  -e /"^$ID_IN_CHECK_QUEUE"/d $1

}


remove_from_qpass () {

sed -i -e /"^$ID_IN_PASSWORD_QUEUE"/d $1

}


view_queue
#############################################################################

#Read in $QUEUE_PASS file
while read PASS_LINE; 
do


    #Separate Passwords and IDs in the Password Queue
          ID_IN_PASSWORD_QUEUE=$(echo $PASS_LINE | cut -d "$DELIM1" -f1)
    PASSWORD_IN_PASSWORD_QUEUE=$(echo $PASS_LINE | cut -d "$DELIM1" -f2)


    #Check #1 - Is the value of the original sent passwd in the check Queue?
    if (grep ${PASSWORD_IN_PASSWORD_QUEUE:0:5} $QUEUE_CHECK > /dev/null ); then 

          while read CHECK_LINE; 
          do 

              #Separate Passwords/Command/and IDs in the Check Queue
              PASSWORD_IN_CHECK_QUEUE=$(echo $CHECK_LINE | cut -d "$DELIM1" -f2)
               COMMAND_IN_CHECK_QUEUE=$(echo $CHECK_LINE | cut -d "$DELIM1" -f3)
                  HOST_IN_CHECK_QUEUE=$(echo $CHECK_LINE | cut -d "$DELIM1" -f4)
                    ID_IN_CHECK_QUEUE=$(echo $CHECK_LINE | cut -d "$DELIM1" -f1)


              #Check #2 
              #Add key to password in Qcheck to get Final passwd 
              FINAL_PASSWD="$PASSWORD_IN_CHECK_QUEUE$KEY"

              #Compare password sent to final expected passwd
              if (( "$PASSWORD_IN_PASSWORD_QUEUE" == "$FINAL_PASSWD" )); then

                        #Check #3
                        if (( "$ID_IN_CHECK_QUEUE" == "$ID_IN_PASSWORD_QUEUE" ));then

                          winner; 

                          if [[ -n "$HOST_IN_CHECK_QUEUE" ]];then

                           #process remotely
                           ./whitelist.pl "$COMMAND_IN_CHECK_QUEUE" > $OUTPUT_FILE 2>&1   && \
                               ssh "$HOST_IN_CHECK_QUEUE"  "$COMMAND_IN_CHECK_QUEUE" > $OUTPUT_FILE 2>&1

                          else

                           #process locally
                           ./whitelist.pl "$COMMAND_IN_CHECK_QUEUE" > $OUTPUT_FILE 2>&1   && \
                               eval "$COMMAND_IN_CHECK_QUEUE" > $OUTPUT_FILE 2>&1

                          fi


                           echo Sending results to $RECP
                           cat $OUTPUT_FILE | mail -s "$COMMAND_IN_CHECK_QUEUE"-"$ID_IN_CHECK_QUEUE"-res  "$RECP"

                          remove_from_qcheck $QUEUE_CHECK
                          remove_from_qpass  $QUEUE_PASS
                        fi
               else

                    echo No Match Password in Qpass $PASSWORD_IN_PASSWORD_QUEUE not same as password in Qcheck $PASSWORD_IN_CHECK_QUEUE

               fi

          done < $QUEUE_CHECK

  else 

    echo  Password  in Qpass not even seen in Check Q

  fi
   


done < $QUEUE_PASS

view_queue
