#!/usr/bin/env zsh
# optimise gifs with gifsicle

local before
local after
local ofile

for i in $@; do
	if [[ -f "$i" ]]; then
		ofile="$i.out"
		while [[ -f "$ofile" ]]; do
			ofile="$ofile.out"
		done
		du -h "$i" | cut -f 1 | read before
		echo "$i..."
		gifsicle --batch --no-ignore-errors -w -O3 -i "$i" \
		&& du -h "$i" | cut -f 1 | read after \
		&& echo "   $before -> $after" || echo "  failed"
	fi
done