#!/bin/sh
# GNU public license
# Written by Philippe Serbryns <ps@telin.rug.ac.be>
# This tool is an example to convert "*.pgm" to any other graphics format when
# in ImageMagick available.

NAME=$0

function usage {
 echo "Usage: $NAME -format \"ImageMagick_known_format\" [ImageMagick enhance options] PGM-files"
 exit 1 }

if [ $# -lt 3 ]; then
echo "$NAME: wrong parameters"
usage
fi

OPTIONS=""
while [ 1 -eq 1 ]; do
case $1 in
-format) FORMAT=$2
	 case $FORMAT in
	 -*) echo "$FORMAT: wrong format"
	     usage
         esac
	 shift
	 ;;
-*) OPTIONS="$OPTIONS $1"
    ;;
*.pgm) if [ -f $1 ]; then break; fi
	echo "$1: file not found"
        usage
        ;;
*) echo "$1: no PGM file"
             usage ;;
esac
shift
done

for i in $*
do 
j=`basename $i .pgm`.$FORMAT
echo "convert $OPTIONS $i $j"
convert $OPTIONS $i $j
if [ -s $j ]; then rm -f $i;fi
done
