#!/bin/sh
TARGET_DIRECTORY=${TARGET_DIRECTORY?-out}
INPUT=$1
PACK=`basename $INPUT`
mkdir -p "$TARGET_DIRECTORY"
printf "Writing $PACK..."
printf "{\"$PACK\":{" > "$TARGET_DIRECTORY/$PACK.json"
shopt -s nullglob
for i in $INPUT/*.png $INPUT/*.jpg $INPUT/*.gif $INPUT/*.svg; do
	FILE="$(basename "${i%.*}")"
	OUTNAME="$(sha256sum "$i" | awk '{ print $1 }').${i##*.}"
	cp "$i" "$TARGET_DIRECTORY/$OUTNAME"
	printf "$FILE..."
	printf "\"$FILE\":\"$OUTNAME\"," >> "$TARGET_DIRECTORY/$PACK.json"
done
printf "DONE!\n"
sed -i '$ s/.$//' "$TARGET_DIRECTORY/$PACK.json"
printf "}}" >> "$TARGET_DIRECTORY/$PACK.json"
