#! /bin/csh -f -e

# Generates POVRAY rendering of scene $1.pov
#
# All output (incluring a copy of the data files)
# goes to a subdirectory whose name is the current date and time.
# The directory is automatically deleted if the run fails.
#
# The current directory may include a file called SCENE.sizes
# redefining the csh variables
#   ${smallwidth}, ${smallheight}  (default 100)
#   ${plainwidth}, ${plainheight}  (default 200)
#   ${bigwidth},   ${bigheight}    (default 400)
# 
# The current directory may also include files called SCENE.options
# and povray.options, with additional options to be passed to POVRAY
# (for instance, "+L" options).
#
# The "-transparent" flag runs povray twice, with black and white
# background (which the scene must #include from a file called 
# "background.inc"), and then uses ppmoquant to merge them into 
# a partially-transparent gif.
#
# The "-in" flag specifies that povray should be run in an
# existing directory DIR, which should contain all the 
# necessary files.  In this case, DIR is not removed if the run fails.
# This option is useful to re-run an old run with, say, increased 
# resolution or improved tools.
#

set povbin = "/n/lac/pkg/povray-3.0-1/PUB/sun4-SunOS-5.5/bin"

set path = ( ${povbin} ${path} )

set usage = "$0 [ -quick | -small | -big | -size WD HT ] [ -in DIR ] [ -transparent ] SCENE.pov"

set size = ( "plain" )
set rays = 3
set qual = 9
set transp = 0
unset givendir 

while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( "x$1" == "x-quick" ) then 
    set rays = 1
    set qual = 4
    set size = ( "small" )
    shift
  else if ( "x$1" == "x-small" ) then 
    set rays = 3
    set qual = 9
    set size = ( "small" )
    shift
  else if ( "x$1" == "x-big" ) then 
    set rays = 3
    set qual = 9
    set size = ( "big" )
    shift
  else if ( ( $#argv >= 3 ) && ( "x$1" == "x-size" ) ) then 
    set size = ( $2 $3 )
    shift; shift; shift
  else if ( "x$1" == "x-transparent" ) then 
    set transp = 1;  shift
  else if ( ( $#argv >= 3 ) && ( "x$1" == "x-dir" ) ) then 
    set givendir = "$2";  shift; shift
  else
    echo ${usage} ; exit 1
  endif
end
  
if ( $#argv != 1 ) then
  echo ${usage}; exit 1
endif

set scene = "$1"
if ( "x${scene:e}" != "xpov" ) then
  echo "usage: ${usage} "
  exit 1
endif
set scene = ${scene:r}

if ( $?givendir ) then
  if ( ! ( -d ${givendir} ) ) then
    echo "$givendir: not a directory" ; exit 1
  endif
  set d = "${givendir}"
  echo "re-generating picture in directory $d"
else
  set d = `date +%y-%m-%d-%H%M%S`
  echo "generating picture in directory $d"
  mkdir $d
  cp -p $0 $d
  cp -p ${scene}.pov [a-zA-Z0-9]*.inc $d
  foreach f ( ${scene}.{sizes,options} povray.options )
    if ( -r $f ) cp -p $f $d
  end
endif

set curd = "$cwd"
cd $d

if ( $#size == 1 ) then
  if ( -r ${scene}.sizes ) then
    source ${scene}.sizes 
  else
    set smallsize = ( 100 100 )
    set plainsize = ( 200 200 )
    set bigsize   = ( 400 400 )
  endif
  if ( "$size[1]" == "small" ) then
    set size = ( ${smallsize} )
  else if ( "$size[1]" == "plain" ) then
    set size = ( ${plainsize} )
  else if ( "$size[1]" == "big" ) then
    set size = ( ${bigsize} )
  else 
    echo "run-povray: internal bug (size = ${size})"
    exit 1
  endif
endif

if ( -r ${scene}.options ) then
  set sceneopts = ( `cat ${scene}.options` )
else
  set sceneopts = ( )
endif
if ( -r povray.options ) then
  set povopts = ( `cat povray.options` )
else
  set povopts = ( )
endif

/bn/rm -f ${scene}.gif p.gif
set povrayok = 1

if ( ${transp} == 0 ) then
  # run povray once, with gray default background:
  echo "background { color rgb <0.5, 0.5, 0.5> }" > background.inc
  nice \
    povray \
      +A0 +FP +Q${qual} +R${rays} +W${size[1]} +H${size[2]} \
      ${povopts} ${sceneopts} \
      +L${HOME}/PUB/povray \
      +I${scene}.pov \
      +O${scene}.ppm \
    |& tee povray.log

  if ( ( ! ( -r ${scene}.ppm ) ) || ( -z ${scene}.ppm ) ) then
    set povrayok = 0
  endif
  
  if ( ${povrayok} ) then
    linear-ppm-to-gif ${scene}.ppm > ${scene}.gif
    ln -s ${scene}.gif p.gif
    ln -s ${scene}.ppm p.ppm
  endif
else
  # run povray twice, with black and white backgrounds:
  foreach bg ( 0 1 )
    echo "=== generating image with background = ${bg} ==="
    echo "background { color rgb <${bg}, ${bg}, ${bg}> }" > background.inc
    nice \
      povray \
        +A0 +FP +Q${qual} +R${rays} +W${size[1]} +H${size[2]} \
        ${povopts} ${sceneopts} \
        +L${HOME}/PUB/povray \
        +I${scene}.pov \
        +O${scene}-${bg}.ppm \
      |& tee povray-${bg}.log
    if ( ( ! ( -r ${scene}-${bg}.ppm ) ) || ( -z ${scene}-${bg}.ppm ) ) then
      set povrayok = 0
      break
    endif
  end

  if ( ${povrayok} ) then
    linear-ppm-pair-to-gif ${scene}-{0,1}.ppm > ${scene}.gif
    ln -s ${scene}.gif p.gif
    ln -s ${scene}-0.ppm p-0.ppm
    ln -s ${scene}-1.ppm p-1.ppm
  endif
endif

cd ${curd}
if ( ( -r $d/${scene}.gif ) && ( ! ( -z $d/${scene}.gif ) ) ) then
  echo 'rendering succeeded in directory '"$d"
  xv $d/p.gif $d/${scene}*.{gif,ppm} &
  exit 0
else
  echo 'rendering failed in directory '"$d"
  if ( ( ! $?givendir ) && ( ! ${povrayok} ) ) then
    echo "deleting directory $d"
    /bin/rm -rf "$d"
  endif
  exit 1
endif
