#! /bin/csh -f -e

set usage = "$0 [ -good | -quick ] [ -detail XMIN XMAX YMIN YMAX ] [ -tiny | -small | -plain | -big | -size WD HT ] [ -scale s ] [ -in DIR ] [ -list DIRLIST ] [ -hist ] [ -transparent ] [ -noshow ] SCENE.pov"

# Generates POVRAY rendering of a scene
#
# All output (incluring a copy of the data files)
# goes to a subdirectory whose name is the current date and time.
# The subdirectory is automatically deleted if the run fails.
# Otherwise the subdirectory's name is stored in the files 
# ".last" in the current directory, and appended to the 
# file DIRLIST (which defaults to "SCENE.dirlist")
#
# The current directory may include a file called SCENE.sizes
# redefining the csh variables
#   ${tinysize}   default ( 050 050 )
#   ${smallsize}  default ( 100 100 )
#   ${plainsize}  default ( 200 200 )
#   ${bigsize}    default ( 400 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, and then uses ppmoquant to merge them into 
# a partially-transparent gif.  The SCENE.pov should #include 
# a file called "background.inc", generated by this script,
# which will contain
#    #declare bgColor = color rgb <...>
#    background {color bgColor}
#
# 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 postertools = "/home/lac/stolfi/posters/tools"

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

set cmdname = "$0"
set runargs = "$*"
echo "${cmdname} ${runargs}"

set size = ( "plain" )
set rays = 3
set qual = 9
set transp = 0
set scaleopts = ( )
set show = 1
unset givendir 
unset dirlist
set detailopts = ( )
set histopts = ( "+HTX" )

while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( "x$1" == "x-quick" ) then 
    set rays = 1
    set qual = 4
    shift
  else if ( "x$1" == "x-noshow" ) then 
    set show = 0
    shift
  else if ( "x$1" == "x-hist" ) then 
    set histopts = ( "+HTP" )
    shift
  else if ( ( $#argv >= 5 ) && ( "x$1" == "x-detail" ) ) then 
    set detailopts = (  "+SC$2" "+EC$3" "+SR$4" "+ER$5" )
    shift; shift; shift; shift; shift
  else if ( "x$1" == "x-good" ) then 
    set rays = 5
    set qual = 9
    shift
  else if ( "x$1" == "x-tiny" ) then 
    set size = ( "tiny" )
    shift
  else if ( "x$1" == "x-small" ) then 
    set size = ( "small" )
    shift
  else if ( "x$1" == "x-plain" ) then 
    set size = ( "plain" )
    shift
  else if ( "x$1" == "x-big" ) then 
    set size = ( "big" )
    shift
  else if ( ( $#argv >= 3 ) && ( "x$1" == "x-size" ) ) then 
    set size = ( $2 $3 )
    shift; shift; shift
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-scale" ) ) then 
    set scaleopts = ( "-scale" "$2" );  shift; shift;
  else if ( "x$1" == "x-transparent" ) then 
    set transp = 1;  shift
  else if ( ( $#argv >= 3 ) && ( "x$1" == "x-in" ) ) then 
    set givendir = "$2";  shift; shift
  else if ( ( $#argv >= 3 ) && ( "x$1" == "x-list" ) ) then 
    set dirlist = "$2";  shift; shift
  else
    echo "usage: ${usage}" ; exit 1
  endif
end
  
if ( $#argv != 1 ) then
  echo "usage: ${usage}"; exit 1
endif
 
set scene = "$1"
if ( "x${scene:e}" != "xpov" ) then
  echo "usage: ${usage} "
  exit 1
endif
set scene = ${scene:r}

if ( ! ( $?dirlist ) ) set dirlist = "${scene}.dirlist"

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"
  if ( -r $d/run-povray.args ) /bin/mv $d/run-povray.args $d/run-povray.args~
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,comments} povray.options do-run-povray )
    if ( -r $f ) cp -p $f $d
  end
endif

set curd = "$cwd"
cd $d

echo "${cmdname} ${runargs}" > run-povray.args
echo "[`uname -n`] ${cmdname:t} ${runargs}" >> ${scene}.comments

if ( $#size == 1 ) then
  if ( -r ${scene}.sizes ) then
    source ${scene}.sizes 
  endif
  if ( ! $?tinysize )  set tinysize  = ( 050 050 )
  if ( ! $?smallsize ) set smallsize = ( 100 100 )
  if ( ! $?plainsize ) set plainsize = ( 200 200 )
  if ( ! $?bigsize )   set bigsize   = ( 400 400 )
  if ( "$size[1]" == "tiny" ) then
    set size = ( ${tinysize} )
  else 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

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

if ( ${transp} == 0 ) then
  # run povray once, with white background:
  echo "#declare bgColor = color rgb <0.9, 0.9, 0.9>" > background.inc
  echo "background { color bgColor }" >> background.inc
  /n/gnu/bin/nice -n 19 \
    povray \
      +A0.0 +AM1 +FP +Q${qual} +R${rays} +W${size[1]} +H${size[2]} \
      ${histopts} +HN${scene}-time.ppm \
      ${povopts} ${sceneopts} ${detailopts} \
      +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
    ln -s ${scene}.ppm p.ppm
    ln -s ${scene}.comments p.comments

    linear-ppm-to-gif ${scaleopts} ${scene}.ppm > ${scene}.gif
    ln -s ${scene}.gif p.gif

    linear-ppm-to-gif -ysize 50 ${scene}.ppm > ${scene}-icon.gif
    ln -s ${scene}-icon.gif p-icon.gif
  endif
else
  # run povray twice, with black and white backgrounds:
  foreach bg ( 0 1 )
    echo "=== generating image with background = ${bg} ==="
    echo "#declare bgColor = color rgb <${bg}, ${bg}, ${bg}>" > background.inc
    echo "background { color bgColor }" >> background.inc
    /n/gnu/bin/nice -n 19 \
      povray \
        +A0 +FP +Q${qual} +R${rays} +W${size[1]} +H${size[2]} \
        ${povopts} ${sceneopts} ${detailopts} \
        +L/home/lac/stolfi/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
    ln -s ${scene}-0.ppm p-0.ppm
    ln -s ${scene}-1.ppm p-1.ppm

    linear-ppm-pair-to-gif ${scaleopts} ${scene}-{0,1}.ppm > ${scene}.gif
    ln -s ${scene}.gif p.gif
    ln -s ${scene}.comments p.comments

    linear-ppm-pair-to-gif -ysize 50 ${scene}-{0,1}.ppm > ${scene}-icon.gif
    ln -s ${scene}-icon.gif p-icon.gif

  endif
endif

cd ${curd}
if ( ( -r $d/${scene}.gif ) && ( ! ( -z $d/${scene}.gif ) ) ) then
  echo 'rendering succeeded in directory '"$d"
  echo "$d" > .last
  echo "$d" >> ${dirlist}
  if ( ${show} != 0 ) xv $d/p.gif $d/${scene}*.{gif,ppm} $d/p.comments &
  make-image-index-entry $d/${scene}.gif > $d/${scene}.html-inc
  ln -s ${scene}.html-inc $d/p.html-inc   
  ${postertools}/make-povray-index
  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
