#!/usr/bin/env bash

if [[ "$#" -ne 4 ]]
then
	echo Side by side image generator
	echo By Starbeamrainbowlabs
	echo -e "\nUsage:"
	echo "    ./genframes.sh [leftimage.ext] [rightext.ext] [step] [outdir]" 
	echo -e "\nNote that the output directory should *not* contain the trailing slash."
	exit
fi

i=0
step="$3"

imwidth=1680
imheight=1050

leftimage="$1"
rightimage="$2"

outdirprefix="$4/"

frame=0
while [ $(echo "$i <= 1" | bc) -eq 1 ]
do
	nextoffset=$(calc -p "(${imwidth} / 2) * ${i}")
	
	leftpixcount=$(calc -p "$i * $imwidth")
	rightpixcount=$(calc -p "$imwidth - $leftpixcount")
	
	framefilename=${outdirprefix}$(printf "%03d" "$frame").jpeg
	
	echo $frame: $leftpixcount / $rightpixcount $framefilename
	
	if [[ $(echo "$leftpixcount == 0" | bc) -eq 1 ]]
	then
		cp $rightimage $framefilename
	elif [[ $(echo "$rightpixcount == 0" | bc) -eq 1 ]]
	then
		cp $leftimage $framefilename
	else
		convert $leftimage -crop ${leftpixcount}x${imheight}+0+0 $rightimage -crop ${rightpixcount}x${imheight}+$leftpixcount+0 +append "$framefilename"
	fi
	
	frame=$(calc -p "$frame + 1")
	i=$(calc -p "$i + $step")
done


