#!/bin/bash

# c2015 by Paul Sherman http://www.pcbypaul.com 
# just a small wrapper to convert a png to an svg using potrace

if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
	dialog="Xdialog"
else
	dialog="dialog"
fi

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 potrace`
if [ ! "$ret" == "/usr/bin/potrace" ]; then
	$dialog --title "Potrace not found..." \
        	--msgbox "Potrace needs to be installed for this conversion.\n" 0 0
	exit 0
fi


pth=`dirname "$1"`
cd $pth
mv "$1" `echo $1 | tr ' ' '_' | tr A-Z a-z` 2>/dev/null
tmpfile=`echo "$1" | sed "s/\.png/\.pnm/g"`
svgfile=`echo "$1" | sed "s/\.png/\.svg/g"`

convert "$1" "$tmpfile"
potrace -s -o "$svgfile" "$tmpfile"
rm "$tmpfile"
sed -i '/<metadata>/a Image by Paul Sherman\nPublished by wpclipart.com\nLicense is Public Domain' "$svgfile"
