#!/bin/bash
#Just a reminder for the bash regex substituition syntax:
#${} sourrounds it all: $()
#then comes the "input var": $(input_var)
#followed by two slashes, indicating that a regex follows: $(input_var//)
#then comes the part to be replaced, should be POSIX regex: $(input_var//a)
#then, after another slash, comes the part it should be replaced with: $(input_var//a/b)
#there, done.

image_width=300
svg_name=flower.svg

#remove old file and make all layers visible
rm -rf flower_sprite.png
inkscape --verb LayerShowAll --verb FileSave --verb FileQuit $svg_name

#OPENING ANIMATION -----------------------------------------------------------------------------------------------
for i in {1..17}
do
	layerName=opening_$i
	layerId=$(grep -Pazo "(?s)<[^<]*?(label=\"$layerName\").*?>" $svg_name | sed -rn 's/id="(.*?)"/\1/p')
	#out with the extra spaces
	layerId=${layerId// /}
	echo $layerId
	inkscape $svg_name -z --export-id=$layerId --export-id-only --export-area-page --export-png=$layerName.png -w$image_width
	convert +append -background transparent flower_sprite_part.png $layerName.png flower_sprite_part.png
	rm $layerName.png
done

#append prefixed with a plus, appends files horizontally, prefixed with a minus it does so vertically
convert -append -background transparent flower_sprite.png flower_sprite_part.png flower_sprite.png
rm flower_sprite_part.png

#lather, rinse, repeat
