#!/bin/sh
if [ $(basename $1) = "template.pov" ]; then
    USER_TEMPLATE=1;
else
    USER_TEMPLATE=0;
fi

INIFILE=$(tempfile)

cat > $INIFILE <<EOF
;; Image size.
Width=400			;; (+Wn)
Height=400			;; (+Hn)


;; Partial trace of the image.
;; Values range from top left to bottom right.
;; Can use either 0.0 to 1.0 as fraction of height/width, or
;; whole numbers as rows and columns.
;Start_Row=0.1			;; (+SR0.n / +SRn)
;End_Row=0.2			;; (+ER0.n / +ERn)
;Start_Column=45		;; (+SC0.n / +SCn)
;End_Column=90			;; (+EC0.n / +ECn)


;; Test for trace abort.  Use 'q' or 'Q' to abort if enabled.  Not
;; really needed on Unix as you can always use the Interrupt signal.
Test_Abort=true			;; (+/-X)
Test_Abort_Count=100		;; (+Xn)


;; Output quality. (Ray tracer actions).
;;     0-1    Quick colours.  Full ambient lighting only.
;;     2-3    Quick colours.  Diffuse and ambient lighting only.
;;     4      Quick colours.  Render shadows.  No extended lights.
;;     5      Quick colours.  Render shadows including extended lights.
;;     6-7    Texture patterns.
;;     8      Reflected, refracted and transmitted rays.
;;     9      Compute media.
;;     10     Compute radiosity but no media.
;;     11     Compute radiosity and media.
Quality=9			;; (+Qn)


;; Anti-aliasing controls.
;;Antialias=true			;; (+/-An)
;;Sampling_Method=2


Antialias=Off
;;Antialias_Threshold=0.25
Antialias_Depth=5


;; Display the image while tracing controls.
Display=false			;; (+/-D)
Pause_When_Done=false		;; (+/-P)


;; Internal animation loop control.
;; The 'clock' definition within the source changes from 0 to 1 for each
;; frame of the animation.  If cyclic animation is enabled it steps from
;; 0 to a fraction below 1 (actually 1 - 1/Final_Frame).
;Initial_Frame=1		;; (+KFIn)
;Final_Frame=36			;; (+KFFn)
;Cyclic_Animation=true		;; (+/-KC)


;; File output type control.
;;     T    Uncompressed Targa-24
;;     C    Compressed Targa-24
;;     P    UNIX PPM
;;     N    PNG (8-bits per colour RGB)
;;     Nc   PNG (``c'' bit per colour RGB where 5 <= c <= 16)
Output_to_File=true
Output_File_Type=N		;; (+/-Ftype)


;; Define the Gamma output responce of the monitor.
;; This is about correct for most Unix an PC monitors.
Display_Gamma=2.2		;; (Not available as command line switch)
EOF

for a in $*
  do
 #term_title Rendering: $a
  
  if [ $a != "template.pov" ]; then
      if [ $USER_TEMPLATE = 0 ]; then
	  cat > ${a%.xml}.pov <<EOF
// Persistence Of Vision raytracer version 3.1 sample file.

global_settings { assumed_gamma 2.2 }

camera {
   location  <0.0, 0.0, 3.0>
   direction <0, 0,    -1>
   up        <0, 1,    0>
   right   <1, 0,    0>
   look_at   <0.0, 0.0,    0>
}

// Light source

light_source {< -20, 21, 20>  color <1.0, 1.0, 1.0>  }
light_source {< 1, 1, 20>  color <1.0, 1.0, 1.0>  }
light_source {< 20, -10, 20>  color <1.0, 1.0, 1.0> }
EOF
      else
	  cat template.pov > ${a%.xml}.pov
      fi

#      if [ ! -f ${a%.xml}.png ]; then
      if [ ${a%.xml}.png -ot $a ]; then
          nice -19 ko-phys2pov $a >> ${a%.xml}.pov 2> /dev/null
          nice -19 povray $INIFILE +I${a%.xml}.pov 2> /dev/null
          rm ${a%.xml}.pov
          echo Rendered ${a%.xml}.png
      else
          echo Skipping ${a%.xml}.png, already up to date
      fi
  fi
done

rm $INIFILE
