#!/bin/sh

if [ "$1" = "" ]; then
  echo -e 'Usage: makeicon FILE [DPI]\n'
  exit 1
fi

if [ "$2" = "" ]; then
  DPI=150
else
  DPI=$2
fi

DIR=$PWD
FILE=$DIR/`echo $1 | awk 'BEGIN{FS=".";} {print $1}'`
PNG=$FILE.png
PNGs="$FILE"_.png
echo $PNG

psselect 1 $1 makeicon.ps
gs -q -dSAFER -sPAPERSIZE=a5 -sDEVICE=ppmraw -sOutputFile=- -dNOPAUSE -r"$DPI"x"$DPI" makeicon.ps quit.ps > makeicon.pnm
pnmscale 0.25 < makeicon.pnm | pnmcut 9 10 200 288 | pnmtopng > $PNG
pngtopnm < $PNG | pnmscale 0.5 | pnmtopng > $PNGs

#convert $FILE.png $FILE.gif
#convert $FILE"_.png" $FILE"_.gif"

#pnmscale 0.25 < makeicon.pnm | pnmcut 18 20 400 576 | pnmtopng > $PNG
#rm makeicon.*

