#!/bin/sh
# Copyright (c) 2007 Timothy Redaelli <tredaelli@inventati.org>
# Distributed under the terms of the GNU General Public License v2
# $Id$

URL=$1

download()
{
	wget -U "" -nv "$@"
}

parse()
{
	wget -U "" http://www.deviantart.com/download/"$URL"/ && exit 0
	URLS=$(wget -qU "" -O - http://www.deviantart.com/deviation/"$URL"/ | fgrep 'deviantART.pageData' | sed -e 's/^.*"fullview":{[^}]*"\(http[^"]*\).*$/\1/' -e 's/\\//g' | awk -F / '{for (i = 0; i <= 0xF; i++) for (j = 0; j <= 0xF; j++) printf "http://fc01.deviantart.com/%s/f/%s/%s/%x/%x/%s\n", $4, $6, $7, i, j, $10}')
}

parse "$1"

echo "$URLS" | while read x; do
	download "$x" && exit 0
done
