#!/bin/bash

	avatar_factory_PATH=${0%%/themes/*}

	destination=$1
	grabber_picture=$2
	grabber_picture2=$3
	grabber_picture3=$4

	create_theme ()	{
		convert "$1" \
			-background  none   -rotate $rotate \
			\( +clone -background black -shadow 45x2+2+2 -channel A -evaluate multiply 2 +channel \) \
			+swap +repage -gravity center -geometry -0-1 -composite \
			"$1"
	}

	#If there are 3 pictures:
	if [[ -n "$grabber_picture3" ]]; then
		convert "$grabber_picture"  -thumbnail 62x62 /tmp/thumb$$.png
		convert "$grabber_picture2"  -thumbnail 62x62 /tmp/thumb2$$.png
		convert "$grabber_picture3"  -thumbnail 62x62 /tmp/thumb3$$.png
		picture_aspect=$(echo "scale=2; $(identify -format %w /tmp/thumb$$.png)/$(identify -format %h /tmp/thumb$$.png)*100" | bc | cut -d. -f 1)
		picture_aspect2=$(echo "scale=2; $(identify -format %w /tmp/thumb2$$.png)/$(identify -format %h /tmp/thumb2$$.png)*100" | bc | cut -d. -f 1)
		picture_aspect3=$(echo "scale=2; $(identify -format %w /tmp/thumb3$$.png)/$(identify -format %h /tmp/thumb3$$.png)*100" | bc | cut -d. -f 1)

		icon_size=128x90

		if [ "$picture_aspect3" -gt 105 ]; then
			rotate=-120
		else
			rotate=-30
		fi

		create_theme /tmp/thumb3$$.png

		if [ "$picture_aspect2" -gt 105 ]; then
			rotate=-90
		else
			rotate=0
		fi

		create_theme /tmp/thumb2$$.png

		if [ "$picture_aspect" -gt 105 ]; then
			rotate=-60
		else
			rotate=30
		fi

		create_theme /tmp/thumb$$.png

		convert  \
			   -size $icon_size null: \
			   /tmp/thumb3$$.png -composite \
			   /tmp/thumb2$$.png -gravity north -composite \
			   /tmp/thumb$$.png -gravity northeast -composite \
			   "$destination"

		rm /tmp/thumb$$.???
		rm /tmp/thumb2$$.???
		rm /tmp/thumb3$$.???

	#If there are 2 pictures:
	elif [[ -n "$grabber_picture2" ]]; then
		convert "$grabber_picture"  -thumbnail 62x62 /tmp/thumb$$.png
		convert "$grabber_picture2"  -thumbnail 62x62 /tmp/thumb2$$.png
		picture_aspect=$(echo "scale=2; $(identify -format %w /tmp/thumb$$.png)/$(identify -format %h /tmp/thumb$$.png)*100" | bc | cut -d. -f 1)
		picture_aspect2=$(echo "scale=2; $(identify -format %w /tmp/thumb2$$.png)/$(identify -format %h /tmp/thumb2$$.png)*100" | bc | cut -d. -f 1)

		icon_size=128x90

		if [ "$picture_aspect2" -gt 105 ]; then
			rotate=-120
		else
			rotate=-30
		fi
		create_theme /tmp/thumb2$$.png

		if [ "$picture_aspect" -gt 105 ]; then
			rotate=-90
		else
			rotate=0
		fi
		create_theme /tmp/thumb$$.png

		convert  \
			   -size $icon_size null: \
			   /tmp/thumb2$$.png -composite \
			   /tmp/thumb$$.png -gravity north -composite \
			   "$destination"

		rm /tmp/thumb$$.???
		rm /tmp/thumb2$$.???

	#If there is 1 picture:
	else
		convert "$grabber_picture"  -thumbnail 62x62 /tmp/thumb$$.png
		picture_aspect=$(echo "scale=2; $(identify -format %w /tmp/thumb$$.png)/$(identify -format %h /tmp/thumb$$.png)*100" | bc | cut -d. -f 1)

		icon_size=128x90

		if [ "$picture_aspect" -gt 105 ]; then
			rotate=-120
		else
			rotate=-30
		fi
		create_theme /tmp/thumb$$.png

		convert  \
			   -size $icon_size null: \
			   /tmp/thumb$$.png -composite \
			   "$destination"

		rm /tmp/thumb$$.???

	fi

