#! /bin/bash -f
# Last edited on 2019-04-05 20:59:17 by stolfilocal

usage="$0 FORMAT SCENE STYLE OPTIONS"

cmd="$0"; cmd="${cmd%%*/}"
runargs="$@"

echo '====================================================================='
echo '=== '"${cmd} ${runargs}"

if [[ $# -lt 3 ]]; then
  echo ${usage} 2>&1; exit 1
fi

scene="$1"; shift
format="$1"; shift
style="$1"; shift

parminc="parameters.inc"
parmsh="parameters.sh"
makefile="${format}.make"
cmtfile="${format}.comments"

# Create the parameter files for inclusion by the .pov file
# and by scripts:

/bin/rm -f ${parminc}
cat <<EOF > ${parminc}
//  created by $0 - DO NOT EDIT
#declare style_tag = "${style}"
#declare scene_tag = "${scene}"
EOF
if [[ -s ${parmsh} ]]; then
  cat ${parmsh} \
    | sed \
        -e '/^[ ]*[#]/d' \
        -e '/^[ ]*$/d' \
        -e 's/[; ]*$/;/' \
        -e 's/^[ ]*/#declare sh_/' \
    >> ${parminc}
fi

# Create a makefile to build mechanically generated objects

if [[ -s scene-${scene}.make ]]; then
  echo "# created by $0 - DO NOT EDIT" > ${makefile}
  cat scene-${scene}.make >> ${makefile}
else
  /bin/rm -rf ${makefile}
fi

# Create the ".comments" file:

/bin/rm -f ${cmtfile}
echo "${cmd} ${runargs}" > ${cmtfile}
echo "  scene = ${scene}" >> ${cmtfile}
echo "  format = ${format}" >> ${cmtfile}
echo "  style = ${style}" >> ${cmtfile}

${STOLFIHOME}/posters/tools/run-povray-png -show $@ ${format}.pov

echo '=== END '"${cmd} ${runargs}" 

echo '====================================================================='
