#! /usr/bin/env bash
# Download script written by Sebastian Pipping <sebastian@pipping.org>
# Licensed as Public Domain
# 2009-04-20
if ! which convert &>/dev/null ; then
	echo "ERROR: Command 'convert' of ImageMagick required but missing."
	exit 1
fi

hoxify() {
	case $1 in
	elephantrev2)
		piece=elephant
		;;
	envoy|guard)
		piece=advisor
		;;
	tank|rook)
		piece=chariot
		;;
	*)
		piece=$1
		;;
	esac
	echo ${piece}
}

chessvariants_com_dir() {
	case $1 in
	elephantrev2)
		dir=alfaerie3
		;;
	envoy)
		dir=alfaerie4
		;;
	*)
		dir=alfaerie
		;;
	esac
	echo ${dir}
}

summary() {
	pwd
	ls -al
	echo
}

mkdir -p pragmaticlogic_com_50x50
(
	cd pragmaticlogic_com_50x50 || exit 1

	PIECES="cannon elephant guard horse king pawn tank"
	for p in ${PIECES} ; do
		piece=$(hoxify ${p})
	
		if [[ ! -f b${piece}.png ]]; then
			wget http://ajaxchess.pragmaticlogic.com/img/${p}_light.png -O b${piece}.png
		fi
		if [[ ! -f r${piece}.png ]]; then
			wget http://ajaxchess.pragmaticlogic.com/img/${p}_dark.png -O r${piece}.png
		fi
	done

	summary
)

mkdir -p chessvariants_com_western_50x50
(
	cd chessvariants_com_western_50x50 || exit 1

	PIECES="cannon elephantrev2 envoy horse king pawn rook"
	for p in ${PIECES} ; do
		piece=$(hoxify ${p})
		dir=$(chessvariants_com_dir ${p})
		
		if [[ ! -f b${piece}.png ]]; then
			wget http://www.chessvariants.com/graphics.dir/${dir}/b${p}.gif -O b${piece}.gif
			convert b${piece}.gif b${piece}.png && rm -f b${piece}.gif
		fi
		if [[ ! -f r${piece}.png ]]; then
			wget http://www.chessvariants.com/graphics.dir/${dir}/w${p}.gif -O r${piece}.gif
			convert r${piece}.gif r${piece}.png && rm -f r${piece}.gif
		fi
	done

	summary
)
