#!/bin/bash


type pngquant 2> /dev/null > /dev/null
if [ $? -ne 0 ]
then
	echo "ERROR: pngquant not found."
	exit 1
fi

dpkg --compare-versions $(pngquant -V | cut -d' ' -f1) gt 1.8.2
if [ $? -ne 0 ]
then
	echo "ERROR: pngquant is too old. 1.8.3 or newer needed."
	exit 1
fi

pngquant *.png

origAmount=$(ls *.png | grep -v fs8 | wc -l)
convAmount=$(ls *fs8.png | wc -l)


if [ $origAmount -ne $convAmount ]
then
	echo "ERROR: The amount of original and converted PNGs differs."
	exit 1
fi

ls *-fs8.png | while read old
do
	new=$(echo -n $old | sed 's/-fs8//')
	mv "$old" "$new"
done