#!/bin/bash

in_file="${1}"
test -n "${in_file}" || exit 1
test -r "${in_file}" || exit 2

out_dir="${2}"
test -n "${out_dir}" || exit 3
mkdir -p "${out_dir}" || exit 4

# We are now going to generate 20 pictures based on in_file, assuming it is a
# 100x100 PNG picture with transparent background, showing a face looking
# towards the left.

function proceed {
	local out_file="${1}"
    shift
    convert "${in_file}" -alpha 'set' "$@" "${out_dir}/${out_file}.png"
}

# C000: moving to the right ( = flop), downward (= rotate 45 deg)
proceed C000 \( +clone -background 'none' -flop -rotate '45' \) -gravity 'center' -compose 'Src' -composite
# C001: moving to the right ( = flop), downward (= rotate 45 deg), with a slight bounce
proceed C001 \( +clone -background 'none' -flop -rotate '45' \) -gravity 'center' -compose 'Src' -composite -distort SRT '0,0 1,1 0 5,-5'

# C010: moving to the right (= flop), no vertical movement
proceed C010 -flop
# C011: moving to the right (= flop), no vertical movement, with a slight bounce
proceed C011 -flop -distort SRT '0,0 1,1 0 0,-5'

# C020: moving to the right ( = flop), upward (= rotate -45 deg)
proceed C020 \( +clone -background 'none' -flop -rotate '-45' \) -gravity 'center' -compose 'Src' -composite
# C021: moving to the right ( = flop), upward (= rotate -45 deg), with a slight bounce
proceed C021 \( +clone -background 'none' -flop -rotate '-45' \) -gravity 'center' -compose 'Src' -composite -distort SRT '0,0 1,1 0 5,5'

# C100: no horizontal movement, moving downward
proceed C100 -distort SRT '0,0 1,1 0 -3,0'
# C101: no horizontal movement, moving downward, with a slight bounce
proceed C101 -distort SRT '0,0 1,1 0 +3,0'

# C110: no horizontal movement, no vertical movement
proceed C110
# C111: no horizontal movement, no vertical movement, with a slight bounce
proceed C111 -distort SRT '0,0 1,1 0 0,-5'

# C120: no horizontal movement, moving upward
proceed C120 -distort SRT '0,0 1,1 0 +3,0'
# C121: no horizontal movement, moving downward, with a slight bounce
proceed C121 -distort SRT '0,0 1,1 0 -3,0'

# C200: moving to the left, moving downward
proceed C200 \( +clone -background 'none' -rotate '-45' \) -gravity 'center' -compose 'Src' -composite
# C201: moving to the left, moving downward, with a slight bounce
proceed C201 \( +clone -background 'none' -rotate '-45' \) -gravity 'center' -compose 'Src' -composite -distort SRT '0,0 1,1 0 5,5'

# C210: moving to the left, no vertical movement
proceed C210
# C211: moving to the left, no vertical movement, with a slight bounce
proceed C211 -distort SRT '0,0 1,1 0 0,-5'

# C220: moving to the left, moving upward
proceed C220 \( +clone -background 'none' -rotate '45' \) -gravity 'center' -compose 'Src' -composite
# C221: moving to the left, upward (= rotate 45 deg), with a slight bounce
proceed C221 \( +clone -background 'none' -rotate '45' \) -gravity 'center' -compose 'Src' -composite -distort SRT '0,0 1,1 0 -5,5'

# sleep1: do not sleep, bouncing
proceed 'sleep1' -distort SRT '0,0 1,1 0 0,-5'
# sleep2: do not sleep, keep bouncing
proceed 'sleep2' -distort SRT '0,0 1,1 0 0,5'
