#!/bin/bash

# 1 2 wh 3 4 wh 5 out 6 in

CRC=crc32
CACHE=~/.TeXmacs/system/cache/images

echo crc32 $6
CRC=`$CRC $6 | sed  's/\(.\)/\\1\//g'`
echo $CRC

mkdir -p $CACHE/$CRC

img=$CACHE/$CRC/img_$1x$2_$3x$4.png

EPSFILE=`mktemp -t ps_to_png_tmp.XXXX`


if ! [ -a $img ] ; then
    ps2epsi $6 $EPSFILE # This is only needed because at some point
    # There was a bug that didn't create correct eps files, and now
    # I have some .tm files with wrong ps in them.
    gs -dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -dGraphicsAlphaBits=4 -dEPSCrop -g$1"x"$2 -r$3"x"$4 -sOutputFile=$img $EPSFILE
    rm $EPSFILE
fi
ln -s $img $5