#! /bin/bash
tex_path="$1"
count="$2"
use_style="$3"

. scripts/inc/assets.sh

if [ "$tex_path" = "" ] || [ "$count" == "" ]
then
    echo >&2 "USAGE: scripts/repeat-block-anim.sh <tex-path> <count> [use-style]"
    echo >&2 ""
    echo >&2 "Given a texture path, relative to each of the block tex"
    echo >&2 "dirs, repeat its animation <count> times, in each component."
    echo >&2 "If no animation metadata currently exists, it is also created."
    echo >&2 "If use-style is given, it names the style to be used by the"
    echo >&2 "newly-created meta; otherwise, a template is entered."
    exit 1
fi

if [ ! -e "assets/blocktex/$tex_path.png" ]
then
    echo >&2 "Texture '$tex_path' does not exist."
    exit 1
fi

for subdir in $blocktex_subdirs
do
    this_img="assets/$subdir/$tex_path.png"
    if [ -e "$this_img" ]
    then
        input_images=""
        for _ in `seq 1 $count`
        do
            input_images="$input_images $this_img"
        done

        echo "Repeating animation: $this_img"
        montage -mode concatenate -tile 1x$count $input_images $this_img || exit 1
    fi
done

meta_path="assets/blockmeta/$tex_path.meta"
if [ ! -e "$meta_path" ]
then
    echo "Creating meta file: $meta_path"
    mkdir -p "`dirname $meta_path`" || exit 1
    echo "# '$tex_path' animation." > $meta_path

    if [ "$use_style" = "" ]
    then
        echo "frameDurationMS=100" >> $meta_path
    else
        echo "@$use_style" >> $meta_path
    fi
fi