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

echo ===================================================================


appli=`basename $0`

# verification du bon nombre d'arguments (nom du .ps)

if ((test $# -eq 0) || (test $# -gt 2))
   then echo "il faut  le nom du fichier .ps en argument"
        echo "exemple: ps_to_pdf.sh toto.ps"
        echo "     ou: ps_to_pdf.sh toto.ps compress"
        echo "         (les images seront alors compressees)"
        echo "*** $appli aborted !!"
        exit 1
fi

# verification que le fichier .ps existe

if test ! -f $1
   then echo "Le fichier $1 n'existe pas"
        echo "*** $appli aborted !!"
        exit 2
fi

# fabrication du pdf
# si il existe un deuxielme argument, de type "compress",
# on compresse les images.

nompdf=`basename $1 .ps`.pdf

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 "    command aborted !"
        exit 1
fi


if (test $# -eq 2) && (test $2 = compress)
   then 
        echo "conversion of $1 "
        echo "           in $nompdf"
        echo "WITH  image compression"
        $lgs  -dBATCH -dNOPAUSE -dSAFER -dQUIET -sPAPERSIZE=a4 \
             -sDEVICE=pdfwrite -sOutputFile=$nompdf $1  
   else 
        echo "conversion of $1 "
        echo "           in $nompdf"
        echo "WITHOUT image compression"
        $lgs -dBATCH -dNOPAUSE -dSAFER -dQUIET -sDEVICE=pdfwrite -sOUTPUTFILE=$nompdf \
	    -sPAPERSIZE=a4  -dCompatibilityLevel=1.3  \
	    -dEmbedAllFonts=true          -dSubsetFonts=true  -dMaxSubsetPct=100  \
	    -dAutoFilterColorImages=false -dColorImageFilter=false  \
	    -dAutoFilterGrayImages=false  -dGrayImageFilter=false  \
            -dAutoFilterMonoImages=false  -dMonoImageFilter=false  $1
fi

echo " "
wc -c $1
wc -c $nompdf

echo " "
echo ===================================================================
