#! /bin/bash 

NEO="$1"; shift
OSHAPE="$1"; shift
INNER="$1"; shift
SUBFIG="$1"; shift

QUICK=1
if [[ ${QUICK} -ne 0 ]]; then
  WIDTH=400
  HEIGHT=400
  NRAYS=1
else
  WIDTH=800
  HEIGHT=800
  NRAYS=2
fi

printf "run_hel_povray.sh: NEO = ${NEO} OSHAPE = ${OSHAPE} INNER = ${INNER}" 1>&2
printf " SUBFIG = ${SUBFIG}\n" 1>&2

printf "run_hel_povray.sh: QUICK = ${QUICK}" 1>&2
printf " ( WIDTH = ${WIDTH} HEIGHT = ${HEIGHT} NRAYS = ${NRAYS} )\n" 1>&2

# Should images be displayed automatically?
SHOW=YES

# Should filter error messages (0 or 1):
FILTER=1

# Directories where POV-Ray is installed:
POVRAY=/usr/bin/povray
POVINC=( +L.. )

NEO8="`printf '%08d' ${NEO}`"

# Main input files:
POV_FILE=hel_main.pov
INC_FILE="out/hel_${NEO8}_osh${OSHAPE}_ich${INNER}.inc"

# Output image file:
IMG_FILE="out/hel_${NEO8}_${OSHAPE}_${INNER}_${SUBFIG}.png"

# Command to display resultin image:
IMVIEW=( display -title '%d/%f' )

rm -f ${IMG_FILE} params.inc
echo "#declare inc_file = \"${INC_FILE}\"" >> params.inc
echo "#declare subfig = ${SUBFIG};" >> params.inc
${POVRAY} \
    +K0.5000 \
    +FN +Q9 \
    +W${WIDTH} +H${HEIGHT} \
    +AM1 +A0.0 +R${NRAYS} \
    +D \
    ${POV_INC[@]} \
    +I${POV_FILE} \
    +O${IMG_FILE} \
  2>&1 \
  | povray-output-filter.gawk -v do_filter=${FILTER}

if [[ "/${SHOW}" == "/YES" ]]; then
  if [[ -s ${IMG_FILE} ]]; then
    ${IMVIEW[@]} ${IMG_FILE}
  fi
fi
