#!/bin/sh

convert=no

# Read ppm image on stdin, spit out scaled, color-reduced ppm image on stdout
makeicon()
{
	pnmscale -xysize 48 32 | ppmquant 16
#	pnmscale -xysize 48 32 | ppmdither -dim 4 -red 2 -green 3 -blue 2
}

makeone()
{
	makeicon | ppmtoxpm > "$icon"
	convert=yes
}

# Create a subdirectory called .xfiler, if it doesn't already exist.
# If we can't create the directory, fail.
#echo creating .xfiler
if [ ! -d .xfiler ]; then
	mkdir .xfiler || exit 1
fi

# If we don't have netpbm, we can't do anything, can we
#echo looking for netpbm
test -x "`which pnmscale`" || exit 1

for f in *; do
	icon=".xfiler/$f.xpm"
#	if [ -f "$icon" -a "$icon" -nt "$f" ]; then
#		continue
#	fi
#	echo Processing $f...
	case $f in
	*.jpg | *.jpeg | *.JPG | *.JPEG )
		djpeg $f | makeone $f
		;;
	*.gif | *.GIF )
		giftopnm $f | makeone $f
		;;
	*.tif | *.tiff | *.TIF | *.TIFF )
		tifftopnm $f | makeone $f
		;;
	*.png | *.PNG )
		pngtopnm $f | makeone $f
		;;
	*.bmp | *.BMP )
		bmptoppm $f | makeone $f
		;;
	*.xpm | *.XPM )
		sed -e s/none/grey/g -e s/None/grey/g $f | xpmtoppm | makeone $f
		;;
	*.ppm | *.PPM | *.pnm | *.PNM )
		cat $f | makeone $f
		;;
	# Don't try to do anything with unknown extensions
	esac
done 2> /dev/null

if [ $convert = no ]; then
	exit 1
fi

