#!/bin/sh
TARGET_DIRECTORY=${TARGET_DIRECTORY?-out}
INPUT=$1
_BASEDIR=`basename $INPUT`
PACK=${_BASEDIR%.*}
printf "{\"$PACK\":{" > "$TARGET_DIRECTORY/$PACK.json"
OUTNAME="$(sha256sum "$1" | awk '{ print $1 }').${1##*.}"
cp "$1" "$TARGET_DIRECTORY/$OUTNAME"
echo "Writing frame '$PACK'..."
printf "\"frame\":\"$OUTNAME\"," >> "$TARGET_DIRECTORY/$PACK.json"
if [ ! -z $5 ]; then
	printf "\"size\":[$2,$3,$4,$5]" >> "$TARGET_DIRECTORY/$PACK.json"
elif [ ! -z $3 ]; then
	printf "\"size\":[$2,$3]" >> "$TARGET_DIRECTORY/$PACK.json"
else
	printf "\"size\":[$2]" >> "$TARGET_DIRECTORY/$PACK.json"
fi
printf "}}" >> "$TARGET_DIRECTORY/$PACK.json"