#!/bin/bash
########################################################################################################
#                                                                                                      #
#                                            howtogeek.sh                                              #
#                                                                                                      #
#  This is a script designed to download the daily geek comic from www.howtogeek.com and place a copy  #
#  on the desktop.  New comics are posted daily Monday through Friday.                                 #
#                                                                                                      #
#  Create a "howtogeek" directory, place this file in it and give user RWX permission.                 #
#  Default location is the user's home directory, edit the first line if other location.               #
#                                                                                                      #
#  Created Sep 24, 2011                                                                                #
#                                                                                                      #
#  Changelog                                                                                           #
#  Rev Sep 26, 2011 change image destination to archive directory - image now named $DATE.jpg          #
#  Rev Sep 27, 2011 automated creation of archive directory                                            #
#  Rev Nov 15, 2011 fixed problem with extracting $CODE variable                                       #
#  Rev Dec 12, 2011 fixed same problem, looks like site went back to old code from November?           #
#  Rev May 12, 2012 Removed day check portion as site now has comics 7 days per week.                  #
#                                                                                                      #
########################################################################################################

PLACE=${HOME}/howtogeek                          # edit this line for location of howtogeek directory

[ ! -d /$PLACE/archive ] && mkdir /$PLACE/archive     # create archive directory if nonexistant

DATE=$(date +'%Y%m%d')                           # create DATE variable for today similar to 20110923

cd $PLACE/                                       # change to the howtogeek directory


# download the www.howtogeek.com home page and extract the URL for today's comic file image file

wget -T 20 -t 2 http://www.howtogeek.com/

grep -m 1 "geek-comic" index.html > tmp1.tmp     # grep first occurance of "geek-comic"

sed 's/^.*uploads//' tmp1.tmp > tmp2.tmp         # delete start of line to comic date and image data

CODE=$(sed -e 's/".*$//g' tmp2.tmp)              # delete after date and image data, create CODE variable
  

echo $CODE

# next the comic image file is downloaded to the archive directory, file is renamed to $DATE.jpg

wget -T 20 -t 2 http://www.howtogeek.com/wp-content/uploads$CODE -O $PLACE/archive/$DATE.jpg


# today's comic is copied to the desktop, leaving downloaded file in the howtogeek archive directory

cp $PLACE/archive/$DATE.jpg /${HOME}/Desktop     # copy today's comic to the desktop

rm *.html                                        # delete html files

rm *.tmp                                         # delete temporary files


