#!/bin/bash

# c2016 by Paul Sherman http://www.pcbypaul.com 
# uses imagemagick (smaller grayscale than GIMP, posterized), then pngcrush,
# and fnally exiftool to put in ps, wpclipart stuff.

if [ "$1" == "" ]; then
  $dialog --title "No PNG to convert" --msgbox "\nThis utility needs to be passed the filename of a PNG image, including the path.\n" 0 0
  exit 0
fi

ret=`which pngcrush`
if [ ! "$ret" == "/usr/bin/pngcrush" ]; then
	$dialog --title "PNG Crush not found..." \
        	--msgbox "PNG Crush needs to be installed for this conversion.\n" 0 0
	exit 0
fi

pth=`dirname "$1"`
cd $pth


ret=`identify -verbose "$1" | grep Colorspace`

if [[ $ret == *"Gray"* ]]
then
	Xdialog --title "It begins" --infobox "hang on a second.\n" 12 50 1000
	tmpfile="/tmp/1.png"
	convert "$1" -colorspace gray +dither -posterize 15 "$tmpfile"
	pngcrush -reduce -brute -q "$tmpfile" "$1"  2>/dev/null
	exiftool -Comment="Edited by Paul Sherman for WPClipart, Public Domain" -overwrite_original_in_place "$1"
	Xdialog --title "DONE" --infobox "$1 \nhas been reduced to 16 shades of gray\n" 16 90 4000
	paplay /usr/share/sounds/opcomp.wav
	exit 0
fi

	Xdialog --title "HEY" --infobox "image was not grayscale to begin with...\n" 0 0 1000
	