#!/bin/bash

mkdir /tmp/wotd
cd /tmp/wotd

#wget -r -l1 -p --convert-links http://www.oed.com/cgi/display/wotd
wget  -E -H -k -K -p --convert-links http://www.oed.com/cgi/display/wotd
#if wotd.html exists already wget does mv wotd wotd.html
# but if I clean up need to mv self
mv www.oed.com/cgi/display/wotd  www.oed.com/cgi/display/wotd.html
#gets wotd, inside frames of wotd are wotd?bleh but already thinks it has it.


#grep "SRC=" /tmp/wotd/www.oed.com/cgi/display/wotd.html  |sed s/.*SRC=/SRC=/
grep "SRC=" www.oed.com/cgi/display/wotd.html  |sed s/.*SRC=/SRC=/ |cut -d \" -f 2 |grep http |sed "s/http/wget  -E -H -k -K -p --convert-links http/" >getrest
chmod 755 getrest
./getrest

cd www.oed.com/cgi/display/
mv wotd.html wotd
sed "s/SRC=\"http:\/\/www.oed.com\//SRC=\"..\/..\//" wotd >wotd.html

ls -al /tmp/wotd/www.oed.com/cgi/*/


#TODO check success and that entry_main exists and changes have not been made

# mail what is in here
cd /tmp/wotd/www.oed.com/cgi
wotdhfile=`ls display_header/wotd*.html`
wotdefile=`ls entry_main/wotd*.html`
mv $wotdhfile wotdhfile.html
mv $wotdefile wotdefile.html

#mail -s "www.oed.com wotd" jamesc@dspsrv.com
echo "http://www.oed.com/cgi/display/wotd">content.txt
echo ".">>content.txt

#TODO -F option to get mail send without dspsrv
touch blankmuttrc
echo "set hostname=\"doolin.com\"" > blankmuttrc
echo "my_hdr From: jamesc@doolin.com (James Coleman)" >> blankmuttrc
echo "set envelope_from=yes" >> blankmuttrc

#mutt -nx -s "www.oed.com wotd" -a $wotdfile -i content.txt jamesc@dspsrv.com <content.txt
#mutt -nx -F blankmuttrc -s "www.oed.com wotd" -a $wotdhfile -a $wotdefile jamesc@dspsrv.com <content.txt
#mutt -nx -F blankmuttrc -s "www.oed.com wotd" -a display_header/wotd*.html -a entry_main/wotd*.html jamesc@dspsrv.com <content.txt
mutt -nx -F blankmuttrc -s "www.oed.com wotd" -a wotdhfile.html -a wotdefile.html jamesc@dspsrv.com <content.txt


mkdir oldfiles
#mv $wotdhfile $wotdefile oldfiles/
#mv display_header/wotd*.html entry_main/wotd*.html oldfiles/
mv wotd*.html oldfiles/

#mv to backup





#sudo tail -n 100 /var/log/maillog
#Nov 19 12:37:47 betty sendmail[22585]: hAJCblod022585: from=jamesc, size=19510, class=0, nrcpts=1, msgid=<20031119123747.GA22583@doolin.com>, relay=jamesc@localhost
#Nov 19 12:37:47 betty sendmail[22587]: hAJCblZZ022587: from=<jamesc@betty.dev.ie.alphyra.com>, size=19668, class=0, nrcpts=1, msgid=<20031119123747.GA22583@doolin.com>, proto=ESMTP, daemon=MTA, relay=localhost.localdomain [127.0.0.1]
#Nov 19 12:37:47 betty sendmail[22585]: hAJCblod022585: to=jamesc@dspsrv.com, ctladdr=jamesc (500/500), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30304, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (hAJCblZZ022587 Message accepted for delivery)
#Nov 19 12:38:09 betty sendmail[22409]: hAJAhLZZ020338: to=<jamesc@dspsrv.com>, ctladdr=<jamesc@betty.dev.ie.alphyra.com> (500/500), delay=01:54:48, xdelay=00:02:32, mailer=esmtp, pri=210462, relay=mxbackup.esat.net. [193.95.141.39], dsn=4.0.0, stat=Deferred: 451 rejected: temporarily unable to verify sender address (try again later) <jamesc@betty.dev.ie.alphyra.com>
#bash$ sudo mailq
#                /var/spool/mqueue (9 requests)
#-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
#hAJCXJZZ022419     4574 Wed Nov 19 12:33 <jamesc@betty.dev.ie.alphyra.com>
#                 (Deferred: 451 rejected: temporarily unable to verify sender )
#                                         <jamesc@dspsrv.com>
#hAJCblZZ022587*   19206 Wed Nov 19 12:37 <jamesc@betty.dev.ie.alphyra.com>
#                                         <jamesc@dspsrv.com>
