#!/usr/bin/env bash
# $Id$
# --------------------------------------------------------------------------
#
#              //===//   //=====   //===//   //       //   //===//
#             //    //  //        //    //  //       //   //    //
#            //===//   //=====   //===//   //       //   //===<<
#           //   \\         //  //        //       //   //    //
#          //     \\  =====//  //        //=====  //   //===//   Version III
#
# ------------- An Efficient RSerPool Prototype Implementation -------------
#
# Copyright (C) 2002-2025 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com


# ====== Get arguments ======================================================
if [ $# -lt 3 ] ; then
   echo >&2 "ERROR: Usage $0 [Width] [Height] [Input POV]"
   exit 1
fi
WIDTH=$1
HEIGHT=$2
INPUT=$3
OUTPUT="`echo $INPUT | sed -e "s/.pov/-$WIDTH-$HEIGHT.png"/g`"
if [ -e $OUTPUT ] ; then
   echo >&2 "ERROR: Output file $OUTPUT already exists!"
   exit 1
fi


# ====== Create temporary directory =========================================
TEMPDIR="temp-$INPUT-$WIDTH-$HEIGHT"
umask 077
rm -rf $TEMPDIR
mkdir $TEMPDIR
cp $INPUT $TEMPDIR/input.pov
find -name "*.inc" | xargs --no-run-if-empty -n1 -i§ cp § $TEMPDIR


# ====== Write ssrun script =================================================
(
   echo "#!/bin/sh"
   echo "OUTPUT_ARCHIVE=\$1"
   echo "SUCCESS=1"
   echo -n "povray -w$WIDTH -h$HEIGHT +a0.3 -D +FN8 +Ooutput.png "
   echo    "+Iinput.pov >output.txt 2>&1 || SUCCESS=0"
   echo "tar czvf \$OUTPUT_ARCHIVE output.png output.txt || SUCCESS=0"
   echo "exit $SUCCESS"
) >"$TEMPDIR/ssrun"
chmod +x "$TEMPDIR/ssrun"


# ====== Create and distribute work package =================================
cd "$TEMPDIR"
find . -name "ssrun" -or -name "input.pov" -or -name "*.inc" | \
   xargs tar czf input.tar.gz
cd ..
scriptingclient -quiet -input=$TEMPDIR/input.tar.gz \
                -output=$TEMPDIR/output.tar.gz
if [ -e "$TEMPDIR/output.tar.gz" ] ; then
   cd "$TEMPDIR"
   tar xzf output.tar.gz
   cd ..
   if [ -e "$TEMPDIR/output.png" ] ; then
     mv $TEMPDIR/output.png $OUTPUT
     rm -rf $TEMPDIR
   else
      echo >&2 "ERROR: No image has been created. Check log:"
      echo "------ LOG ------------------------------------------------"
      cat "$TEMPDIR/output.txt"
      echo "-----------------------------------------------------------"
   fi
fi
