#!/bin/bash
# This program goes to a website, downloads an updated 'daily' picture
#  and sets your KDE background to that picture.
# Author: Luke Pahler
#  Many thanks to NASA/contributors for maintaining this 
#  killer webpage: http://antwrp.gsfc.nasa.gov/apod/archivepix.html

# http://antwrp.gsfc.nasa.gov/apod/ap070920.html
# http://antwrp.gsfc.nasa.gov/apod/ap070917.html
# http://antwrp.gsfc.nasa.gov/apod/ap070910.html
# Date Pattern: YYMMDD

#Calculate the URL to visit
# http://antwrp.gsfc.nasa.gov/apod/ap-=DATEVARIABLE=-.html
magicDate=`date +%y%m%d`

# Might as well store these pics in a tmp dir
mkdir -p /tmp/astronomyPicOfDay/${magicDate}
cd /tmp/astronomyPicOfDay/${magicDate}

urlToVisit=http://antwrp.gsfc.nasa.gov/apod/ap${magicDate}.html
echo I plan on visiting this URL: ${urlToVisit}

#Download the jpg from the page
# --ignore-case not implemented in GNU wget 1.10.2
wget -r -nd -np -l1 -A '*.jpg,*.jpeg,*.gif,*.JPG,*.JPEG,*.GIF' ${urlToVisit} &> /dev/null
# now you have the thumbnail and the large linked to one
# *note gifs aren't animated, but still display

#Get the image to use
# fancy ls to get the largest file
bgImage=`ls --sort=size | head -n 1`
bgImageFullPath=`pwd`/${bgImage}
echo I plan on using this image: ${bgImageFullPath}

#Set your background to it (learnkde.txt)
dcop --user `whoami` kdesktop KBackgroundIface setWallpaper ${bgImageFullPath} 5


