#!/bin/bash
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#
#    convertit un fichier toto.ps en fichier toto.png 256 couleurs
#
#                         Patrick ROBERT, 1996
#                   revision Nov 2011 pour compatibilite PostScript
#                   (image non compressees, et donc non deformees,
#                    requis pour les spectrogrammes)
#
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=2

if( (test $# = 1 ) && (test $1 = -h ) ) ; then hh=1 ; else hh=0 ; fi

if test $# != $Narg || test $hh = 1
   then echo "$appli : produce .png file from .ps one"
        echo "$appli   require 2 arguments, ex :"
        echo "$appli   toto.ps  80"
        echo "$appli   toto.ps 300"
        echo "         (300= res in ppi our dpi)"
        if test $hh = 1 ; then exit 0 ; fi
        echo "$appli36 : *** ERROR ! Command aborted." >&2
        exit 1
fi

# verification que le fichier .ps existe

if test ! -f $1
   then echo "no-existing ps file $1"
        echo "$appli36 : *** ERROR ! Command aborted." >&2
        exit 2
fi

# fabrication du png 256

nompng=`basename $1 .ps`.png

Syst=`uname -s | cut -c1-5`
if (test $Syst = 'MINGW') 
    then lgs=gswin32c.exe
    else lgs=gs
fi

which $lgs > /dev/null
if test $? != 0
   then echo "gs is not installed on your $Syst system"
        echo "*** $appli aborted !"
        exit 3
fi

$lgs  -dBATCH -dNOPAUSE -dSAFER -dQUIET -sPAPERSIZE=a4 \
      -sDEVICE=png256 -sOutputFile=$nompng -r$2x$2 $1

echo "$nompng    created !"
