#!/bin/bash

#*****************************************************************************
#
#     $Id: notify_cellular.bash 513 2010-05-04 19:51:24Z c.schneider $
#     $URL: svn://ereo-bstt/DDN/TC/SDD_Bin/trunk/notify_cellular.bash $
#     $Author: c.schneider $
#     $Revision: 513 $
#     $Date: 2010-05-04 15:51:24 -0400 (Tue, 04 May 2010) $
#
#*****************************************************************************
ME=${0##*/}
FIRST=${0:0:1}
if [ "$FIRST" == '/' ]; then
  MY_PATH=$(dirname $0)
else
  MY_PATH=$(dirname $(pwd)/$0)
fi
export PATH=$MY_PATH:$PATH

HOST=mysql.midmdtriclub.org
USER=midmdtricluborg
PASSWD=*m^!^ag4
DB=midmdtriclub_org

LIST=/tmp/mmtc_to_$(basename $0)
RRPT=/home/wp_7j9yjb/midmdtriclub.org/pub/MMTC_Race_Reports.html
MAIL=/tmp/mmtc_mail_$(basename $0)


FROM="Mid Maryland Triathlon Club <rr@midmdtriclub.org>"
TO=carlandvalerie@gmail.com
TO="MMTC <mmtri@yahoogroups.com>"

#
# Routine:	main
#
echo $0: 1:$1, 2:$2
if [ -z "$1" ]; then
    DAYS=1
else
    DAYS=$1
fi

declare -a TGTS
ID_LIST=$(echo "SELECT ID FROM elogs WHERE Leg = 'Race' AND Athlete != 'test_user' AND WorkOut = 'Triathlon' AND Date < DATE(NOW()) AND Date LIKE '2018%' AND TO_DAYS(NOW()) - TO_DAYS(Modified) <= ${DAYS};" | mysql -h $HOST -u$USER -p$PASSWD midmdtriclub_org | egrep -v ^ID)

echo "$0: ID_LIST=$ID_LIST"

for ID in $ID_LIST
do
    ##echo ID=$ID
    TEXT=/tmp/$ME.$ID.txt
    MAIL=/tmp/$ME.$ID.dat

    echo "SELECT elogs.ID, user_email, display_name AS Athlete, Date, Workout, Leg, Miles, Time, LEFT(LEFT(Notes, -1+LOCATE(\"<\", CONCAT(Notes, \"<\"))),80) AS Title, Notes FROM elogs,wp_users WHERE elogs.ID = $ID AND Athlete = user_login \\G" | mysql -h $HOST -u$USER -p$PASSWD midmdtriclub_org | egrep -v '^\*\*\*' >> /tmp/$ME.$ID.txt

  ATHLETE=$(egrep    Athlete: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)
    EMAIL=$(egrep user_email: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)
     DATE=$(egrep       Date: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)
  WORKOUT=$(egrep    Workout: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)
      LEG=$(egrep        Leg: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)
    MILES=$(egrep      Miles: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)
    TITLE=$(egrep      Title: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)
     TIME=$(egrep       Time: /tmp/$ME.$ID.txt | head -1 | cut -d: -f2-)

      URL="https://www.midmdtriclub.org/cgi-bin/Races.pl?Go=$ID"

    cat > $MAIL <<EOF
From: $FROM
To: $TO
Subject: MMTC Race Report-$TITLE
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">
EOF
     echo "$ATHLETE,<br>"  >> $MAIL
     echo "$DATE,<br>"     >> $MAIL
     echo "$TITLE,<br>"    >> $MAIL
     echo "$URL,<br><br>"  >> $MAIL

     echo "SELECT Notes FROM elogs WHERE ID = $ID \\G" | mysql -h $HOST -u$USER -p$PASSWD midmdtriclub_org | egrep -v '^\*\*\*' >> $MAIL
     
     ENTRY="ID=$ID, ATHLETE=$ATHLETE, DATE=$DATE"
  EGREP=$(egrep "${ENTRY}" /tmp/$ME.log)
  ##echo $ME: EGREP=$EGREP
  if [ -z "$EGREP" ]; then
      ## send_email
      SUBJ="$ID, $ATHLETE, $WORKOUT, $LEG, $MILES, $TIME"
      MSG=/tmp/$ME.$ID.txt
      echo "URL: $URL" >> $MSG
      ##cat $MSG
      ##pause
      echo $SUBJ | /usr/sbin/sendmail -f "$FROM" $TO < $MAIL

      echo "$MSG sent to $TO for $ID"
      sleep 1
      echo NOO rm $MSG
      echo "$ENTRY" >> /tmp/$ME.log
  else
      echo "$ME: $ENTRY already sent"
  fi
done
exit
exit
exit
exit



echo "SELECT DISTINCT(user_email) FROM wp_usermeta, wp_users WHERE ID = user_id AND meta_key = 'wp_user_level' AND meta_value != 0;" | mysql -h $HOST -u $USER -p$PASSWD $DB > $LIST
wc $LIST $RRPT

for TO in $(cat $LIST)
do
    cat > $MAIL <<EOF
From: $FROM
To: $TO
Subject: MMTC Race Reports
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">
EOF

    cat $RRPT >> $MAIL
    /usr/sbin/sendmail -f "$FROM" $TO < $MAIL
    sleep 1
done
rm -v $LIST $MAIL
date
