#! /bin/csh -f -e

set rot = "0,0,0"
set width = 600
set height = 600

if ( $#argv < 1 ) then
  echo "usage: $0 [ -rot <x>,<y>,<z> ] [ -size width height ] infile"
  exit 1
endif

while ( $#argv > 1 )
  if ( "x$1" == "x-rot" ) then
    set rot = $2
    shift; shift
  else if ( "x$1" == "x-size" ) then
    set width = $2
    set height = $3
    shift; shift; shift
  else
    echo "unrecognized option $1"
    exit 1
  endif
end

set name = "$1"; set name = "${name:r}"
set tempname = "/tmp/$$"

if ( ! ( -e ${name}.inc ) ) then
  echo "fixing up colors and radiuses in ${name}.top ..."
  cat ${name}.top \
    | top-remove-huge-edges \
    | top-make-faces-opaque \
    | top-remove-dual-edges \
    > ${tempname}.top
  echo "converting ${name}.top to ${name}.inc ..."
  TriangToPOV \
    -inFile ${tempname} -outFile ${name}
  /bin/rm ${tempname}.top
endif

if ( ! ( -e top-pov-eps-header.inc ) ) then
  ln -s ${STOLFIHOME}/lib/top-pov-eps-header.inc
endif

echo "creating ${name}.pov ..."
cat > ${name}.pov <<EOF
#include "top-pov-eps-header.inc"
#include "${name}.inc"
EOF

echo "running POVRAY on ${name}.pov ..."
nice ${STOLFIHOME}/programs-by-others/CODE/raytrace-pov-TMP/SPARC/povray \
  +I${name}.pov +O${name}.tga \
  +A0.0 +R2 +FT +W${width} +H${height} \
  +Linclude
  
echo "converting ${name}.tga to ${name}.ppm ..."
tgatoppm < ${name}.tga > ${name}.ppm

echo "converting ${name}.ppm to ${name}.eps ..."
ppm-to-bw-eps ${name}.ppm > ${name}.eps

xv ${name}.ppm ; ghostview ${name}.eps &

