#!/bin/sh
# Images to .anme
## version 0.5 (090317)
## Created by David Rylander, 2009, www.rylanderanimation.se
## Creative Commons Attribution-Share Alike 3.0 licenced (http://creativecommons.org/licenses/by-sa/3.0/)

##### Variables #####
mypath="`pwd`"  #get current path.
foldertype=$(zenity  --list  --text "What kind of folder would you like to place the images in?" --radiolist  --column "Choose" --column "Foldertype;" TRUE "Group" FALSE "Bone" FALSE "Switch" FALSE "None"); echo $foldertype
docwidth=0
docheight=0
#Remove existing anme-file.
#TODO - check if it exists, if so add padding to filename, check which padding already exists.
if [ -f image-export.anme ]
then
rm -f image-export.anme
fi

#set doc-size to match images.
for filename in "$@"
do
imgsize=$(identify $filename | cut -d ' ' -f 3)
imgwidth=`echo $imgsize | cut -d x -f 1`	
imgheight=`echo $imgsize | cut -d x -f 2`
if [ $imgwidth -gt $docwidth ]; then
docwidth=$imgwidth
fi
if [ $imgheight -gt $docheight ]; then
docheight=$imgheight
fi
done

cat >> image-export.anme << ONE
application/x-vnd.lm_mohodoc
version 15
### Created by ImagesToAnme nautilus script, CC 2009 David Rylander
### Created: Yes it is created.

### static values
ONE
echo "dimensions $docwidth $docheight" >> image-export.anme
cat >> image-export.anme <<TWO
frame_range 1 72
fps 25
back_color 234 234 234 255

TWO

# find out layertype to create
if  [ $foldertype = Group ]
then
layertype=3
elif  [ $foldertype = Bone ]
then
layertype=4
elif  [ $foldertype = Switch ]
then
layertype=5
fi

#create a folder for the images.
if [ $layertype ] 
then
echo "layer_type $layertype" >> image-export.anme
cat >> image-export.anme << THREE
{
	name "imported-images"
expanded true

THREE
fi

#create image layers.
for filename in "$@"		
do				#TODO - check so it's really an image-file! -With file; file -ib
imgsize=$(identify $filename | cut -d ' ' -f 3)
imgwidth=`echo $imgsize | cut -d x -f 1`	
imgheight=`echo $imgsize | cut -d x -f 2`
imgwidthshape=$(echo "2.0 * $imgwidth / $docheight" | bc -l)
imgheightshape=$(echo "2.0 * $imgheight / $docheight" | bc -l)
layername=`echo $filename | sed 's/\..\{3\}$//'`
cat >> image-export.anme << FOUR
layer_type 2
{
FOUR
echo "name \"$layername"\" >> image-export.anme
echo "image \"$filename"\"  >> image-export.anme
echo "shape $imgwidthshape $imgheightshape"  >> image-export.anme
cat >> image-export.anme << FIVE	
	translation
	[
		keys 1
			0 3 0.1 0.5 0 0 0
	]
}
FIVE
done

#end the folder if any.
if [ $layertype ] 
then
echo "}" >> image-export.anme
fi
#zenity --info --text "done!"