xkcd 1110: single images


Shell script to get today xkcd comic images:

dl-piece.sh
#!/bin/env sh
if [ $# -ne 1 ]; then
	echo 'needs at least one argument '
	exit 2
fi

[ -e "$1.png" ] && exit 0

wget -T 3 http://imgs.xkcd.com/clickdrag/$1.png
if [ $? -ne 0 ]; then
	touch $1.png
	exit 1
fi

declare -i vv
declare -i hv
eval `echo $1 | sed -e 's/\([0-9]*\)\(n\|s\)\([0-9]*\)\(e\|w\)/v=\4;vv=\3;h=\2;hv=\1;/'`

declare -i vs
declare -i hs
declare -i nvv
declare -i nhv
declare -i dl_failed=0

for order in up down right left; do

	nvv=$vv
	nhv=$hv
	nv=$v
	nh=$h

	case $order in
		right) vs=1; hs=0; ;;
		left) vs=-1; hs=0;  ;;
		up) vs=0; hs=1;  ;;
		down) vs=0; hs=-1;  ;;
		*) echo 'am i too stupid?';;
	esac
	nvv+=vs
	nhv+=hs
	if [ $nvv -eq 0 ]; then
		[ $v = 'w' ] && nv='e'
		[ $v = 'e' ] && nv='w'
		nvv=1
	fi

	if [ $nhv -eq 0 ]; then
		[ $h = 'n' ] && nh='s'
		[ $h = 's' ] && nh='n'
		nhv=1
	fi

	newfile=$nhv$nh$nvv$nv
	$0 $newfile
	dl_failed+=$?
	if [ $dl_failed -eq 4 ]; then
		exit 1
	fi
done
chmod u+x dl-piece.sh

# NOTICE: it downloads all file to current directory!
./dl-piece.sh 1n1e
find . -size 0 -exec rm -rf '{}' +