#!/bin/bash
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#
#    convertit un fichier toto.ps en fichier toto.png 
#
#                         Patrick ROBERT, 1996
#                   revision Nov 2011 pour compatibilite PostScript
#                   ajou du mode png janvier 2015
#
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

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 3 arguments, ex :"
        echo "$appli   toto.ps  80 256"
        echo "$appli   toto.ps 300 16m"
        echo "         (300= res in ppi or dpi, 256 colors or 16 M)"
        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 16 M couleurs

nompng=`basename $1 .ps`.png
if test -f $nompng ; then \rm $nompng ; fi

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=png$3 -sOutputFile=$nompng -r$2x$2 $1

echo " $nompng    created !"
