#/!bin/bash


tmpfile1=$(tempfile --prefix xfigt --suffix .png)

# remove file if something strange happens
trap 'rm -rf $tmpfile1' INT EXIT TERM

die() {
  echo >&2 "$@"
  rm -rf $tmpfile1
  exit 1
}
 
# The script will be called with parameters : -s %s %u %o
# -s %s: vertical size,  %u: input url, %o: output file

if [ $1 = "-s" ]; then 
   size=$2
   uin=$3
   fout=$4
else
   size=32 #default size
   uin=$1
   fout=$2
fi

# uin es una url, eliminar "file://"
fin=${uin#file://*}

fig2dev -L png $fin $tmpfile1 || die "fig2dev failed"
convert  $tmpfile1 -resize x$size \
        -gravity northwest -fill darkgreen -pointsize $((size/3)) \
        -annotate 0 "Xfig" $fout || die "convert failed"
rm -rf $tmpfile1
exit 0

