#!/usr/bin/env bash


# XXX add help...
# XXX add better options...


FPS=${FPS:-8}
if [ $1 == "-r" ] ; then
	FPS=$2
	shift 2
fi

PATTERN=${PATTERN:-'*.jpg'}

# generate name...
LST=($PATTERN)
A=${LST[0]}
A=${A%.jpg}
B=${LST[$(( ${#LST[@]} - 1 ))]}
B=${B%.jpg}

NAME=${NAME:-${A}-${B}}

PALETTE=${PALETTE:-.palette.png}


# generate...
{
	set -o noglob
	# gif palette...
	ffmpeg \
		-pattern_type glob \
			-i $PATTERN \
		-vf palettegen \
		"$PALETTE"
	# gif...
	ffmpeg \
		-r $FPS \
		-pattern_type glob \
			-i $PATTERN \
		-i "$PALETTE" \
		-filter_complex paletteuse=dither=none \
		"${NAME}".gif
	# cleanup...
	rm "$PALETTE"

	# mkv...
	ffmpeg \
		-r $FPS \
		-pattern_type glob \
			-i $PATTERN \
		"${NAME}".mkv
	#set +o noglob
}

