#!/bin/sh
#
# Start POV-Ray, reading a POV file as input, creating a TIFF file.
# Requires pbmplus/netpbm.
#
# The first argument is the file name, without extension, the
# second argument the desired horizontal resolution (width) of
# the image. The height is determined to preserve the aspect
# ratio. This may not work for general POV files, it works for
# the ones generated by MOLMOL.
#
# If the option -n is given, text labels do not throw shadows.
#
# Note that it is normally more convenient to use the povmol
# script.

LIBDIR=/opt/group/lib/povray3

if [ $1 = "-n" ]; then
  shadow=n
  shift
else
  shadow=y
fi

width=$2
height=`awk -F, '
  BEGIN { u = 1.0 }
  /^[ ]*up / {
    u = $2 + 0.0
    if (u < 0.0)
      u = - u;
  }
  /^[ ]*right / {
    i = index($0, "<")
    j = index($0, ",")
    r = substr($0, i + 1, j - i - 1) + 0.0
    if (r < 0.0)
      r = - r
    print int(width * (u / r) + 0.5)
    exit
  }
' width=$width $1.pov`

if [ $shadow = "n" ]; then
  awk '
    BEGIN { inText = 0 }
    /^text \{/ { inText = 1 }
    /^\}$/ {
      if (inText == 1) {
	print "no_shadow"
	inText = 0
      }
    }
    { print }
  ' $1.pov > $1.pov.tmp
  povname=$1.pov.tmp
else
  povname=$1.pov
fi

povray $LIBDIR/povray.ini -i$povname -o$1.ppm -w$width -h$height

if [ $shadow = "n" ]; then
  rm $1.pov.tmp
fi

pnmtotiff $1.ppm > $1.tif

rm $1.ppm
